Skip to content
Computer ApplicationsICSE Class 9 · Nested for Loops

Row and Column Patterns with Numbers, Characters and Spaces Using Nested for Loops (ICSE Class 9)

Updated 18 October 2026 · Fact-checked

In a nested for loop the outer loop controls the rows and the inner loop controls the columns. For each pass of the outer loop the inner loop runs completely, so a loop of r rows and c columns runs the inner body r times c times. The inner loop prints with print(); a println() after it ends the row. The limit of the inner loop decides the shape: a fixed limit gives a rectangle, the outer variable as the limit gives a triangle.

The outer loop picks the row; the inner loop restarts each row and stops at i, so row i gets i symbols and 5 rows give 15 runs.

Illustration drawn for QuestionBuddy.

Understand Row and Column Patterns with Numbers, Characters and Spaces

How a nested loop works.
for (i = 1; i <= r; i++) // rows
{
for (j = 1; j <= c; j++) // columns
System.out.print(...);
System.out.println(); // end of row
}
The outer variable changes once per row; the inner variable restarts for every row. The inner body runs r x c times.

Shape rules.

  • Rectangle: the inner limit is fixed (j <= c).
  • Increasing triangle: the inner limit grows with the row (j <= i).
  • Decreasing triangle: the inner limit shrinks (j <= n - i + 1, or the outer loop counts down).
  • Right-aligned shapes: one inner loop prints the spaces (n - i of them), another prints the symbols.
  • What is printed decides the content: j gives 1 2 3, i gives 1 then 22 then 333, the character (char) ('A' + i) gives letters.

Counting iterations. For a triangle with j from 1 to i and i from 1 to n, the inner body runs 1 + 2 + ... + n times; for n = 5 this is 15. For rows 1 to 3 and columns 1 to 4 it is 12.

Tracing method. Write a table with a row for each value of i and show what the inner loop prints, then write each line of output.

Characters. A char plus an int is an int, so cast back: (char) ('A' + i).

Neighbouring topics: series programs and break and continue inside nested loops are on the next page of this chapter.

Row and Column Patterns with Numbers, Characters and Spaces: key facts

Iterations
rows times columns for a rectangle | 1 + 2 + ... + n for a triangle
Inner body count.
Row end
println() goes after the inner loop | not inside it
Otherwise every symbol is on its own line.
Right-aligned triangle
  • spaces: n - i
  • symbols: i
  • on row i
Two inner loops.
Letter of the row
(char) (65 + i) gives A, B, C for i = 0, 1, 2 | cast back from int
A is 65.

How to answer Row and Column Patterns with Numbers, Characters and Spaces questions

Use this routine for pattern questions.

  1. 1Decide the number of rows and write what each row looks like.
  2. 2Choose the outer loop for the rows.
  3. 3Decide how many items each row has in terms of the row variable and write the inner loop.
  4. 4Decide what to print (a symbol, i, j or a character).
  5. 5Put println() after the inner loop and trace the first two rows to check.

Row and Column Patterns with Numbers, Characters and Spaces 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.

A seating plan

Suppose a hall has 3 rows of seats with 4 seats in each row, and each ticket shows the row number and the seat number.

How many tickets are printed and what does the third ticket of row 2 show?

Try it first, then show the solutionSolution
  1. The outer loop gives the rows 1 to 3 and the inner loop gives the seats 1 to 4.
  2. Tickets: 3 x 4 = 12.
  3. Row 2, seat 3.

Answer: 12 tickets; the third ticket of row 2 shows row 2, seat 3.

Memory hook: Rows outside, seats inside.

Tricks to remember Row and Column Patterns with Numbers, Characters and Spaces

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

  • Rows outside, columns inside

    The outer loop is the row; the inner loop is the column.

    Use it when: Structure.

  • Println ends the row

    Put println() after the inner loop.

    Use it when: Layout.

  • Limit of the inner loop is the shape

    Fixed gives a rectangle; row variable gives a triangle.

    Use it when: Shapes.

Row and Column Patterns with Numbers, Characters and Spaces: solved examples

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

Example 1: Iterations · 1 mark · MCQBoard level

How many times does the inner body run?

for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 3; j++)
System.out.print("*");

(a) 8 (b) 15 (c) 5 (d) 3

Try it first, then show the solutionSolution
  1. The inner loop runs 3 times for each of the 5 rows: 5 x 3 = 15.

Answer: (b) 15

Example 2: Decreasing triangle · 3 marks · Short answerHarder board level

Show the output:

for (int i = 4; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
System.out.print("*");
System.out.println();
}
Try it first, then show the solutionSolution
  1. i = 4 prints four stars and ends the line. (1 mark)
  2. i = 3, 2, 1 print three, two and one star. (1 mark)
  3. The output is a decreasing triangle of 4, 3, 2 and 1 stars. (1 mark)

Answer: ****
***
**
*

Example 3: Letter pattern · 3 marks · Short answerHarder board level

Show the output:

