Skip to content
Computer ApplicationsICSE Class 10 · Iterative Constructs in Java

Loops in Java: Tracing, break, continue and Converting Between Loops (ICSE Class 10)

Updated 17 October 2026 · Fact-checked

for and while are entry controlled loops: the condition is tested before each pass, so the body may run zero times. do-while is an exit controlled loop: the body runs first and the condition is tested after it, so the body runs at least once. break leaves the loop at once; continue skips the rest of the current pass and goes on to the next test or update.

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

Illustration drawn for QuestionBuddy.

Understand Loop Tracing, break, continue and Loop Conversion

Parts of every loop: initialization, test condition, body and update. In for (i = 1; i <= 5; i++) all four are visible in the header and body.

Syntax

for (init; condition; update) { body }

init;
while (condition) { body; update; }

init;
do { body; update; } while (condition);

The do-while ends with a semicolon.

Entry vs exit control. for and while test first (entry controlled). do-while tests last (exit controlled), so it runs at least once even when the condition is false from the start.

Counting the iterations. The number of passes of for (i = a; i <= b; i += s) is the number of values a, a+s, a+2s ... that are not above b. For i = 2; i <= 10; i += 3 the values are 2, 5, 8: three passes.

Jump statements.

  • break: ends the loop (or switch) immediately; control goes to the statement after the loop.
  • continue: skips the remaining statements of this pass. In a for loop the update still runs; in a while or do-while the update must happen before continue, otherwise the loop may never move on.

Variations. Multiple counters: for (i = 1, j = 10; i < j; i++, j--). A finite loop ends; an infinite loop never ends, for example when the update is missing (while (i <= 5) with no i++) or for (;;) with no break. A delay loop has an empty body only to waste time: for (t = 1; t <= 1000000; t++); (note the semicolon).

Converting. Move the initialization before the loop, keep the condition in the header, and put the update at the end of the body. Converting to do-while keeps the same output only if the loop would have run at least once.

Digit extraction. To take the digits of n: d = n % 10 gives the last digit, n = n / 10 drops it, repeat while n > 0. The digits come out from right to left, so rev = rev * 10 + d builds the reversed number.

Loop Tracing, break, continue and Loop Conversion: key facts

Loop types
forentry controlled
whileentry controlled
do-whileexit controlled, runs at least once
do-while ends with a semicolon.
Iterations of a counting for loop
  • List the values a
  • a+s
  • a+2s
  • and so on
  • Count those that satisfy the condition
for (i = 2; i <= 10; i += 3) runs for 2, 5, 8: 3 times.
Jump statements
break: leave the loop | continue: skip to the next pass
In while loops update before continue.
Digit extraction
  • d = n % 10
  • n = n / 10
  • repeat while n > 0
  • rev = rev x 10 + d
Uses int division and remainder.

How to answer Loop Tracing, break, continue and Loop Conversion questions

Use this routine for loop output questions.

  1. 1Make a table with a column for every variable, including the loop variable.
  2. 2Check the condition before each pass (for and while) or after it (do-while).
  3. 3Execute the body statement by statement; at continue jump to the update/condition; at break stop and go past the loop.
  4. 4Apply the update, then test again; stop when the condition is false.
  5. 5Write the output after the loop with the final values of the variables.

Loop Tracing, break, continue 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.

Do-while as a login prompt

Suppose a school portal asks for a password at least once, and asks again only if the password is wrong.

Which loop fits, and why not a while loop?

Try it first, then show the solutionSolution
  1. The prompt must appear at least once before any check: exit controlled, do-while.
  2. A while loop would test the condition first, before any password exists.

Answer: do-while, because the body must run once before the test.

Memory hook: Ask first, check later.

Digit extraction

Imagine a code-checking program that adds the digits of a 4-digit ticket number.

For ticket 2475 what are the digit sum and the number of digits?

Try it first, then show the solutionSolution
  1. 5, 7, 4, 2 are taken from the right: 5 + 7 + 4 + 2 = 18.
  2. Four digits were extracted.

Answer: Sum 18 with 4 digits.

