java loops
What is the output of the following code segment? int num = 1, max = 20; while (num < max) { println(num); num += 4; }
1 5 9 13 17
What is the output of the following code segment? int num = 1, max = 20; while (num < max) { num += 4; println(num); }
5 9 13 17 21
What is the output of the following code segment? String letter = "A"; while (!letter.equals("AAAA")) { println(letter); letter = letter + "A"; }
A AA AAA
What is the continue keyword used for?
The keyword allows us to preemptively end the current iteration of a loop.
What is the break keyword used for?
The keyword allows us to preemptively exit a loop.
What is wrong with the following code segment? What do we call this behavior? double number = 2.0; while (number <= 20) { println(number); number -= 2.0; }
This is an infinite loop.
Write a for loop that prints the following numbers: 3 4 5 6 7 8 9
for (int i = 3; i <= 9; i++) { print(i + " "); }
Write a for loop that prints the following series of numbers: 6 8 10 12 14 16
for (int i = 6; i <= 16; i += 2) { print(i + " "); }
Write a for loop that prints the following series of numbers: 7 6 5 4 3 2 1 0
for (int i = 7; i >= 0; i--) { print(i + " "); }
