forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopPractice.java
More file actions
141 lines (128 loc) · 2.49 KB
/
LoopPractice.java
File metadata and controls
141 lines (128 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
public class LoopPractice
{
public static void main(String[] args)
{
forOneToTen();
whileOneToTen();
doWhileOneToTen();
forTenToOne();
whileTenToOne();
doWhileTenToOne();
forByTens();
whileByTens();
doWhileByTens();
forSequence();
whileSequence();
doWhileSequence();
printOneToNumber(15);
}
public static void forOneToTen()
{
for (int i = 1; i <= 10; i++)
{
System.out.println(i);
}
}
public static void whileOneToTen()
{
int i = 1;
while (i <= 10)
{
System.out.println(i);
i++;
}
}
public static void doWhileOneToTen()
{
int i = 1;
do
{
System.out.println(i);
i++;
} while (i <= 10);
}
public static void forTenToOne()
{
for (int i = 10; i >= 1; i--)
{
System.out.println(i);
}
}
public static void whileTenToOne()
{
int i = 10;
while (i >= 1)
{
System.out.println(i);
i--;
}
}
public static void doWhileTenToOne()
{
int i = 10;
do
{
System.out.println(i);
i--;
} while (i >= 1);
}
public static void forByTens()
{
for (int j = 0; j <= 100; j += 10)
{
System.out.println(j);
}
}
public static void whileByTens()
{
int j = 0;
while (j <= 100)
{
System.out.println(j);
j += 10;
}
}
public static void doWhileByTens()
{
int j = 0;
do
{
System.out.println(j);
j += 10;
} while (j <= 100);
}
public static void forSequence()
{
for (int k = 100; k >= -100; k -= 8)
{
System.out.println(k);
}
}
public static void whileSequence()
{
int k = 100;
while (k >= -100)
{
System.out.println(k);
k -= 8;
}
}
public static void doWhileSequence()
{
int k = 100;
do
{
System.out.println(k);
k -= 8;
} while (k >= -100);
}
public static void printOneToNumber(int n)
{
int k = 1;
while (k <= n)
{
System.out.println(k);
k += 1;
}
}
}