Skip to content
Computer ApplicationsICSE Class 9 · Looping (Iterative Statements) in Java

for, while and do-while Loops in Java with Output Tracing (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

A loop repeats statements while a condition is true. Every loop needs an initialisation, a test condition and an update. The for loop and the while loop test the condition before the body (entry controlled), so the body may run zero times; the do-while loop tests after the body (exit controlled), so the body runs at least once. A loop whose condition never becomes false is an infinite loop.

In for and while the test comes before the body, so it may never run; in do-while the body runs first, so it runs at least once.

Illustration drawn for QuestionBuddy.

Understand for, while and do-while Loops with Output Tracing and Loop Conversion

Parts of a loop. Initialisation (the starting value of the control variable), test condition (checked to decide whether to repeat), body (the statements repeated) and update (the change that moves towards ending).

for loop. for (initialisation; condition; update) body
Example: for (int i = 1; i <= 5; i++) sum = sum + i; Execution: initialise once, test, run the body, update, test again, and so on until the condition is false. A control variable declared inside the for header is not available after the loop.

while loop. Initialise before the loop; while (condition) { body including the update }. The condition is tested first.

do-while loop. Initialise before the loop; do { body including the update } while (condition); The condition is tested after the body, and the loop ends with a semicolon.

Entry controlled and exit controlled. for and while test first (entry controlled) and may never run the body. do-while tests last (exit controlled) and always runs the body at least once.

Counting iterations. For i = a, condition i <= b, step s, the body runs for a, a + s, a + 2s, ... as long as the value is within the limit. For example i = 2; i <= 10; i += 3 gives 2, 5, 8: three iterations.

Converting loops. Any loop can be written as another. for (int i = 1; i <= 4; i++) body becomes int i = 1; while (i <= 4) { body; i++; }

Variations. A for loop can have several counters (for (i = 1, j = 10; i < j; i++, j--)), an empty part (the initialisation or update can be written elsewhere, but the semicolons stay), or no condition at all, which makes an infinite loop. A delay loop (an empty loop that only counts) slows a program and has no body statements. A missing or wrong update (i-- when i should increase) causes an infinite loop.

Tracing method. Make a table of the control variable and the other variables for every pass of the body, then write the printed items in order.

Neighbouring topics: break, continue and digit-based programs are on the next page of this chapter.

for, while and do-while Loops with Output Tracing and Loop Conversion: key facts

Parts of a loop
  • Initialisation
  • test condition
  • body
  • update
All four are needed to avoid an infinite loop.
Entry versus exit
for and while: condition first, may run zero times | do-while: condition last, runs at least once
do-while ends with a semicolon.
for to while
for (a; b; c) body | becomes a; while (b) { body; c; }
Initialisation before the loop, update at the end.
Iterations
i from a, step s, while i <= b | values a, a + s, a + 2s, ...
Count the values that satisfy the condition.

How to answer for, while and do-while Loops with Output Tracing and Loop Conversion questions

Use this routine for loop output questions.

  1. 1Write the starting values of all variables.
  2. 2Test the condition; if it is false, stop (for an entry-controlled loop, the body is skipped).
  3. 3Run the body, noting the output and changed variables.
  4. 4Apply the update and test again.
  5. 5When the condition fails, write the final values and the printed output.

for, while and do-while Loops with Output Tracing and Loop Conversion in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Entry check versus exit check

Suppose a school gate guard checks the identity card before letting a student in, while a mess counter serves a plate first and asks for the coupon afterwards.

Which is entry controlled and which is exit controlled? What happens when the check fails the first time?

Try it first, then show the solutionSolution
  1. The gate check happens first: entry controlled; a student without a card never enters (zero iterations).
  2. The mess serves once before the coupon check: exit controlled (at least once).

Answer: The gate is like for and while; the mess is like do-while, which always runs once.

Memory hook: Check first or check last.

Tricks to remember for, while and do-while Loops with Output Tracing and Loop Conversion

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • do-while does once

    do-while runs the body at least once even if the condition is false at the start.

    Use it when: Entry versus exit control.

  • I-T-U

    Initialise, Test, Update.

    Use it when: Loop parts.

  • Semicolon at the end of do-while

    while (condition); closes a do-while loop.

    Use it when: Syntax.

for, while and do-while Loops with Output Tracing and Loop Conversion: solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: At least once · 1 mark · MCQBoard level

Which loop executes its body at least once even when the condition is false at the start?
(a) for (b) while (c) do-while (d) none

Try it first, then show the solutionSolution
  1. do-while tests the condition after the body.

Answer: (c) do-while

Example 2: Sum with a step · 3 marks · Short answerHarder board level

Find the output:

int s = 0;
for (int i = 2; i <= 10; i += 3)
s = s + i;
System.out.println(s);
Try it first, then show the solutionSolution
  1. The control variable takes 2, 5, 8; next is 11 which fails i <= 10. (1 mark)
  2. s = 0 + 2 = 2, then 7, then 15. (1 mark)
  3. The loop ends and 15 is printed. (1 mark)

Answer: 15

Example 3: while with multiplication · 3 marks · Short answerHarder board level

Find the output and the number of iterations:

int n = 2;
while (n < 50)
n = n * 3;
System.out.println(n);
Try it first, then show the solutionSolution
  1. n = 2, then 6, 18 and 54 after each pass. (1 mark)
  2. The condition n < 50 is true for 2, 6 and 18 (three iterations) and false for 54. (1 mark)
  3. Output: 54 after 3 iterations. (1 mark)

Answer: 54 (3 iterations)

Example 4: Case study: while versus do-while · 4 marks · Case-basedHOTS / competency

Two programs begin with int k = 10;
Program A:

do
{
System.out.print(k + " ");
k = k + 5;
}
while (k < 10);
System.out.println(k);

Program B:

while (k < 10)
{
System.out.print(k + " ");
k = k + 5;
}
System.out.println(k);

(i) Find the output of A. (2)
(ii) Find the output of B. (1)
(iii) Explain the difference. (1)

Try it first, then show the solutionSolution
  1. (i) The body runs once: it prints 10 and a space, then k = 15; 15 < 10 is false, so the loop ends. Then 15 is printed: 10 15.
  2. (ii) The condition 10 < 10 is false at once, so the body is skipped and only 10 is printed.
  3. (iii) do-while tests after the body (runs at least once), while tests before the body (may run zero times).

Answer: (i) 10 15 (ii) 10 (iii) do-while runs at least once; while may not run at all

Example 5: Condition inside a loop · 3 marks · Short answerHarder board level

Find the output:

for (int i = 1; i <= 4; i++)
{
if (i % 2 == 0)
System.out.print(i * 10 + " ");
else
System.out.print(i + " ");
}
Try it first, then show the solutionSolution
  1. i = 1 is odd, so 1 is printed; i = 2 is even, so 20 is printed. (1 mark)
  2. i = 3 prints 3; i = 4 prints 40. (1 mark)
  3. Output: 1 20 3 40 (with spaces). (1 mark)

Answer: 1 20 3 40

Where students lose marks in for, while and do-while Loops with Output Tracing and Loop Conversion

The slips examiners see most often, and what to write instead.

  • Mistake: Forgetting the update

    The loop never moves.

    Wrong working

    int i = 1; while (i <= 5) System.out.println(i);

    Right working

    add i++; inside the loop

    Do this: Check the update every time.

    Usually costs: 2 marks

  • Mistake: Missing semicolon after do-while

    Java reports a syntax (compile-time) error because the do-while statement is not terminated, so the program does not compile and the marks are lost.

    Wrong working

    while (k < 10)

    Right working

    while (k < 10);

    Do this: End a do-while with a semicolon.

    Usually costs: 1 mark

  • Mistake: Counting iterations wrongly

    The last value is mishandled.

    Wrong working

    i = 2; i <= 10; i += 3 runs 4 times

    Right working

    It runs 3 times: 2, 5, 8

    Do this: List the values.

    Usually costs: 1 mark

Exam tips

  • Always write a table of the control variable when tracing.
  • Say entry controlled or exit controlled when comparing loops.
  • When converting, keep initialisation before and update inside the new loop.
  • State clearly the final values of variables after the loop.

Practice questions on for, while and do-while Loops with Output Tracing and Loop Conversion

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    A menu must be shown at least once and repeated until the user chooses Exit. Which loop fits best?
    (a) for (b) while (c) do-while (d) none of these

    Hint

    The body must run first.

    Answer

    (c) do-while

  2. Q2 · 2 marks · Very short answerBoard level

    Write a for loop that prints the numbers 5, 4, 3, 2, 1 on separate lines.

    Hint

    Count down.

    Answer

    for (int i = 5; i >= 1; i--)
    System.out.println(i);

  3. Q3 · 3 marks · Short answerHarder board level

    Rewrite using a while loop and find s:

    int s = 0;
    for (int i = 1; i <= 4; i++)
    s += i * 2;
    Hint

    Initialise before, update at the end.

    Answer

    int s = 0;
    int i = 1;
    while (i <= 4)
    {
    s += i * 2;
    i++;
    }
    s = 2 + 4 + 6 + 8 = 20

  4. Q4 · 3 marks · Short answerHarder board level

    Find the output:

    int x = 1;
    while (x <= 3)
    {
    System.out.print(x + " " + x * x + " ");
    x++;
    }
    Hint

    Print x and its square in each pass.

    Answer

    1 1 2 4 3 9

  5. Q5 · 4 marks · Short answerHOTS / competency

    (i) How many times does the body run in for (int i = 0; i < 20; i += 5)? (ii) How many times in for (int i = 20; i > 0; i -= 6)? (iii) Why does for (int i = 1; i <= 5; i--) never end normally?

    Hint

    List the values of i.

    Answer

    (i) i = 0, 5, 10, 15: 4 times. (ii) i = 20, 14, 8, 2: 4 times. (iii) i decreases, so i <= 5 stays true; the loop is infinite (it ends only when the value overflows).

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.Name the four parts of a loop.Show answer

    Initialisation, test condition, body and update.

  2. 2.Which loop always runs once?Show answer

    do-while

  3. 3.Which loops are entry controlled?Show answer

    for and while

  4. 4.What ends a do-while statement?Show answer

    A semicolon.

  5. 5.What is an infinite loop?Show answer

    A loop whose condition never becomes false.

  6. 6.What is a delay loop?Show answer

    A loop without a body that only counts, to slow a program.

for, while and do-while Loops with Output Tracing and Loop Conversion: frequently asked questions

Can the for loop header be empty?

Yes, for (;;) has no parts and is an infinite loop, but the semicolons must stay.

Is a variable declared in the for header usable later?

No. It exists only inside the loop.

Which loop should I use when the number of repeats is known?

The for loop is the natural choice.