Iterative Constructs in Java ICSE Class 10 Computer Applications
Updated 14 October 2026 · Fact-checked
Iterative constructs (loops) repeat statements. for and while are entry-controlled loops that test the condition before each pass, so they may run zero times; do-while is exit-controlled and always runs at least once. break leaves a loop and continue skips to the next pass. ICSE Class 10 tests loop outputs, conversions and digit-based programs.
What Iterative Constructs in Java is about
Loops are the engine of almost every Section B program: digit programs (EvenPal 2024, largest and smallest digit 2025), series, patterns and array processing. Section A regularly asks for loop conversion (do-while to for in 2024; for to an exit-controlled loop in 2025), output with the number of iterations (2024, 2025), and spotting infinite loops and the effect of a semicolon after for (2025).
The syllabus asks for: types of loops, entry-controlled (for, while) and exit-controlled (do-while) loops, variations of loops, jump statements break and continue, inter-conversion between for, while and do-while, finite and infinite loops, delay loops, and multiple counter variables.
Weightage: CISCE publishes no chapter weightage. Section A (40 marks) is compulsory: 1-mark MCQs and 2-mark short answers including output and error questions; in Section B you answer any four of six 15-mark programming questions written in Java.Source: CISCE ICSE Computer Applications (661) specimen paper structure for 2027
Key concepts
- for, while and do-while
Every loop has four parts: initialisation, condition (test), body and update.
for loop (entry controlled; best when the number of passes is known):
for (int i = 1; i <= 5; i++)
System.out.print(i + " ");
Output: 1 2 3 4 5while loop (entry controlled; best when passes depend on a condition, such as digits of a number):
int n = 1234, s = 0;
while (n > 0)
{
s = s * 10 + n % 10;
n = n / 10;
}
After the loop s = 4321 (the reverse).do-while loop (exit controlled; body runs at least once; note the semicolon after while):
int k = 10;
do
{
System.out.println(k);
k++;
} while (k < 5);
Output: 10 (once), although the condition is false at the start.Worked example
(2 marks) Distinguish between entry-controlled and exit-controlled loops.
Try it first, then show the solutionSolution
- Entry controlled (for, while): condition tested before the body; may run zero times - 1 mark.
- Exit controlled (do-while): condition tested after the body; runs at least once - 1 mark.
Answer: In an entry-controlled loop (for, while) the condition is checked before the body, so the body may not run at all; in an exit-controlled loop (do-while) the condition is checked after the body, so it runs at least once.
- Counting iterations and output
To trace, make a table of the loop variable, the condition and what is printed.
Without braces only the next statement is the loop body:
for (x = 10; x > 20; x++)
System.out.println(x);
System.out.println(x * 2);
The condition 10 > 20 is false at once: the loop runs 0 times; only the second println runs, printing 20.A semicolon right after for(...) makes an empty body:
for (p = 4; p > 0; p -= 2);
System.out.print(p);
System.out.println(p);
The loop changes p: 4, 2, 0 and stops; then 0 is printed twice on one line: 00.Worked example
(2 marks) Give the output and the number of iterations:
int i;
for (i = 1; i <= 20; i += 4)
System.out.print(i + " ");Try it first, then show the solutionSolution
- i takes 1, 5, 9, 13, 17; next 21 fails - 1 mark.
- 5 iterations; output on one line - 1 mark.
Answer: 1 5 9 13 17 (5 iterations)
- break and continue
break ends the loop at once; control goes to the statement after the loop.
continue skips the rest of the current pass and goes to the next pass (in a for loop, the update runs next).for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0) continue;
if (i == 8) break;
System.out.print(i + " ");
}
Trace: 1 printed, 2 printed, 3 skipped, 4, 5 printed, 6 skipped, 7 printed, 8 breaks.
Output: 1 2 4 5 7Worked example
(2 marks) Give the output:
int s = 0;
for (int k = 1; k <= 6; k++)
{
if (k == 4) continue;
s += k;
}
System.out.println(s);Try it first, then show the solutionSolution
- k = 4 is skipped; s = 1 + 2 + 3 + 5 + 6 - 1 mark.
- s = 17 - 1 mark.
Answer: 17
- Loop conversion
To convert, keep the same initialisation, condition and update, and move them into the new form.
for → while:
for (i = 1; i <= 5; i++) sum += i;
becomes
i = 1;
while (i <= 5)
{
sum += i;
i++;
}for → do-while (exit controlled):
int a, b;
for (a = 10, b = 1; a >= 1; a -= 2) { b += a; b++; }
becomes
a = 10; b = 1;
do
{
b += a;
b++;
a -= 2;
} while (a >= 1);
Both give b = 36 (b: 12, 21, 28, 33, 36 for a = 10, 8, 6, 4, 2).do-while → for: a do-while always runs once, so the converted for gives the same result only if the condition is true at the start. Multiple variables go in the header separated by commas: for (x = 10, y = 20; x <= 20; x++, y++);
Worked example
(2 marks) Rewrite using for and give the output:
x = 10; y = 20;
do
{
x++;
y++;
} while (x <= 20);
System.out.println(x * y);Try it first, then show the solutionSolution
- for (x = 10, y = 20; x <= 20; x++, y++); (empty body; the condition is true at the start, so results match) - 1 mark.
- Loop ends at x = 21, y = 31; output 21 × 31 = 651 - 1 mark.
Answer: for (x = 10, y = 20; x <= 20; x++, y++);
System.out.println(x * y);
Output: 651- Finite, infinite and delay loops; digit extraction
A finite loop ends after a fixed number of passes. An infinite loop never ends because its condition never becomes false: for (int i = 2; i != 0; i -= 3) (i = 2, -1, -4, ... never 0); while (true) { } without break; for ( ; ; ).
A delay loop has an empty body and only wastes time: for (long t = 1; t <= 100000; t++);
Digit extraction pattern (used in most number programs):
int copy = n; // keep the original
while (n > 0)
{
int d = n % 10; // last digit
... use d ...
n = n / 10; // remove last digit
}
With it you can find the sum, product, count, largest or smallest digit, and the reverse (rev = rev * 10 + d).Worked example
(1 mark) Which is an infinite loop?
(a) for (int i = 1; i <= 10; i++)
(b) for (int i = 2; i != 0; i -= 3)
(c) for (int i = 5; i <= 5; i++)
(d) for (int i = 1; i >= 1; i--)Try it first, then show the solutionSolution
- (b): i = 2, -1, -4, ... skips 0, so i != 0 is always true.
Answer: (b) for (int i = 2; i != 0; i -= 3)
Iterative Constructs 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.
for loop with known repetitions
Suppose a PE teacher asks students to do a side stretch 10 times.
Which loop fits, and how would you write it?
Try it first, then show the solutionSolution
- The number of repetitions is known: 10.
- A for loop suits a known count.
- for (int i = 1; i <= 10; i++) stretch();
Answer: A for loop: for (int i = 1; i <= 10; i++) { stretch(); }
Memory hook: Known count, use for.
do-while runs at least once
Imagine an ATM menu that shows options, performs the chosen one and then asks 'Another transaction? (y/n)'.
Why is do-while the natural choice?
Try it first, then show the solutionSolution
- The menu must appear at least once before asking.
- The repeat condition is checked after the transaction.
- That is exit-controlled behaviour.
Answer: do-while, because the menu must run at least once and the decision to repeat comes at the end.
Memory hook: Do first, ask after.
while with unknown repetitions
Suppose a piggy bank program keeps adding coins until the total reaches ₹500, and coins vary in value.
Which loop suits it and why?
Try it first, then show the solutionSolution
- The number of coins needed is unknown in advance.
- The loop depends only on the condition total < 500.
- while (total < 500) { total += coin(); }
Answer: A while loop, because the repetitions depend on a condition rather than a known count.
Memory hook: Unknown count, use while.
Topics in this chapter
Iterative Constructs in Java: syntax and key facts
- for syntax
- for (init; condition; update) { body }
- Commas for multiple variables.
- while / do-while
- while (condition) { body } do { body } while (condition);
- do-while ends with a semicolon.
- Digit loop
- while (n > 0) { d = n % 10; … ; n = n / 10; }
- Save a copy of n first; reverse: rev = rev * 10 + d.
- Jumps
- break: leave loop; continue: next pass
How the board asks Iterative Constructs in Java
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| MCQ (Section A) | Entry-controlled loops (2024); infinite loop (2025); semicolon after for (2025); jumbled factorial statements (2025); a real-life repetition (2025). | Dry-run each option for two or three passes; watch semicolons and update directions. |
| Conversion and output (2 marks) | do-while to for (2024); for to exit-controlled (2025); output and iterations of a for loop without braces (2024). | Copy init, condition and update exactly; trace in a table. |
| Program (15 marks) | EvenPal number (2024); sum of largest and smallest digit even or odd (2025). | Accept the number, save a copy, one digit loop for all quantities, then test and print both messages. |
Tricks to remember Iterative Constructs in Java
Short memory aids. Say each one aloud once, then use it on a question the same day.
ICBU
Initialise, Check, Body, Update: the order of every for and while.
Use it when: Tracing and converting loops.
Do-while: do it, then doubt it
Body first, condition later; always once.
Use it when: Exit-controlled questions.
Mod takes, divide drops
n % 10 takes the last digit; n / 10 drops it.
Use it when: Digit programs.
Iterative Constructs in Java: board-level solved questions
Ordered from board level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: Entry-controlled loops · 1 mark · MCQBoard level
Which are entry-controlled loops? 1. for 2. while 3. do-while 4. switch
(a) only 1
(b) 1 and 2
(c) 1 and 3
(d) 3 and 4
Try it first, then show the solutionSolution
- for and while test before the body; do-while after; switch is not a loop.
Answer: (b) 1 and 2
Example 2: Semicolon after for · 1 mark · MCQHarder board level
int p = 0;
for (p = 4; p > 0; p -= 2);
System.out.print(p);
System.out.println(p);
The output is:
(a) 42
(b) 4200
(c) 0 and 0 on two lines
(d) 00
Try it first, then show the solutionSolution
- The ; makes an empty body: p goes 4, 2, 0, then stops.
- print then println on the same line: 00.
Answer: (d) 00
Example 3: Zero-iteration loop · 2 marks · Very short answerHarder board level
Give the output and the number of times the loop runs:
for (x = 10; x > 20; x++)
System.out.println(x);
System.out.println(x * 2);
Try it first, then show the solutionSolution
- 10 > 20 is false: loop runs 0 times.
- Only the second statement is outside the loop: prints 20.
Answer: Output: 20; the loop executes 0 times.
Example 4: for to do-while · 2 marks · Very short answerHarder board level
Convert into an exit-controlled loop and give the output:
int a, b;
for (a = 10, b = 1; a >= 1; a -= 2)
{
b += a;
b++;
}
System.out.print(b);
Try it first, then show the solutionSolution
- a = 10; b = 1; do { b += a; b++; a -= 2; } while (a >= 1);
- b: 12, 21, 28, 33, 36 for a = 10, 8, 6, 4, 2; prints 36.
Answer: int a = 10, b = 1;
do
{
b += a;
b++;
a -= 2;
} while (a >= 1);
System.out.print(b);
Output: 36
Example 5: Jumbled factorial · 1 mark · MCQHarder board level
Arrange to calculate and return the factorial of 4: (1) for (k = 1; k <= 4; k++) (2) return fa; (3) long fa = 1, k; (4) fa *= k;
(a) 1, 2, 3, 4
(b) 3, 1, 4, 2
(c) 3, 1, 2, 4
(d) 1, 3, 2, 4
Try it first, then show the solutionSolution
- Declare first (3), loop (1) with body (4), return after the loop (2).
Answer: (b) 3, 1, 4, 2
Example 6: Harshad number (Section B) · 15 marks · Long answerHarder board level
Write a program to accept a number and check whether it is a Harshad (Niven) number: a number divisible by the sum of its digits. Example: 156 → 1 + 5 + 6 = 12, and 156 ÷ 12 = 13, so 156 is a Harshad number.
Try it first, then show the solutionSolution
- import java.util.Scanner;
class Harshad
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int copy = n, sum = 0; - while (copy > 0)
{
sum = sum + copy % 10;
copy = copy / 10;
} - if (sum > 0 && n % sum == 0)
System.out.println(n + " is a Harshad number");
else
System.out.println(n + " is not a Harshad number");
}
} - Trace 156: digits 6, 5, 1 → sum 12; 156 % 12 = 0 → Harshad.
- Trace 157: sum 13; 157 % 13 = 1 → not Harshad. (sum > 0 guards against 0.)
Answer: Sample runs:
Enter a number: 156
156 is a Harshad number
Enter a number: 157
157 is not a Harshad number
Example 7: Largest and smallest digit · 15 marks · Long answerHOTS / competency
Write a program to accept a number and check whether the sum of its largest and smallest digits is even or odd. Example: 6425 → 6 + 2 = 8, Sum is even; 3748 → 8 + 3 = 11, Sum is odd.
Try it first, then show the solutionSolution
- import java.util.Scanner;
class DigitSum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int max = 0, min = 9; - while (n > 0)
{
int d = n % 10;
if (d > max) max = d;
if (d < min) min = d;
n = n / 10;
} - int s = max + min;
System.out.println("Largest digit: " + max + ", Smallest digit: " + min);
if (s % 2 == 0)
System.out.println("Sum is even");
else
System.out.println("Sum is odd");
}
} - max starts at 0 (smallest possible digit) and min at 9 (largest possible); the sum is found after the loop, not inside it.
- Trace 6425: digits 5, 2, 4, 6 → max 6, min 2 → 8 → even. Trace 3748: 8, 4, 7, 3 → max 8, min 3 → 11 → odd.
Answer: Sample run:
Enter a number: 3748
Largest digit: 8, Smallest digit: 3
Sum is odd
Case-study practice (Section B style)
Read the situation, then answer every part. In the board paper this is a 10-mark question split into 2 + 3 + 5 marks.
Checking a palindrome
Imagine Meera writes a program to check whether a number is a palindrome. Her loop is:
while (n > 0)
{
d = n % 10;
rev = rev * 10 + d;
n = n / 10;
}
if (rev == n) System.out.println("Palindrome");
At first she declared int rev; without a value and the program did not compile; after she fixed that, it said no number is a palindrome.
(i) · 2 marks
Why must rev be initialised, and to what?
Answer
Answer: rev is an accumulator built from scratch; a local variable must be given a value before use (otherwise a compile-time error), and it must start at 0.
1 mark each.
(ii) · 3 marks
Why does her program never print 'Palindrome'? Fix it.
Answer
Answer: After the loop n has become 0, so rev is compared with 0, not the original number. Fix: store int copy = n; before the loop and test if (rev == copy).
Reason 2 marks; fix 1 mark.
(iii) · 5 marks
Trace the corrected loop for 121 and give the final message. Extend it to print 'EvenPal' if the number is a palindrome and its digit sum is even.
Answer
Answer: 121: d = 1, rev = 1, n = 12; d = 2, rev = 12, n = 1; d = 1, rev = 121, n = 0. rev == copy → Palindrome. Extension: add sum += d; inside the loop; then if (rev == copy && sum % 2 == 0) print "EvenPal" else "Not EvenPal". For 121 the sum is 4 → EvenPal.
Trace 2 marks; extension 3 marks.
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.
Convert a for loop into an exit-controlled loop and give the output. (2 marks) (2 marks)
| Step | Marks |
|---|---|
| Correct do-while with init before, update inside, condition and semicolon at the end. | 1 |
| Correct output. | 1 |
Program: largest + smallest digit even or odd. (15 marks) (15 marks)
| Step | Marks |
|---|---|
| Class, main, Scanner and number accepted. | 2 |
| max and min initialised correctly (0 and 9). | 2 |
| Digit loop with n % 10 and n /= 10. | 4 |
| Correct comparisons updating max and min. | 3 |
| Sum after the loop and even/odd test. | 2 |
| Both messages printed as required. | 2 |
Where students lose marks in Iterative Constructs in Java
The slips examiners see most often, and what to write instead.
Mistake: Ignoring the semicolon after for
The 2025 examiners noted candidates ignored the ; at the end of the for statement.
Do this: A ; after for(...) means an empty body; the next statement runs once, after the loop.
Usually costs: 1 mark.
Mistake: Treating both statements as the loop body
In 2024 candidates printed two lines per pass, missing that without braces only the next statement is in the loop.
Do this: Without { }, the loop controls one statement only.
Usually costs: 1 to 2 marks.
Mistake: Missing the semicolon after while in do-while, or the update
The 2025 examiners reported these errors in loop conversion.
Do this: do { …; update; } while (condition);
Usually costs: 1 mark.
Mistake: Not saving the original number
In the 2024 EvenPal program candidates compared the reverse with the destroyed n.
Do this: int copy = n; before the digit loop.
Usually costs: 2 to 3 marks of 15.
Mistake: Wrong initial max/min or sum inside the loop
In 2025 candidates initialised max and min wrongly and found the sum inside the loop.
Do this: max = 0, min = 9 for digits; compute the sum after the loop.
Usually costs: 2 to 4 marks of 15.
Practice questions on Iterative Constructs 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 · MCQBoard level
The loop that always executes at least once is:
(a) for
(b) while
(c) do-while
(d) noneHint
Exit controlled.
Answer
(c) do-while
Q2 · 1 mark · MCQBoard level
Which statement skips the remaining body and starts the next iteration?
(a) break
(b) continue
(c) exit
(d) returnHint
Go on.
Answer
(b) continue
Q3 · 2 marks · Very short answerHarder board level
Give the output:
int i = 1;
while (i < 30)
{
i = i * 3;
System.out.print(i + " ");
}Hint
Multiply then print.
Answer
3 9 27 81 (i = 27 < 30 runs once more, printing 81)
Q4 · 2 marks · Very short answerBoard level
Convert to a while loop: for (int k = 5; k >= 1; k--) System.out.print(k);
Hint
Move init and update.
Answer
int k = 5;
while (k >= 1)
{
System.out.print(k);
k--;
}
Output: 54321Q5 · 2 marks · Short answerHOTS / competency
Give the output:
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0) continue;
if (i == 8) break;
System.out.print(i + " ");
}Hint
Skip multiples of 3; stop at 8.
Answer
1 2 4 5 7
Q6 · 2 marks · Short answerHOTS / competency
How many times does this loop run, and what is the final value of m? int m = 100; do { m = m / 3; } while (m > 5);
Hint
Integer division.
Answer
m: 33, 11, 3; it runs 3 times (after 3, 3 > 5 is false). Final m = 3.
Q7 · 15 marks · Long answerHarder board level
Write a program to print the sum of the series 1 + 1/2 + 1/3 + ... + 1/n for a given n.
Hint
Use 1.0 / i.
Answer
import java.util.Scanner;
class Series
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("n: ");
int n = sc.nextInt();
double s = 0;
for (int i = 1; i <= n; i++)
s = s + 1.0 / i;
System.out.println("Sum = " + s);
}
}
Trace n = 4: 1 + 0.5 = 1.5; + 0.333... = 1.8333...; + 0.25 = 2.0833...; printed as Sum = 2.083333333333333. (1 / i would give 0 for i > 1.)Q8 · 15 marks · Long answerHOTS / competency
Write a program to accept a number and check whether it is a Spy number (sum of digits equals product of digits). Example: 1124 → sum 8, product 8.
Hint
Product starts at 1.
Answer
import java.util.Scanner;
class Spy
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Number: ");
int n = sc.nextInt();
int sum = 0, prod = 1;
while (n > 0)
{
int d = n % 10;
sum += d;
prod *= d;
n /= 10;
}
if (sum == prod)
System.out.println("Spy number");
else
System.out.println("Not a Spy number");
}
}
Traced: 1124 → sum 8, product 8 → Spy number; 123 → sum 6, product 6 → Spy number; 124 → 7 and 8 → Not a Spy number.Q9 · 2 marks · Very short answerBoard level
What is a delay loop? Write one.
Hint
Empty body.
Answer
A loop with an empty body used only to pass time, e.g. for (long t = 1; t <= 500000; t++);
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.What does n % 10 give?Show answer
The last digit of n.
3.What does break do in a loop?Show answer
Ends the loop immediately.
4.Write an infinite for loop.Show answer
for ( ; ; )
5.Initial value of a product accumulator?Show answer
1
Best order to study it
- for, while, do-while syntax and flow.
- Entry vs exit controlled.
- Tracing outputs and counting iterations.
- break and continue.
- Loop conversion.
- Digit programs: sum, product, reverse, palindrome, max/min.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- for/while: test first (may run 0 times); do-while: test last (runs ≥ 1).
- No braces → one statement in the loop; ; after for → empty body.
- break exits; continue skips to next pass.
- Convert by moving init, condition, update.
- Digits: d = n % 10; n /= 10; save a copy first.
Iterative Constructs in Java: frequently asked questions
Are nested while and nested do-while in the syllabus?
No; nested loops are studied with for only (Nested for Loops chapter).
Can a for loop have no initialisation?
Yes: for ( ; i <= 5; i++) is valid if i was initialised earlier.
When should I use do-while?
When the body must run at least once, such as menus that ask 'continue?' at the end.
Based on: CISCE ICSE 2027 Computer Applications syllabus; CISCE Analysis of Pupil Performance ICSE 2025: Computer Applications (with the 2025 board paper)