Iterative Statements in Java ICSE Class 9: Loops, break and continue
Updated 15 October 2026 · Fact-checked
Iterative (looping) statements repeat a block of statements while a condition is true. ICSE Class 9 tests the entry-controlled for and while loops, the exit-controlled do-while loop, their syntax and inter-conversion, finite, infinite and delay loops, multiple counter variables, and the jump statements break and continue.
What Iterative Statements in Java is about
Computers are valuable because they repeat work tirelessly. Printing a table, adding 1,000 numbers, checking every digit of a number or every possible factor all need loops. Without loops you would have to write the same statement again and again.
The CISCE syllabus asks for: looping statements and their types; entry-controlled loops (for, while) and the exit-controlled loop (do-while); variations in looping statements; jump statements; syntax of entry- and exit-controlled loops; break and continue; simple programs using all three loops; inter-conversion between for, while and do-while; finite and infinite loops; delay loops; and multiple counter variables (initialisations and updations). The need for loops should be shown by examples.
In output questions, make a trace table with a column for each variable and the condition, and count iterations carefully; most errors are off-by-one.
Weightage: CISCE publishes no chapter weightage and no question-paper layout for ICSE Class 9 Computer Applications. Class IX is examined by your school: one 100-mark theory paper of 2 hours, plus 100 marks of internal assessment (at least 15 programming assignments and a written project on ethical computing). This is topic 9 of the 2028 syllabus.Source: CISCE ICSE 2028 Computer Applications syllabus, Class IX
Key concepts
- Why loops and the parts of a loop
A loop repeats a set of statements (the body) as long as a test condition is true. Each repetition is an iteration.
Why loops are needed: to print the multiplication table of 7 without loops needs 10 println statements; with a loop it needs one statement inside a for. For 1,000 values the saving is enormous, and the number of repetitions can be decided at run time (for example, a number entered by the user).
Every loop has four parts:
1. Initialisation: gives the loop control variable its starting value (int i = 1).
2. Test condition: decides whether to continue (i <= 10).
3. Body: the statements repeated.
4. Update (increment/decrement): changes the control variable so that the condition eventually becomes false (i++).Types:
- Entry-controlled (pre-tested) loops test the condition before the body: for and while. If the condition is false at the start, the body runs zero times.
- Exit-controlled (post-tested) loop tests after the body: do-while. The body always runs at least once.
Worked example
(2 marks) Distinguish between entry-controlled and exit-controlled loops with an example of each.
Try it first, then show the solutionSolution
- Entry-controlled: condition checked before the body; may run zero times; for, while - 1 mark.
- Exit-controlled: condition checked after the body; runs at least once; do-while - 1 mark.
Answer: An entry-controlled loop (for, while) tests its condition before executing the body, so the body may not run at all. An exit-controlled loop (do-while) tests after the body, so the body runs at least once.
- The for loop
Syntax:
for (initialisation; test condition; update)
{
body;
}
Order of execution: initialisation once → test → body → update → test → body → update ... until the test is false.Use for when the number of iterations is known in advance.
Examples (as run in this study guide):
- Factorial: long f = 1; for (int i = 1; i <= 6; i++) f = f * i; gives 6! = 720.
- Counting iterations: for (int i = 10; i > 0; i -= 3) prints 10 7 4 1: 4 times.
- Sum of even numbers 2 to 20: for (int i = 2; i <= 20; i += 2) es += i; gives 110.
- Series: double s = 0; for (int i = 1; i <= 5; i++) s = s + 1.0 / i; gives s = 2.283333333333333 (1.0 / i avoids integer division).
The control variable after the loop: int p; for (p = 1; p <= 5; p++); leaves p = 6, the first value that failed the test (the semicolon makes the body empty).
Worked example
(2 marks) How many times does this loop run, and what does it print? for (int i = 10; i > 0; i -= 3) System.out.print(i + " ");
Try it first, then show the solutionSolution
- i takes 10, 7, 4, 1; next value −2 fails i > 0 - 1 mark.
- Runs 4 times; prints 10 7 4 1 - 1 mark.
Answer: 4 times; output: 10 7 4 1
- The while and do-while loops
while loop (entry-controlled):
initialisation;
while (condition)
{
body;
update;
}
Use while when the number of iterations is not known in advance, for example processing the digits of a number until it becomes 0.do-while loop (exit-controlled):
initialisation;
do
{
body;
update;
} while (condition); // note the semicolon
Use do-while when the body must run at least once, for example showing a menu before asking whether to continue.Difference shown by code (as run in this study guide):
int x = 10; do { System.out.println("x = " + x); x++; } while (x < 5); prints x = 10 once.
int y = 10; while (y < 5) { System.out.println("y = " + y); y++; } prints nothing; y stays 10.Digit processing pattern (while):
while (copy > 0)
{
int d = copy % 10; // last digit
sum = sum + d;
rev = rev * 10 + d;
copy = copy / 10; // remove last digit
}
For 4729: digits 9, 2, 7, 4; sum = 22; reverse = 9274.Worked example
(2 marks) State two differences between while and do-while.
Try it first, then show the solutionSolution
- while tests before the body (entry-controlled); do-while tests after (exit-controlled) - 1 mark.
- while may run zero times; do-while runs at least once; do-while ends with a semicolon after while(condition) - 1 mark.
Answer: (1) while checks the condition before the body, whereas do-while checks it after the body. (2) A while loop may not execute at all, but a do-while loop executes at least once (and its syntax ends with while (condition);).
- Inter-conversion of loops
Any loop can be rewritten as another. Move the parts carefully.
Printing 1 to 5 (all three as run in this study guide, each printing 1 2 3 4 5):
for:
for (int i = 1; i <= 5; i++)
System.out.print(i + " ");while:
int i = 1;
while (i <= 5)
{
System.out.print(i + " ");
i++;
}do-while:
int i = 1;
do
{
System.out.print(i + " ");
i++;
} while (i <= 5);Steps for conversion from for: put the initialisation before the loop; put the condition in while( ); put the update as the last statement of the body.
Caution: converting to do-while changes behaviour if the condition is false at the start (do-while still runs once). Conversion is exact only when the first test is true.
Worked example
(2 marks) Convert to a while loop: for (int k = 20; k >= 5; k -= 5) System.out.println(k);
Try it first, then show the solutionSolution
- Initialisation before the loop: int k = 20; condition in while: while (k >= 5) - 1 mark.
- Body then update inside braces: { System.out.println(k); k -= 5; } - 1 mark.
Answer: int k = 20;
while (k >= 5)
{
System.out.println(k);
k -= 5;
}- Variations: multiple counters, infinite and delay loops
Multiple counter variables: a for loop can initialise and update several variables, separated by commas.
for (int i = 1, j = 10; i < j; i += 2, j -= 2)
System.out.print(i + "," + j + " ");
As run: 1,10 3,8 5,6 (stops when i = 7, j = 4).Finite loop: ends after a fixed number of iterations.
Infinite loop: never ends on its own because the condition never becomes false.- for (;;) { } , while (true) { } , or a forgotten update such as int i = 1; while (i <= 5) System.out.println(i);
- Infinite loops are usually errors, but are useful with break: int k = 1; for (;;) { k = k * 3; if (k > 100) break; } gives k = 243 (as run).
Delay loop: a loop with an empty body, used to create a pause by making the computer count:
for (long t = 1; t <= 100000000L; t++);
The semicolon after the header means the body is empty. The length of the delay depends on the speed of the computer, so it is not precise.Empty-body trap: for (p = 1; p <= 5; p++); System.out.println(p); prints 6 once, because the semicolon ended the loop.
Worked example
(2 marks) Give the output: int k = 1; for (;;) { k = k * 3; if (k > 100) break; } System.out.println("k = " + k);
Try it first, then show the solutionSolution
- k becomes 3, 9, 27, 81, 243 - 1 mark.
- 243 > 100, so break exits the infinite loop: k = 243 - 1 mark.
Answer: k = 243
- Jump statements: break and continue
break: ends the loop immediately; control passes to the first statement after the loop. (It also ends a switch.)
continue: skips the rest of the current iteration and goes on to the next iteration (in a for loop, to the update; in while and do-while, to the condition).As run in this study guide:
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0) continue;
if (i == 8) break;
System.out.print(i + " ");
}
Output: 1 2 4 5 7
(3 and 6 are skipped by continue; at 8 the loop breaks.)Caution with continue in a while loop: if the update comes after continue, it may be skipped and the loop may never end. As run:
int m = 1;
while (m <= 20)
{
m = m * 2;
if (m == 8) continue;
System.out.print(m + " ");
}
Output: 2 4 16 32 (8 is skipped; the update is before continue, so the loop is safe).Use of loops for checking: prime test by counting factors: for (int i = 1; i <= n; i++) if (n % i == 0) factors++; n is prime if factors == 2. For 29: prime (as run).
Worked example
(2 marks) Distinguish between break and continue.
Try it first, then show the solutionSolution
- break terminates the loop and control moves to the statement after it - 1 mark.
- continue skips the remaining statements of the current iteration and starts the next iteration - 1 mark.
Answer: break ends the loop completely and passes control to the statement after the loop; continue skips only the rest of the current iteration and moves on to the next iteration of the same loop.
Iterative Statements in Java in real life
Each concept, met in an everyday situation. Read the situation, answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
do-while: at least once
Suppose an ATM shows its menu, lets you do one transaction, then asks 'Another transaction? (Y/N)'.
Which loop fits and why?
Try it first, then show the solutionSolution
- The menu must appear at least once before any question is asked.
- The check comes after the transaction.
- This is an exit-controlled loop: do-while.
Answer: do-while, because the body (menu and transaction) must run at least once and the condition (Y/N) is checked afterwards.
Memory hook: Do first, ask later.
while: unknown count
Imagine counting how many times you can halve a 1,000-rupee note's worth in coins until it is less than 1 rupee.
Which loop suits a task where the number of repetitions is not known in advance?
Try it first, then show the solutionSolution
- We repeat 'halve the amount' while amount >= 1.
- We do not know beforehand how many halvings are needed.
- A while loop with a counter fits.
Answer: A while loop: while (amt >= 1) { amt = amt / 2; count++; } The loop decides the number of iterations from the data.
Memory hook: Known count: for; unknown count: while.
break and continue
Suppose you check exam answer sheets in order: you skip any sheet marked 'absent' and stop completely when the bell rings.
Which jump statement models each action?
Try it first, then show the solutionSolution
- Skipping one sheet and moving to the next: continue.
- Stopping the whole job: break.
Answer: Skipping an 'absent' sheet is continue; stopping when the bell rings is break.
Memory hook: continue skips one; break stops all.
Topics in this chapter
Iterative Statements in Java: syntax and key facts
- for
- for (init; condition; update) { body }
- Entry-controlled; known count.
- while / do-while
- while (condition) { body; update; } do { body; update; } while (condition);
- do-while runs at least once.
- Digit loop
- while (n > 0) { d = n % 10; ...; n = n / 10; }
- Reverse: rev = rev * 10 + d.
- Jump statements
- break: leave loop; continue: next iteration
- Both act on the innermost loop.
- Infinite / delay
- for (;;), while (true); delay: for (long t = 1; t <= N; t++);
- Empty body = delay.
How the school exam asks Iterative Statements in Java
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| MCQ and assertion-reason (1 mark) | Which loop runs at least once; what break/continue does; number of iterations. | do-while; break leaves, continue skips; count with a trace. |
| Output (2 marks) | Trace loops with break, continue, multiple counters, empty bodies. | Trace table: variable values, condition, output per iteration. |
| Conversion (2 marks) | Convert a for loop to while or do-while and vice versa. | Init before, condition in while, update at the end of the body. |
| Program (15 marks) | Digits (sum, reverse, palindrome, Armstrong), factorial, Fibonacci, prime, series sums. | Choose the loop; initialise accumulators; test with known values. |
Tricks to remember Iterative Statements in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
I-T-B-U
Initialise, Test, Body, Update: the four parts of every loop.
Use it when: Writing and converting loops.
do-while = do then check
The 'do' comes first, so the body runs before the check.
Use it when: Exit control.
%10 and /10
% 10 picks the last digit; / 10 drops it.
Use it when: Digit programs.
Iterative Statements in Java: exam-level solved questions
Ordered from exam level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: Exit-controlled loop · 1 mark · MCQExam level
Which loop is exit-controlled?
(a) for
(b) while
(c) do-while
(d) none
Try it first, then show the solutionSolution
- for and while test before the body.
- do-while tests after.
Answer: (c) do-while
Example 2: Assertion and reason: do-while · 1 mark · Assertion-reasonHarder exam level
Assertion (A): int x = 10; do { System.out.println(x); x++; } while (x < 5); prints 10.
Reason (R): A do-while loop executes its body before testing the condition.
(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
Try it first, then show the solutionSolution
- The body runs once, printing 10, then 11 < 5 is false.
- R explains A.
Answer: (a) Both A and R are true and R is the correct explanation of A
Example 3: break and continue trace · 2 marks · Short answerHarder exam level
Give the output:
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0) continue;
if (i == 8) break;
System.out.print(i + " ");
}
Try it first, then show the solutionSolution
- i = 3 and 6 are skipped by continue.
- At i = 8 the loop breaks before printing; printed: 1 2 4 5 7.
Answer: 1 2 4 5 7
Example 4: Multiple counters · 2 marks · Very short answerHarder exam level
Give the output: for (int i = 1, j = 10; i < j; i += 2, j -= 2) System.out.print(i + "," + j + " ");
Try it first, then show the solutionSolution
- Pairs: (1, 10), (3, 8), (5, 6).
- Next (7, 4): 7 < 4 is false; stop.
Answer: 1,10 3,8 5,6
Example 5: Post-decrement in the condition · 2 marks · Short answerHOTS / competency
Give the output: int z = 5; while (z-- > 0) System.out.print(z + " ");
Try it first, then show the solutionSolution
- z-- compares the old value with 0, then decreases z: 5 > 0 true, z = 4, print 4.
- Continues: 3, 2, 1, 0; when the old value is 0, 0 > 0 is false and the loop ends.
Answer: 4 3 2 1 0
Example 6: Fibonacci · 2 marks · Short answerExam level
Write a loop to print the first 10 Fibonacci terms starting 0 1, and give the output.
Try it first, then show the solutionSolution
- int a = 0, b = 1; for (int i = 1; i <= 10; i++) { System.out.print(a + " "); int c = a + b; a = b; b = c; }
- Each term is the sum of the previous two.
Answer: 0 1 1 2 3 5 8 13 21 34
Example 7: Digits program · 15 marks · Long answerHarder exam level
Write a program to accept a positive integer and print the number of digits, the sum of the digits, the reverse, and whether it is a palindrome (same as its reverse).
Try it first, then show the solutionSolution
- import java.util.*;
class Digits
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = sc.nextInt();
int copy = n, sum = 0, rev = 0, count = 0; - while (copy > 0)
{
int d = copy % 10; // last digit
sum = sum + d; // accumulator
rev = rev * 10 + d; // build the reverse
count++; // counter
copy = copy / 10; // remove the last digit
} - System.out.println("Digits : " + count);
System.out.println("Sum : " + sum);
System.out.println("Reverse : " + rev);
if (rev == n)
System.out.println("Palindrome : yes");
else
System.out.println("Palindrome : no");
}
} - A copy is used so that n is still available for the palindrome check. Trace 4729: d = 9, 2, 7, 4; sum = 22; rev = 9, 92, 927, 9274.
Answer: Sample runs (as run):
Enter a positive number: 4729
Digits : 4
Sum : 22
Reverse : 9274
Palindrome : no
Enter a positive number: 1221
Digits : 4
Sum : 6
Reverse : 1221
Palindrome : yes
Case-study practice (Section B style)
Read the situation, then answer every part. In a school exam set on the board pattern this is a 7-mark question split into 2 + 3 + 2 marks.
A loop that never ends
Imagine Kunal writes a program to print the numbers 1 to 5:
int i = 1;
while (i <= 5)
System.out.println(i);
i++;
The program keeps printing 1 for ever. He then tries:
int p;
for (p = 1; p <= 5; p++);
System.out.println(p);
which prints only 6.
(i) · 2 marks
Why does the first loop never end? Correct it.
Answer
Answer: Without braces, only the println is the body; i++ is outside the loop, so i stays 1 and i <= 5 is always true (an infinite loop). Correct: while (i <= 5) { System.out.println(i); i++; }
1 mark reason, 1 mark correction.
(ii) · 3 marks
Explain why the for loop prints only 6.
Answer
Answer: The semicolon after the for header makes the body empty, so the loop just counts p from 1 to 6. When p becomes 6, p <= 5 fails and the loop ends; then the println runs once and prints 6.
1 mark empty body, 1 mark p reaches 6, 1 mark single print.
(iii) · 2 marks
Name the kind of loop the second one is, and give one legitimate use of it.
Answer
Answer: A loop with an empty body, used as a delay loop to create a pause, for example for (long t = 1; t <= 100000000L; t++);
1 mark name, 1 mark use.
Where the marks are
How a typical answer is split into step marks. Show every step: a right final answer alone rarely earns full marks.
Give the output of a loop with continue and break. (2 marks) (2 marks)
| Step | Marks |
|---|---|
| Correct values skipped by continue. | 1 |
| Correct stopping point with break; complete output. | 1 |
Program: digits count, sum, reverse and palindrome. (15 marks) (15 marks)
| Step | Marks |
|---|---|
| Input with Scanner; copy of n kept. | 2 |
| while (copy > 0) with d = copy % 10 and copy = copy / 10. | 4 |
| sum, count and rev = rev * 10 + d inside the loop. | 4 |
| Palindrome check with if-else. | 3 |
| Labelled output. | 2 |
Where students lose marks in Iterative Statements in Java
The slips examiners see most often, and what to write instead.
Mistake: Off-by-one iteration counts
< and <= confused.
Wrong working
for (i = 1; i < 10; i++) to print 1 to 10
Right working
for (i = 1; i <= 10; i++)
Do this: Check the first and last values in a trace.
Usually costs: 1 to 2 marks.
Mistake: Losing the original number
Dividing n itself in a digit loop.
Do this: Work on a copy so that n is available after the loop.
Usually costs: 2 to 3 marks.
Mistake: Semicolon after the loop header
Habit from statements.
Wrong working
for (i = 1; i <= 5; i++); System.out.println(i);
Right working
for (i = 1; i <= 5; i++) System.out.println(i);
Do this: No semicolon after for (...) or while (...) unless an empty body is intended.
Usually costs: 1 to 2 marks.
Mistake: Missing semicolon after do-while
Other loops do not need it.
Do this: do { ... } while (condition);
Usually costs: 1 mark.
Mistake: Accumulator not initialised
Declared without a value.
Do this: Start sums at 0 and products (factorial) at 1.
Usually costs: 1 mark.
Practice questions on Iterative Statements in Java
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQExam level
Which statement skips the rest of the current iteration?
(a) break
(b) continue
(c) exit
(d) returnHint
Next iteration.
Answer
(b) continue
Q2 · 1 mark · MCQExam level
How many times does this loop execute? for (int i = 0; i < 20; i += 4)
(a) 4
(b) 5
(c) 6
(d) 20Hint
0, 4, 8, ...
Answer
(b) 5 (i = 0, 4, 8, 12, 16)
Q3 · 2 marks · Very short answerHarder exam level
Write a for loop to find the sum 1 + 1/2 + 1/3 + 1/4 + 1/5. What is the result?
Hint
1.0 / i.
Answer
double s = 0; for (int i = 1; i <= 5; i++) s = s + 1.0 / i; Result (as run): 2.283333333333333
Q4 · 2 marks · Very short answerExam level
Convert into a do-while loop: int s = 0; for (int i = 2; i <= 20; i += 2) s += i;
Hint
Init, body, update, condition.
Answer
int s = 0, i = 2;
do
{
s += i;
i += 2;
} while (i <= 20);
(s = 110)Q5 · 2 marks · Short answerHOTS / competency
Give the output:
int m = 1;
while (m <= 20)
{
m = m * 2;
if (m == 8) continue;
System.out.print(m + " ");
}Hint
m is doubled before the test.
Answer
2 4 16 32
Q6 · 2 marks · Very short answerHarder exam level
What is a delay loop? Why is its delay not exact?
Hint
Empty body.
Answer
A loop with an empty body that only counts, used to pause a program (for (long t = 1; t <= N; t++);). Its delay depends on the speed of the computer, so the same loop takes different times on different machines.
Q7 · 15 marks · Long answerHarder exam level
Write a program to check whether a number is an Armstrong number (equal to the sum of the cubes of its digits, for a three-digit number). Test with 153.
Hint
Digit loop with d * d * d.
Answer
import java.util.*;
class Armstrong
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a three-digit number: ");
int num = sc.nextInt();
int t = num, arm = 0;
while (t > 0)
{
int d = t % 10;
arm += d * d * d;
t /= 10;
}
if (arm == num)
System.out.println(num + " is Armstrong");
else
System.out.println(num + " is not Armstrong");
}
}
Sample run (as run, input 153): 153 is Armstrong (1 + 125 + 27 = 153).Q8 · 2 marks · Very short answerHarder exam level
Write a loop to count the factors of n and decide whether n is prime. What is printed for n = 29?
Hint
Prime has exactly two factors.
Answer
int factors = 0; for (int i = 1; i <= n; i++) if (n % i == 0) factors++; System.out.println(n + (factors == 2 ? " is prime" : " is not prime")); Output: 29 is prime
Q9 · 3 marks · Short answerHOTS / competency
The prime-checking loop above tests every number up to n. Suggest two ways to make it faster and explain why each is correct.
Hint
break; stop early.
Answer
(1) Test divisors only from 2 to n / 2 (no factor other than n itself is larger than n / 2), and decide prime if none divides n. (2) Use break as soon as a divisor is found, since one divisor is enough to show n is not prime. Both give the same answer with fewer iterations.
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.Which loops are entry-controlled?Show answer
for and while.
2.Minimum number of times a do-while body runs?Show answer
Once.
3.Value of 6! ?Show answer
720
4.Write an infinite for loop.Show answer
for (;;) { }
5.What does continue do in a for loop?Show answer
Skips to the update and the next test.
6.Expression to remove the last digit of n?Show answer
n = n / 10
Best order to study it
- Learn why loops are needed and the four parts of a loop.
- Learn for, while and do-while syntax and the entry/exit difference.
- Practise inter-conversion of loops.
- Learn variations: multiple counters, infinite loops, delay loops, empty bodies.
- Learn break and continue; write digit, factorial, Fibonacci, prime and series programs.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- Loop parts: initialise, test, body, update.
- Entry-controlled: for, while (may run 0 times); exit-controlled: do-while (at least once).
- for when count is known; while when it is not; do-while when the body must run first.
- Multiple counters: for (i = 1, j = 10; i < j; i++, j--).
- Infinite: for (;;), while (true); delay: empty-body loop.
- break leaves the loop; continue goes to the next iteration.
Iterative Statements in Java: frequently asked questions
Which loop should I use in a program?
Use for when the number of repetitions is known (tables, series), while when it depends on data (digits of a number), and do-while when the body must run at least once (menus).
Is an infinite loop always an error?
Usually it is, but loops such as for (;;) with a break inside are deliberate. A loop that never ends by mistake is a logical error.
Can the loop variable be used after a for loop?
Only if it was declared before the loop (int p; for (p = 1; ...)). If declared in the header (for (int i = 1; ...)), it does not exist after the loop.
Based on: CISCE ICSE 2028 Computer Applications (86) syllabus (Classes IX and X); CISCE Regulations for ICSE Examination Year 2028