Memory hook: % 10 takes, / 10 drops.

Tricks to remember Loop Tracing, break, continue and Loop Conversion

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

  • Entry vs exit

    for and while check at the gate (entry); do-while checks as you leave (exit).

    Use it when: Classification questions.

  • Break = break up, continue = carry on

    break ends the loop; continue only ends the current pass.

    Use it when: Jump statement outputs.

  • Mod takes, divide drops

    n % 10 takes the last digit and n / 10 drops it.

    Use it when: Digit programs.

Loop Tracing, break, continue and Loop Conversion: solved examples

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

Example 1: Counting passes · 2 marks · Short answerBoard level

Write the output and say how many times the loop body runs:

for (int i = 2; i <= 10; i += 3)
    System.out.print(i + " ");
Try it first, then show the solutionSolution
  1. i takes the values 2, 5, 8; the next value 11 fails i <= 10. (1 mark)
  2. The body runs 3 times and prints 2 5 8. (1 mark)

Answer: 2 5 8 (3 iterations)

Example 2: continue and break in a while loop · 3 marks · Short answerHarder board level

Find the output:

int i = 0, s = 0;
while (i < 10) {
    i++;
    if (i % 3 == 0)
        continue;
    if (i > 7)
        break;
    s += i;
}
System.out.println(s + " " + i);
Try it first, then show the solutionSolution
  1. i = 1, 2 add to s (s = 3); i = 3 is skipped by continue. (1 mark)
  2. i = 4, 5 give s = 12; i = 6 skipped; i = 7 gives s = 19. (1 mark)
  3. i = 8: 8 % 3 is 2 so no continue, then i > 7 breaks the loop. Output s = 19 and i = 8. (1 mark)

Answer: 19 8

Example 3: while versus do-while · 3 marks · Short answerHarder board level

Find the output of both versions, starting with int n = 10;
(a)

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

(b)

while (n < 10) {
    System.out.print(n + " ");
    n += 5;
}
System.out.println(n);
Try it first, then show the solutionSolution
  1. (a) The body runs once: prints "10 " and n becomes 15; 15 < 10 is false, so the loop ends. println prints 15, so the line is 10 15. (1 mark)
  2. (b) The condition 10 < 10 is false at once, so the body never runs. (1 mark)
  3. Only println(n) executes and prints 10. This shows do-while runs at least once. (1 mark)

Answer: (a) 10 15 (b) 10

Example 4: Case study: digits of a number · 4 marks · Case-basedHOTS / competency

A program is run with n = 2475:

int n = 2475, s = 0, c = 0, rev = 0;
while (n > 0) {
    int d = n % 10;
    s += d;
    c++;
    rev = rev * 10 + d;
    n /= 10;
}
System.out.println(s + " " + c + " " + rev);

(i) Trace the values of d, s, c, rev and n. (2)
(ii) State the output. (1)
(iii) What would the output be if n started as 0? (1)

Try it first, then show the solutionSolution
  1. (i) Pass 1: d = 5, s = 5, c = 1, rev = 5, n = 247. Pass 2: d = 7, s = 12, c = 2, rev = 57, n = 24. Pass 3: d = 4, s = 16, c = 3, rev = 574, n = 2. Pass 4: d = 2, s = 18, c = 4, rev = 5742, n = 0.
  2. (ii) n = 0 ends the loop. Output 18 4 5742.
  3. (iii) n > 0 is false at once, so nothing runs and the output is 0 0 0.

Answer: (ii) 18 4 5742 (iii) 0 0 0

Example 5: Infinite or finite · 3 marks · Short answerHarder board level

State whether each loop is finite or infinite and correct the infinite ones:
(i) int i = 1; while (i <= 5) System.out.println(i);
(ii) for (int i = 10; i > 0; i--) { }
(iii) for (;;) { System.out.println("x"); }

Try it first, then show the solutionSolution
  1. (i) Infinite: i never changes, so it prints 1 for ever. Correct by adding i++ inside the loop (use braces). (1 mark)
  2. (ii) Finite: i falls from 10 to 1 and then 0 fails the test. (1 mark)
  3. (iii) Infinite: no condition and no break. Add a break inside an if, or a condition such as for (int i = 1; i <= 5; i++). (1 mark)

