-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtutorial_18.java
55 lines (43 loc) · 1.16 KB
/
tutorial_18.java
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
import static java.lang.System.out;
class Driver {
public static void main(String[] args) {
/* infinit loop
while (true) {
Careful.
I will
run from
top to
bottom forever.
}
*/
/* unreachable code
while (false) {
This will throw an
error because you
can never use it.
}
*/
// break out of a loop
while (true) {
out.println("Time to leave");
break;
}
// counter loop
int i = 0;
while (i < 10) {
out.println("Iteration: " + i);
i++; // need to update your counter
// or you will create an infinit loop
}
// conditional loop
boolean running = true;
while (running) {
out.println("I will stop this loop now.");
running = false;
}
// loop first, then check to loop again
do {
out.println("I will run at least once.");
} while(false);
}
}