for (int i = 0; i < 4; i++)
{
for (int j = 0; j <= i; j++)
System.out.print((char) ('A' + i));
System.out.println();
}
Try it first, then show the solutionSolution
  1. Row i = 0 prints one A, since j runs from 0 to 0. (1 mark)
  2. Row i = 1 prints BB, row i = 2 prints CCC. (1 mark)
  3. Row i = 3 prints DDDD. (1 mark)

Answer: A
BB
CCC
DDDD

Example 4: Case study: seat labels · 4 marks · Case-basedHOTS / competency

A booking program prints seat labels:

for (int r = 1; r <= 3; r++)
{
for (int c = 1; c <= 4; c++)
System.out.print("R" + r + "C" + c + " ");
System.out.println();
}

(i) How many labels are printed? (1)
(ii) Write the first line of output. (1)
(iii) Change one line so that row r has only r labels. (2)

Try it first, then show the solutionSolution
  1. (i) 3 rows x 4 columns = 12 labels.
  2. (ii) R1C1 R1C2 R1C3 R1C4
  3. (iii) Change the inner condition to c <= r; row 1 then has one label, row 2 two labels and row 3 three labels.

Answer: (i) 12 (ii) R1C1 R1C2 R1C3 R1C4 (iii) use c <= r

Example 5: Right-aligned triangle · 3 marks · Short answerHarder board level

Show the output for n = 4:

for (int i = 1; i <= 4; i++)
{
for (int s = 1; s <= 4 - i; s++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
System.out.println();
}
Try it first, then show the solutionSolution
  1. Row i prints 4 - i spaces first: 3, 2, 1, 0 spaces. (1 mark)
  2. Then i stars: 1, 2, 3, 4. (1 mark)
  3. The result is a triangle aligned to the right. (1 mark)

Answer: *
**
***
****

Where students lose marks in Row and Column Patterns with Numbers, Characters and Spaces

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

  • Mistake: println inside the inner loop

    Every symbol is on a new line.

    Wrong working

    Each star on its own line

    Right working

    println() after the inner loop

    Do this: Rows end after the inner loop.

    Usually costs: 2 marks

  • Mistake: Resetting the wrong variable

    The inner variable is declared outside.

    Wrong working

    j declared before the outer loop and not reset

    Right working

    Initialise j in the inner for header

    Do this: The inner loop must restart for every row.

    Usually costs: 2 marks

  • Mistake: Forgetting to cast a char

    A char plus an int is an int.

    Wrong working

    print('A' + i) prints a number

    Right working

    print((char) ('A' + i))

    Do this: Cast back to char.

    Usually costs: 1 mark

Exam tips

  • Always trace the first two rows to confirm your pattern.
  • State the number of times the inner body executes when asked about iterations.
  • Write both the loops with the correct limits before choosing what to print.
  • Keep println() after the inner loop.

Practice questions on Row and Column Patterns with Numbers, Characters and Spaces

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

    Where should System.out.println() be placed to end each row in a nested loop pattern?
    (a) inside the inner loop (b) after the inner loop, inside the outer loop (c) before the outer loop (d) after the outer loop

    Hint

    One new line per row.

    Answer

    (b) after the inner loop, inside the outer loop

  2. Q2 · 1 mark · Very short answerBoard level

    How many times does the body run in for (i = 1; i <= 4; i++) for (j = 1; j <= 4; j++)?

    Hint

    Rows times columns.

    Answer

    16

  3. Q3 · 3 marks · Short answerHarder board level

    Show 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

    1 2 3
    2 3
    3

  4. Q4 · 3 marks · Short answerHarder board level

    Write nested loops that print
    1 2 3 4
    1 2 3
    1 2
    1

    Hint

    The inner limit decreases.

    Answer

    for (int i = 4; i >= 1; i--)
    {
    for (int j = 1; j <= i; j++)
    System.out.print(j + " ");
    System.out.println();
    }

  5. Q5 · 4 marks · Short answerHOTS / competency

    For (i) for (i = 1; i <= 4; i++) for (j = i; j <= 4; j++) and (ii) the multiplication pattern for i = 1 to 3 and j = 1 to 3 printing i * j and a space, find the number of inner executions in (i) and show the output of (ii).

    Hint

    Count 4 + 3 + 2 + 1.

    Answer

    (i) i = 1 gives 4 passes, i = 2 gives 3, i = 3 gives 2, i = 4 gives 1: 10 executions. (ii) 1 2 3 on the first line, 2 4 6 on the second, 3 6 9 on the third.

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 gives the rows?Show answer

    The outer loop.

  2. 2.How often does the inner loop restart?Show answer

    Once for every pass of the outer loop.

  3. 3.Inner body executions for 3 rows and 4 columns?Show answer

    12

  4. 4.Which statement ends a row?Show answer

    System.out.println();

  5. 5.Limit of the inner loop for an increasing triangle?Show answer

    j <= i

  6. 6.How to print the letter C from i = 2?Show answer

    (char) ('A' + i)

Row and Column Patterns with Numbers, Characters and Spaces: frequently asked questions

Can a nested loop have three levels?

Yes, but patterns in Class IX use two.

Why do patterns use print() inside?

print() keeps the cursor on the same row; println() moves to the next row.

Does the inner variable need a different name?

Yes, i and j must be different variables.