Java Arena
Step 1 of 7

while with a condition

while (true) needs a break to stop. A loop can instead have a real condition: it's checked before every round, and the loop ends the first time that check finds it false.

int number = 1;
while (number <= 5) {
    System.out.println(number);
    number++;
}
System.out.println("Done, number is now " + number);
Output
1
2
3
4
5
Done, number is now 6

This is a counting loop, and it has three parts:

  1. a variable set before the loop (int number = 1;),
  2. a condition that decides whether another round runs (number <= 5),
  3. a change inside the loop that moves toward the end (number++).

Forget the third part and number stays 1 forever: the condition never turns false.

When the loop should end at a known point, a condition says so right at the top. while (true) with break suits loops that end on something the loop reads, like a word such as end.

Your task

Challenge 1 of 3

Fill in the condition, so the loop prints 10, 20, 30, 40 and 50, one per line.

Challenge 2

Read a number of steps and print Step 1, Step 2 and so on, up to that number. Use while with a condition, not while (true). For the input 3 the program prints:

Step 1
Step 2
Step 3

Challenge 3

Read a whole number, then keep halving it (whole-number division by 2) and print each value while it's above 0. Use a condition, not break. For the input 20 the program prints:

20
10
5
2
1