Pattern Programs and Series with Nested for Loops (ICSE Class 10 Java)
Updated 17 October 2026 · Fact-checked
In a nested for loop the inner loop runs completely for every single pass of the outer loop. The outer variable usually controls the row and the inner variable the columns, with println() after the inner loop to start a new row. If the inner loop runs a fixed count, total inner passes = outer passes multiplied by inner passes; in a triangle the inner limit depends on the outer variable.
The inner loop runs all 4 columns for each row, then println() starts a new row, so the body runs 3 × 4 = 12 times.
Illustration drawn for QuestionBuddy.
Understand Pattern Programs and Series with Nested for Loops
How a nested loop runs. For each value of the outer variable i, the inner loop starts again from its initial value and runs until its condition fails. Only then does the outer loop move to the next i.
Rows and columns. The outer loop counts rows; the inner loop prints the items of one row; System.out.println(); placed after the inner loop (inside the outer one) moves to the next line.
Rectangular pattern. Fixed inner limit:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4; j++)
System.out.print(i * j + " ");
System.out.println();
}Inner body runs 3 times 4 = 12 times.
Right-angled triangle (the only triangle in the syllabus). The inner limit depends on the outer variable:
- ascending:
for (j = 1; j <= i; j++)gives rows of length 1, 2, 3... - descending: outer loop counts down, or the inner limit is
n - i + 1.
Total passes of the inner body for rows 1 to n: 1 + 2 + ... + n.
break and continue in nested loops. They act only on the innermost loop that contains them. break leaves the inner loop; the outer loop carries on with its next pass. continue skips the rest of the inner pass.
Series with a single variable. Use one loop for the terms and update the term with the previous term: for x + x squared + x cubed + ... up to n terms, keep p (the current power) and an accumulator s: p *= x; s += p; inside the loop. Series built from n alone, such as 1 + 4 + 9 + ... (squares), use i * i.
Nested while and do-while loops are not part of the syllabus.
Pattern Programs and Series with Nested for Loops: key facts
- Row-and-column structure
- Outer loop: rows
- Inner loop: items of one row
- println() after the inner loop
- print() inside, println() outside the inner loop.
- Total inner passes
- Fixed inner count: outer passes multiplied by inner passes | Triangle of n rows: 1 + 2 + ... + n
- For n = 4 the triangle needs 10 inner passes.
- Jump statements in nested loops
- break: leaves the innermost loop only | continue: skips the rest of the innermost pass
- The outer loop continues normally.
- Power series pattern
- p = p times x
- s = s + p
- repeat n times
- Start p = 1 and s = 0.
How to answer Pattern Programs and Series with Nested for Loops questions
Use this routine for pattern output questions.
- 1List the values of the outer variable i.
- 2For each i, work out the limits of the inner variable j (fixed, or depending on i).
- 3Write the items printed by the inner loop on one line, using exactly the separators in print.
- 4Look for println after the inner loop and break to the next line there.
- 5Count total body passes by adding the inner counts of all rows.
Pattern Programs and Series with Nested for Loops 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.
Rows and seats
Suppose a hall has 3 rows with 4 seats each and the program prints the seat numbers row by row.
How many times does the inner loop body run in all?
Try it first, then show the solutionSolution
- Each of the 3 rows runs the inner loop 4 times.
- 3 multiplied by 4 = 12 passes.
Answer: 12 passes.
Memory hook: Rows times seats.
Staircase of stacked boxes
Imagine a display where row 1 has 1 box, row 2 has 2 boxes, up to row 4.
How many boxes are used?
Try it first, then show the solutionSolution
- The inner limit equals the row number: 1 + 2 + 3 + 4.
- Total = 10 boxes.
Answer: 10 boxes.
Memory hook: A triangle adds the row numbers.
Tricks to remember Pattern Programs and Series with Nested for Loops
Short memory aids. Say each one aloud once, then use it on a question the same day.
Clock hands
The outer loop is the hour hand (slow), the inner loop the minute hand (fast): the minute hand does a full round for each hour step.
Use it when: How nested loops run.
print inside, println outside
print() belongs inside the inner loop; println() goes after it.
Use it when: Pattern layout.
j <= i makes a triangle
If the inner limit uses i, expect a triangle; if it is a constant, expect a rectangle.
Use it when: Predicting a shape.
Pattern Programs and Series with Nested for Loops: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Triangle of numbers · 2 marks · Short answerBoard level
Write the output:
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j);
System.out.println();
}Try it first, then show the solutionSolution
- For each i the inner loop prints 1 up to i with no separator. (1 mark)
- println() after the inner loop ends each row. (1 mark)
Answer: 1
12
123
1234
Example 2: Rectangular pattern · 3 marks · Short answerHarder board level
Write the output and the number of times the inner body runs:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4; j++)
System.out.print(i * j + " ");
System.out.println();
}Try it first, then show the solutionSolution
- Row i = 1 prints 1 2 3 4; row i = 2 prints 2 4 6 8. (1 mark)
- Row i = 3 prints 3 6 9 12. (1 mark)
- The inner body runs 3 x 4 = 12 times. (1 mark)
Answer: 1 2 3 4
2 4 6 8
3 6 9 12
12 passes (each row ends with a space)
Example 3: Inverted triangle · 2 marks · Short answerHarder board level
Write the output:
for (int i = 4; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print(i);
System.out.println();
}Try it first, then show the solutionSolution
- The outer loop counts down from 4 to 1, and the inner loop prints i exactly i times. (1 mark)
- Rows: 4444, 333, 22 and 1. (1 mark)
Answer: 4444
333
22
1
Example 4: Case study: break and continue in nested loops · 4 marks · Case-basedHOTS / competency
A student runs:
int c = 0;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2)
continue;
if (i == 2)
break;
c++;
}
}
System.out.println(c);(i) Trace the value of c for each i. (2)
(ii) State the output. (1)
(iii) How many times does the inner loop start its body in all? (1)
Try it first, then show the solutionSolution
- (i) i = 1: j = 1 gives c = 1; j = 2 continue; j = 3 gives c = 2. i = 2: j = 1 reaches break, which leaves only the inner loop, so c stays 2. i = 3: j = 1 gives c = 3, j = 2 continue, j = 3 gives c = 4.
- (ii) The output is 4.
- (iii) Bodies started: 3 for i = 1, 1 for i = 2 (then break), 3 for i = 3: 7 in all.
Answer: (ii) 4 (iii) 7
Example 5: Sum of a power series · 3 marks · Short answerHarder board level
Find the output:
int x = 2, n = 4;
int s = 0, p = 1;
for (int i = 1; i <= n; i++) {
p *= x;
s += p;
}
System.out.println(s + " " + p);Name the series computed.
Try it first, then show the solutionSolution
- i = 1: p = 2, s = 2. i = 2: p = 4, s = 6. (1 mark)
- i = 3: p = 8, s = 14. i = 4: p = 16, s = 30. (1 mark)
- Output 30 16. The series is x + x^2 + x^3 + ... + x^n (2 + 4 + 8 + 16). (1 mark)
Answer: 30 16; the series x + x^2 + x^3 + ... + x^n
Where students lose marks in Pattern Programs and Series with Nested for Loops
The slips examiners see most often, and what to write instead.
Mistake: Putting println() inside the inner loop
Every item then goes on its own line.
Wrong working
for j ... { print(j); println(); }
Right working
for j ... print(j); println();
Do this: println after the inner loop.
Usually costs: 1 mark
Mistake: Thinking break leaves both loops
break leaves only the innermost loop.
Wrong working
After break at i = 2 the program stops.
Right working
After break at i = 2 the outer loop continues with i = 3.
Do this: Read break as "leave this loop only".
Usually costs: 1 to 2 marks
Mistake: Skipping the separators in the output
Spaces and the absence of spaces are part of the answer.
Wrong working
1234 for print(j + " ")
Right working
1 2 3 4
Do this: Copy the separator from the print statement.
Usually costs: 1 mark
Exam tips
- Write the pattern lines exactly under each other; marks are given line by line.
- Draw a small table of i and j to avoid slips in descending patterns.
- Only rectangular and right-angled triangle patterns are in the syllabus.
- In series questions, show the accumulator and the term updates for each pass.
Practice questions on Pattern Programs and Series with 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
How many times does the inner body run?
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 4; j++) { ... }
(a) 7 (b) 12 (c) 4 (d) 3Hint
Rows times columns.
Answer
(b) 12
Q2 · 1 mark · Very short answerBoard level
In a nested for loop, which variable stays unchanged while the inner loop runs one full round?
Hint
Outer or inner?
Answer
The outer loop variable.
Q3 · 2 marks · Short answerHarder board level
Write the output:
for (int i = 1; i <= 3; i++) { for (int j = i; j <= 3; j++) System.out.print(j); System.out.println(); }Hint
The inner loop starts from i.
Answer
123
23
3Q4 · 3 marks · Short answerHarder board level
Write nested loops that print the following pattern:
4 3 2 1
4 3 2
4 3
4Hint
Every row starts at 4 and counts down; the stopping value rises with the row.
Answer
for (int i = 1; i <= 4; i++) {
for (int j = 4; j >= i; j--)
System.out.print(j + " ");
System.out.println();
}Q5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): In a nested for loop, the inner loop completes all its passes for each pass of the outer loop.
Reason (R): For a fixed inner count the total number of inner passes equals outer passes multiplied by inner passes.
(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
Does R describe the total or explain the order?
Answer
(b) Both A and R are true but R is not the correct explanation of A. R gives the total count, while A describes the order of execution.
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 loop runs faster in a nested pair?Show answer
The inner loop; it completes for every pass of the outer loop.
2.What does println() after the inner loop do?Show answer
Starts a new row.
3.Which triangle is in the syllabus?Show answer
The right-angled triangle.
4.How many inner passes in a 4-row triangle 1,2,3,4?Show answer
10
5.What does break do in a nested loop?Show answer
Leaves the innermost loop only.
6.Are nested while loops in the syllabus?Show answer
No, only nested for loops.
Pattern Programs and Series with Nested for Loops: frequently asked questions
How do I decide the inner loop limit for a triangle?
Use the outer variable: j <= i for an ascending triangle, and count i downwards for a descending one.
Why does my pattern print on one line?
The println() call is missing after the inner loop.
Do break and continue affect the outer loop?
No, they act on the loop in which they are written.