Answer: (i) infinite, add i++ (ii) finite (iii) infinite, add a condition or break

Where students lose marks in Loop Tracing, break, continue and Loop Conversion

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

  • Mistake: Forgetting the semicolon after while in do-while

    The do-while statement ends with a semicolon after its condition.

    Wrong working

    do { ... } while (i < 5)

    Right working

    do { ... } while (i < 5);

    Do this: Add the semicolon.

    Usually costs: 1 mark

  • Mistake: Putting a semicolon right after the for header

    The loop body becomes empty and the next block runs once.

    Wrong working

    for (i = 1; i <= 3; i++); { System.out.print(i); }

    Right working

    for (i = 1; i <= 3; i++) { System.out.print(i); }

    Do this: No semicolon after for (...) when it has a body.

    Usually costs: 1 mark

  • Mistake: Placing continue before the update in a while loop

    The update is skipped, so the loop repeats the same value for ever.

    Wrong working

    while (i < 5) { if (i == 2) continue; i++; }

    Right working

    while (i < 5) { i++; if (i == 2) continue; }

    Do this: Update first or use for.

    Usually costs: 1 to 2 marks

Exam tips

  • Always make a trace table; the final value of the loop variable after the loop is a favourite question.
  • State "entry controlled" or "exit controlled" with the reason when comparing loops.
  • When converting, copy the initialization and update carefully; mention the semicolon rule of do-while.
  • For digit programs, write all four steps: take, add or build, drop, repeat.

Practice questions on Loop Tracing, break, continue 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

    Which of these is an exit controlled loop?
    (a) for (b) while (c) do-while (d) none of these

    Hint

    The test comes after the body.

    Answer

    (c) do-while

  2. Q2 · 1 mark · Very short answerBoard level

    How many times does the body run?
    for (int i = 1; i <= 10; i += 2) { ... }

    Hint

    List the values of i.

    Answer

    5 times (i = 1, 3, 5, 7, 9).

  3. Q3 · 2 marks · Short answerHarder board level

    Write the output of each loop:

    int s = 0;
    for (int i = 1; i <= 4; i++) { if (i == 3) continue; s += i; }
    System.out.println(s);
    s = 0;
    for (int i = 1; i <= 4; i++) { if (i == 3) break; s += i; }
    System.out.println(s);
    Hint

    continue skips one pass; break stops all.

    Answer

    7
    3

  4. Q4 · 3 marks · Short answerHarder board level

    Convert to a for loop and give the output:

    int i = 10;
    while (i > 0) {
        System.out.print(i + " ");
        i -= 3;
    }
    Hint

    Initialization and update move into the header.

    Answer

    for (int i = 10; i > 0; i -= 3)
    System.out.print(i + " ");
    Output: 10 7 4 1

  5. Q5 · 1 mark · Assertion-reasonHOTS / competency

    Assertion (A): In a for loop, the update expression is skipped when continue is executed.
    Reason (R): continue skips the remaining statements of the current pass of the loop.
    (a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.

    Hint

    Where does control go after continue in a for loop?

    Answer

    (d) A is false: in a for loop, continue jumps to the update expression and then the test. R is true.

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.Which loop runs at least once?Show answer

    do-while.

  2. 2.What does break do?Show answer

    Ends the loop immediately.

  3. 3.What does continue do?Show answer

    Skips the rest of the current pass.

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

    A loop with an empty body used only to waste time.

  5. 5.What does n % 10 give?Show answer

    The last digit of n.

  6. 6.What makes a loop infinite?Show answer

    A condition that never becomes false, for example a missing update.

Loop Tracing, break, continue and Loop Conversion: frequently asked questions

What is the difference between while and do-while?

while tests the condition before the body (entry controlled); do-while tests after it (exit controlled) and so runs at least once.

Can I use break in a for loop?

Yes. It ends the loop at once, and it works the same way in while, do-while and switch.

Does continue run the for update?

Yes; in a for loop the update expression runs before the next test.