forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoops.java
More file actions
33 lines (28 loc) · 711 Bytes
/
Loops.java
File metadata and controls
33 lines (28 loc) · 711 Bytes
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
/**
* Examples from Chapter 7.
*/
public class Loops {
public static void countdown(int n) {
while (n > 0) {
System.out.println(n);
n = n - 1;
}
System.out.println("Blastoff!");
}
public static void sequence(int n) {
while (n != 1) {
System.out.println(n);
if (n % 2 == 0) { // n is even
n = n / 2;
} else { // n is odd
n = n * 3 + 1;
}
}
}
public static void main(String[] args) {
System.out.println("countdown");
countdown(3);
System.out.println("sequence");
sequence(10);
}
}