Nested for Loops ICSE Class 10 Computer Applications
Updated 14 October 2026 · Fact-checked
A nested for loop is a for loop inside another. For each pass of the outer loop the inner loop runs completely, so r outer and c inner passes run the body r × c times. ICSE Class 10 uses nested loops for rectangular and right-angled triangle patterns and series; break and continue affect only their own loop.
What Nested for Loops is about
Pattern questions test whether you understand loop control exactly. The 2024 paper asked for rows of 1 2 3 4 5 inside an overloaded method, and the 2025 paper asked for four rows of @#@#@; examiners deducted marks for using println() in both loops, swapping outer and inner loops, and forgetting println() after the inner loop. A 2025 assertion-reason item compared nested loops to a clock.
The syllabus asks for nested loops through simple examples, break and continue in nested loops, programs on rectangular and right-angled triangle patterns, and series involving a single variable. Nested while and nested do-while are not included.
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
- How nested loops run
In a nested loop, the inner loop is the body of the outer loop. For each value of the outer variable, the inner variable starts again and runs through all its values.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 2; j++)
System.out.print(i + "" + j + " ");
}
Output: 11 12 21 22 31 32The body runs 3 × 2 = 6 times. Think of a clock: for each of the 12 hours (outer), the minute hand runs through all 60 minutes (inner).
For patterns: the outer loop controls rows (one pass per line) and the inner loop controls columns (what is printed on that line); println() after the inner loop moves to the next line.
Worked example
(2 marks) How many times is the statement inside executed? for (i = 1; i <= 4; i++) for (j = 1; j <= 5; j++) count++;
Try it first, then show the solutionSolution
- Outer loop: 4 passes; inner: 5 passes each - 1 mark.
- 4 × 5 = 20 - 1 mark.
Answer: 20 times
- Rectangular patterns
In a rectangle every row has the same number of columns, so the inner loop's limit is fixed.
Rows of 1 2 3 4 5 (r = 4 rows, c = 5 columns):
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 5; j++)
System.out.print(j + " ");
System.out.println();
}
Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5Alternating symbols @#@#@ (4 rows): odd columns '@', even columns '#':
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j % 2 == 1) System.out.print("@");
else System.out.print("#");
}
System.out.println();
}
Output: @#@#@ on each of 4 lines.Worked example
(2 marks) Give the output:
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 4; j++)
System.out.print(i);
System.out.println();
}Try it first, then show the solutionSolution
- Inner loop prints the row number i four times - 1 mark.
- Three rows - 1 mark.
Answer: 1111
2222
3333- Right-angled triangle patterns
In a right-angled triangle the number of columns changes with the row, so the inner limit depends on i.
Increasing triangle (inner j <= i):
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
Output:
1
1 2
1 2 3
1 2 3 4Decreasing triangle (inner j <= 5 - i, or outer counting down):
for (int i = 4; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
System.out.print("*");
System.out.println();
}
Output:
****
***
**
*Floyd-style (numbers continue): use a separate counter k that is never reset:
int k = 1;
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(k++ + " ");
System.out.println();
}
Output:
1
2 3
4 5 6Worked example
(2 marks) Write the loops to print:
5
5 4
5 4 3
5 4 3 2Try it first, then show the solutionSolution
- Outer i from 1 to 4 rows; each row starts at 5 - 1 mark.
- Inner j from 5 down to 6 − i; print j; println after the inner loop - 1 mark.
Answer: for (int i = 1; i <= 4; i++)
{
for (int j = 5; j >= 6 - i; j--)
System.out.print(j + " ");
System.out.println();
}- break and continue in nested loops
break and continue act only on the innermost loop that contains them.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == 2) break;
System.out.print(i + "" + j + " ");
}
}
The break leaves only the inner loop each time; the outer loop continues. Output: 11 21 31With continue instead of break, only j = 2 is skipped: 11 13 21 23 31 33
Worked example
(2 marks) Give the output:
for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 3; j++)
{
if (i == j) continue;
System.out.print(i * j + " ");
}
}Try it first, then show the solutionSolution
- i = 1: j = 1 skipped; j = 2 → 2; j = 3 → 3 - 1 mark.
- i = 2: j = 1 → 2; j = 2 skipped; j = 3 → 6 - 1 mark.
Answer: 2 3 2 6
- Series with a single variable
Series programs add terms that depend on one variable (such as x or n).
Single loop: S = x + x²/2 + x³/3 + ... + xⁿ/n:
double s = 0;
for (int i = 1; i <= n; i++)
s = s + Math.pow(x, i) / i;Nested loop (when each term needs its own inner calculation): S = 1! + 2! + 3! + ... + n!:
long s = 0;
for (int i = 1; i <= n; i++)
{
long f = 1;
for (int j = 1; j <= i; j++)
f = f * j;
s = s + f;
}
For n = 4: 1 + 2 + 6 + 24 = 33. The factorial f must be reset to 1 for each term (inside the outer loop).Worked example
(2 marks) What is wrong if long f = 1; is placed before the outer loop in the factorial series program? What is printed for n = 3?
Try it first, then show the solutionSolution
- f is never reset, so each term multiplies on top of the previous factorial - 1 mark.
- Terms become 1, 1 × 1 × 2 = 2, 2 × 1 × 2 × 3 = 12; sum = 15 instead of 9 - 1 mark.
Answer: f must restart at 1 for each term. With f outside, the terms are 1, 2 and 12, so 15 is printed instead of the correct 1 + 2 + 6 = 9.
Nested for Loops 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.
Nested loops
Suppose a cinema hall has 5 rows with 8 seats in each row, and an usher checks every seat after the show.
How would nested loops model the usher's check, and how many seats are checked?
Try it first, then show the solutionSolution
- Outer loop over rows: 1 to 5.
- Inner loop over seats in a row: 1 to 8.
- Total checks: 5 × 8 = 40.
Answer: for (row = 1; row <= 5; row++) for (seat = 1; seat <= 8; seat++) check(row, seat); 40 seats are checked.
Memory hook: Rows outside, seats inside.
Clock as nested loop
Imagine a digital clock showing hours and minutes from 0:00 to 11:59.
Explain why it is a nested loop and count the displays.
Try it first, then show the solutionSolution
- For each hour (outer, 12 values), minutes run 0 to 59 (inner, 60 values).
- The inner loop completes before the hour changes.
- 12 × 60 = 720 displays.
Answer: Hours form the outer loop and minutes the inner loop; the minutes run through all 60 values for each hour, giving 720 displays.
Memory hook: Minutes finish, then the hour ticks.
Triangle pattern
Suppose cans are stacked in a supermarket display so that row 1 has 1 can, row 2 has 2 cans, up to row 6.
Which pattern does this match, and how many cans are there?
Try it first, then show the solutionSolution
- Columns in row i = i: a right-angled triangle.
- Total = 1 + 2 + ... + 6 = 21.
Answer: A right-angled triangle pattern (inner loop j <= i); 21 cans in all.
Memory hook: Row number = items in the row.
Topics in this chapter
Nested for Loops: syntax and key facts
- Iterations
- Rectangle: rows × columns; triangle: 1 + 2 + … + n = n(n + 1)/2
- Inner limit fixed → rectangle; depends on i → triangle.
- Pattern template
- for (i = 1; i <= rows; i++) { for (j = 1; j <= limit; j++) print(...); println(); }
- print inside, println after the inner loop.
- Jumps
- break / continue affect only the innermost loop
How the board asks Nested for Loops
The question patterns to expect, and the method that works for each.
| Question type | How it is asked | Method |
|---|---|---|
| MCQ and assertion-reason (Section A) | A clock as a real-life nested loop (2025); count iterations; output of a short nested loop. | Multiply outer by inner passes; trace row by row. |
| Output (2 marks) | Give the output of a pattern loop or of break/continue inside nested loops. | Write each row separately; note where println() is. |
| Program (15 marks) | Patterns inside overloaded methods (2024: rows of 1 2 3 4 5; 2025: @#@#@), series programs. | Outer loop = rows, inner = columns; print in inner, println after inner; use the method signatures given. |
Tricks to remember Nested for Loops
Short memory aids. Say each one aloud once, then use it on a question the same day.
Outer rows, inner columns
The outer loop is the number of lines; the inner loop is what goes on a line.
Use it when: Every pattern.
Triangle = j <= i
If the row number decides how many items, the inner limit is i.
Use it when: Right-angled triangles.
Nested for Loops: board-level solved questions
Ordered from board level to HOTS. Cover the solution, write your own, then compare step by step.
Example 1: Clock assertion · 1 mark · Assertion-reasonBoard level
Assertion (A): A clock is a real-life example of nested loops.
Reason (R): The minute hand moves through 60 positions for each position of the hour hand.
(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 inner (minute) loop completes for each outer (hour) pass.
Answer: (a) Both A and R are true and R is the correct explanation of A
Example 2: Count iterations of a triangle · 1 mark · MCQHarder board level
How many stars are printed?
for (int i = 1; i <= 6; i++)
for (int j = 1; j <= i; j++)
System.out.print("*");
(a) 6
(b) 21
(c) 36
(d) 12
Try it first, then show the solutionSolution
- 1 + 2 + 3 + 4 + 5 + 6 = 21.
Answer: (b) 21
Example 3: Break in inner loop · 2 marks · Very short answerHarder board level
Give the output:
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == 2) break;
System.out.print(i + "" + j + " ");
}
}
Try it first, then show the solutionSolution
- For each i, j = 1 prints i1; at j = 2 the inner loop breaks.
- The outer loop continues for i = 2 and 3.
Answer: 11 21 31
Example 4: Number triangle output · 2 marks · Short answerHOTS / competency
Give the output:
for (int i = 1; i <= 4; i++)
{
for (int j = i; j >= 1; j--)
System.out.print(j);
System.out.println();
}
Try it first, then show the solutionSolution
- Row i prints i down to 1.
- Rows: 1, 21, 321, 4321.
Answer: 1
21
321
4321
Example 5: Overloaded pattern methods (Section B) · 15 marks · Long answerHarder board level
Define a class with an overloaded method pattern(): void pattern(int r, int c) prints r rows of the numbers 1 to c; void pattern(int n) prints a right-angled triangle of n rows where row i has i stars. Write main() to call pattern(3, 4) and pattern(4).
Try it first, then show the solutionSolution
- class Patterns
{
void pattern(int r, int c)
{
for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= c; j++)
System.out.print(j + " ");
System.out.println();
}
} - void pattern(int n)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
System.out.print("*");
System.out.println();
}
} - public static void main(String args[])
{
Patterns p = new Patterns();
p.pattern(3, 4);
p.pattern(4);
}
} - The two methods share a name but differ in the number of parameters: method overloading.
- Trace pattern(3, 4): three lines of 1 2 3 4. Trace pattern(4): *, , *, ****.
Answer: Output:
1 2 3 4
1 2 3 4
1 2 3 4
*
**
***
****
Example 6: Factorial series · 15 marks · Long answerHOTS / competency
Write a program to accept n and print the sum S = 1! + 2! + 3! + ... + n! using nested loops.
Try it first, then show the solutionSolution
- import java.util.Scanner;
class FactSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("n: ");
int n = sc.nextInt();
long s = 0; - for (int i = 1; i <= n; i++)
{
long f = 1;
for (int j = 1; j <= i; j++)
f = f * j;
s = s + f;
} - System.out.println("Sum = " + s);
}
} - f is reset to 1 inside the outer loop so each term is a fresh factorial; long avoids overflow for larger n.
- Trace n = 4: terms 1, 2, 6, 24; s = 33.
Answer: Sample run:
n: 4
Sum = 33
Example 7: Alternating pattern · 2 marks · Short answerBoard level
Write the nested loops to print 4 rows of @#@#@.
Try it first, then show the solutionSolution
- Outer i = 1 to 4 rows; inner j = 1 to 5 columns.
- Print @ for odd j and # for even j; println() after the inner loop.
Answer: for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j % 2 == 1) System.out.print("@");
else System.out.print("#");
}
System.out.println();
}
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.
Debugging a pattern
Imagine Rohan wants the pattern
1
1 2
1 2 3
but writes:
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
System.out.println(j + " ");
}
(i) · 2 marks
What does his code print? (Describe it.)
Answer
Answer: Nine lines: 1, 2, 3, 1, 2, 3, 1, 2, 3, each number on its own line, because println is used inside the inner loop.
1 mark for nine lines; 1 mark for the reason.
(ii) · 3 marks
Identify his two mistakes.
Answer
Answer: (1) The inner limit is fixed at 3 (rectangle) instead of i (triangle). (2) println is inside the inner loop; it should be print inside and println after the inner loop.
Inner limit 1 mark; print/println placement 2 marks.
(iii) · 5 marks
Write the corrected loops and say how many numbers are printed for 6 rows.
Answer
Answer: for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
For 6 rows: 1 + 2 + ... + 6 = 21 numbers.Code 4 marks; count 1 mark.
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 nested loop with break. (2 marks) (2 marks)
| Step | Marks |
|---|---|
| Correct effect of break on the inner loop only. | 1 |
| Complete output in order. | 1 |
Program: overloaded pattern methods (rectangle and triangle). (15 marks) (15 marks)
| Step | Marks |
|---|---|
| Class and correct method signatures (same name, different parameters). | 3 |
| Rectangle: outer rows, inner fixed columns. | 3 |
| Triangle: inner limit depends on i. | 3 |
| print inside, println after inner loop in both. | 3 |
| main(): object creation and both calls. | 3 |
Where students lose marks in Nested for Loops
The slips examiners see most often, and what to write instead.
Mistake: println() in both loops
The 2025 examiners reported this in the @#@#@ pattern.
Do this: print() inside the inner loop; println() once after it.
Usually costs: 2 to 3 marks of 15.
Mistake: Swapping outer and inner loops
Also reported in 2025.
Do this: Outer = rows (lines), inner = columns (items per line).
Usually costs: 2 to 3 marks.
Mistake: Forgetting println() after the inner loop
The 2024 examiners noted this in the rows-of-numbers pattern.
Do this: Without it, all rows run together on one line.
Usually costs: 1 to 2 marks.
Mistake: Not resetting inner accumulators
Series terms that need an inner sum or factorial go wrong.
Do this: Initialise the term variable inside the outer loop.
Usually costs: 2 to 4 marks.
Practice questions on Nested for Loops
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
In a pattern program, the outer loop usually controls the:
(a) columns
(b) rows
(c) characters
(d) spacesHint
Lines.
Answer
(b) rows
Q2 · 1 mark · MCQBoard level
for (i = 1; i <= 5; i++) for (j = 1; j <= 3; j++) x++; If x starts at 0, its final value is:
(a) 8
(b) 15
(c) 5
(d) 3Hint
5 × 3.
Answer
(b) 15
Q3 · 2 marks · Very short answerHarder board level
Give the output:
int k = 1;
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(k++ + " ");
System.out.println();
}Hint
k never resets.
Answer
1
2 3
4 5 6Q4 · 2 marks · Short answerHOTS / competency
Give the output:
for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == 2) continue;
System.out.print(i + j + " ");
}
}Hint
i + j is a number here.
Answer
2 4 3 5 (i = 1: j = 1 → 2, j = 3 → 4; i = 2: j = 1 → 3, j = 3 → 5)
Q5 · 2 marks · Very short answerHarder board level
Write loops to print:
A
A B
A B C
A B C DHint
char arithmetic.
Answer
for (int i = 1; i <= 4; i++)
{
for (int j = 0; j < i; j++)
System.out.print((char)('A' + j) + " ");
System.out.println();
}Q6 · 15 marks · Long answerHarder board level
Write a program to print the sum of the series S = 1 + (1 + 2) + (1 + 2 + 3) + ... up to n terms using nested loops.
Hint
Inner sum resets each term.
Answer
import java.util.Scanner;
class TriSeries
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("n: ");
int n = sc.nextInt();
int s = 0;
for (int i = 1; i <= n; i++)
{
int t = 0;
for (int j = 1; j <= i; j++)
t = t + j;
s = s + t;
}
System.out.println("Sum = " + s);
}
}
Trace n = 4: terms 1, 3, 6, 10; Sum = 20.Q7 · 15 marks · Long answerBoard level
Write a program to accept n and print a right-angled triangle where row i contains the number i repeated i times (e.g. n = 3: 1 / 22 / 333).
Hint
Print i, not j.
Answer
import java.util.Scanner;
class Repeat
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("n: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(i);
System.out.println();
}
}
}
Trace n = 3: 1 / 22 / 333.Q8 · 2 marks · Short answerHOTS / competency
Does break inside the inner loop stop the outer loop? Explain with an example.
Hint
Innermost only.
Answer
No. break exits only the loop that contains it. In for (i...) { for (j...) { if (j == 2) break; ... } } the inner loop stops at j = 2, but the outer loop carries on with the next i.
Q9 · 2 marks · Very short answerBoard level
Why are nested while and nested do-while not examined?
Hint
Syllabus.
Answer
The 2027 syllabus limits nested loops to for loops; nested while and nested do-while are explicitly excluded.
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.Iterations of 4 × 5 nested loops?Show answer
20
2.Inner limit for a right-angled triangle?Show answer
j <= i (depends on the row).
3.Does continue in the inner loop skip the outer pass?Show answer
No, only the current inner pass.
4.Where does println() go in a pattern?Show answer
After the inner loop, inside the outer loop.
5.Are nested while loops in the syllabus?Show answer
No.
Best order to study it
- How nested loops execute; counting iterations.
- Rectangular patterns.
- Right-angled triangle patterns (increasing, decreasing, Floyd-style).
- break and continue in nested loops.
- Series using nested loops.
Quick revision checklist
The night before the exam: can you do each of these without looking?
- Inner loop completes for every outer pass.
- Rectangle: inner limit fixed; triangle: inner limit i.
- print inside, println after inner.
- break/continue affect the innermost loop only.
- Reset term variables inside the outer loop for series.
Nested for Loops: frequently asked questions
Are pyramid (centred) patterns in the syllabus?
No. Only rectangular and right-angled triangle patterns are listed.
Can the inner loop variable be used after the loop?
Not if it was declared in the for header (int j); its scope ends with the loop.
How do I print letters in a pattern?
Use char arithmetic with a cast, e.g. (char)('A' + j).
Based on: CISCE ICSE 2027 Computer Applications syllabus; CISCE Analysis of Pupil Performance ICSE 2025: Computer Applications (with the 2025 board paper)