Many tasks have two dimensions: rows and columns of a table, seats in rows of a theatre, pixels in an image. A loop inside another loop, a nested loop, handles exactly this kind of work.
How nested loops work
Intuition
A clock is a nested loop. For every hour (outer loop), the minute hand goes all the way round (inner loop). The inner loop completes fully for every single step of the outer loop.
A nested loop is a loop placed inside the body of another loop. For each repetition of the outer loop, the inner loop runs through all of its repetitions. If the outer loop runs m times and the inner loop n times, the inner body runs m × n times in total.
For printing patterns, use the outer loop for rows and the inner loop for columns , and print a newline after the inner loop finishes each row.
Example 3.12 Number triangle
Goal. Print the pattern:
Required pattern Copy 1
1 2
1 2 3
1 2 3 4
Step. There are 4 rows, so the outer loop runs i = 1 to 4.
Step. Row i contains the numbers 1 to i, so the inner loop runs j = 1 to i and prints j.
Step. After the inner loop finishes a row, print a newline.
C · triangle.c Copy #include <stdio.h>
int main( void )
{
int i, j;
for (i = 1 ; i <= 4 ; i ++ ) { /* one row per i */
for (j = 1 ; j <= i; j ++ ) /* i numbers in row i */
printf( "%d " , j);
printf( " \n " ); /* end of the row */
}
return 0 ;
}
Output Copy 1
1 2
1 2 3
1 2 3 4
Step. Count the work: rows of 1 + 2 + 3 + 4 = 10 numbers in total.
Result. The program prints the four-row triangle exactly as required.
Correction · Example 3.9: pattern program
Study material says: Example 3.9 (§3.3.4) prints the pattern with printf("%d\n", i); before the inner loop and no newline after it.
Correct version: print the numbers with the inner loop, then print \n after the inner loop, as in the program above.
Why: the original prints i and a newline at the start of each row, and the last row has no newline, so its output is five lines: 1, 1 2, 1 2 3, 1 2 3 4, and a repeated 1 2 3 4 (with tabs). Running it confirms the extra line.
Example 3.13 Multiplication table
Goal. Print the multiplication table from 1 × 1 to 5 × 5.
Step. The outer loop chooses the row i; the inner loop chooses the column j; each cell is i * j.
Step. Use %4d to print each value in a 4-character column so the table lines up.
C · table.c Copy #include <stdio.h>
int main( void )
{
for ( int i = 1 ; i <= 5 ; i ++ ) {
for ( int j = 1 ; j <= 5 ; j ++ )
printf( "%4d" , i * j);
printf( " \n " );
}
return 0 ;
}
Output Copy 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Result. The outer loop runs 5 times and the inner loop 5 times for each, so 25 products are printed in 5 aligned rows.
Key idea
In a nested loop the inner loop completes all its repetitions for every single repetition of the outer loop; for patterns, outer = rows, inner = columns, newline after the inner loop.
TEE Exam note
Pattern questions (triangles, pyramids, tables) are common. Write a small table of row number, number of items and what each item is; the inner loop’s limit usually depends on the row number i. Show the expected output with the program.
Common mistake
Printing the newline inside the inner loop, which puts every number on its own line.
Common mistake
Using the same variable for both loops, so the inner loop corrupts the outer counter.
Key takeaways
A nested loop is a loop inside another loop’s body.
The inner loop runs completely for each repetition of the outer loop: m × n repetitions in total.
For patterns: outer loop = rows, inner loop = columns, newline after each row.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice An outer loop runs 4 times and its inner loop runs 3 times per outer repetition. How many times does the inner body run?
A 7 B 12 C 3 D 4
Check answer
Show answer Answer: B. 12
The inner loop completes 3 repetitions for each of the 4 outer repetitions: 4 × 3 = 12. Adding them (7) is the common mistake.
Question 2 · multiple choice What does this program print?
C · stars.c Copy #include <stdio.h>
int main( void )
{
for ( int i = 1 ; i <= 3 ; i ++ ) {
for ( int j = 1 ; j <= i; j ++ )
printf( "*" );
printf( " \n " );
}
return 0 ;
} A Three lines of three stars B One line of six stars C Lines of 1, 2 and 3 stars D Lines of 3, 2 and 1 stars
Check answer
Show answer Answer: C. Lines of 1, 2 and 3 stars
Row i prints i stars, so the rows have 1, 2 and 3 stars. A decreasing triangle would need the inner limit to fall as i rises.
Question 3 · multiple choice Where should the newline be printed to finish each row of a pattern?
A Inside the inner loop B Before the outer loop C After the outer loop only D After the inner loop, inside the outer loop
Check answer
Show answer Answer: D. After the inner loop, inside the outer loop
Each row ends when the inner loop has printed all its items, so the newline belongs right after the inner loop, once per outer repetition.
Question 4 · multiple choice In a nested loop, what happens to the inner loop variable at the start of each outer repetition?
A It is set to its initial value again B It keeps its last value C It is set to the outer variable D It becomes 0 automatically
Check answer
Show answer Answer: A. It is set to its initial value again
The inner for header’s initialisation runs again every time the inner loop starts, so the inner variable restarts.
Question 5 · multiple choice Which inner-loop header prints a decreasing triangle (4, 3, 2, 1 items per row) when the outer loop is for (i = 1; i <= 4; i++)?
A for (j = 1; j <= i; j++)B for (j = 1; j <= 5 - i; j++)C for (j = i; j <= 4; j--)D for (j = 1; j <= 4; j++)
Check answer
Show answer Answer: B. for (j = 1; j <= 5 - i; j++)
For i = 1, 2, 3, 4, the limit 5 − i gives 4, 3, 2 and 1 items. The first option gives an increasing triangle; the third never stops counting down correctly.
Question 6 · short answer
Write a program fragment that prints a 3 × 4 rectangle of # characters and state how many # characters it prints.
Show worked answer for (int r = 1; r <= 3; r++) { for (int c = 1; c <= 4; c++) printf("#"); printf("\n"); }. The outer loop gives 3 rows, the inner loop 4 characters per row, and the newline ends each row. It prints 3 × 4 = 12 # characters in 3 lines of 4.