Java Arena
Step 4 of 7

When the condition is checked

A loop's condition is checked at one moment only: before each round, including the very first. It isn't watched during the round. If the variable passes the limit halfway through the block, the rest of the block still runs, and the loop stops only at the next check.

int total = 0;
int rounds = 0;
while (total < 10) {
    total += 4;
    rounds++;
    System.out.println("Round " + rounds + ": total " + total);
}
System.out.println("After the loop: " + total);
Output
Round 1: total 4
Round 2: total 8
Round 3: total 12
After the loop: 12

In round 3, total went past 10, but the round finished and printed. Only then did the check 12 < 10 fail. So after a loop, its variable holds the first value that made the condition false, which isn't necessarily the limit.

Tracing a loop by hand (writing down each variable after each round) is the surest way to know what it does. These three challenges are for tracing.

Your task

Challenge 1 of 3

What does this program print? Trace i and rounds round by round.

Challenge 2

What does this program print? Two variables change in each round.

Challenge 3

What does this program print? The loop's variable also changes inside the block.