MCS-201 · Unit 3

Decision and Loop Control Statements

Section 6 of 7

Block 1 · Unit 3Decision and Loop Control Statements8 min · Section 6 of 7

3.6Nested Loops

Covers IGNOU MCS-201 §3.3.4

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

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.

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?

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
#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;
}
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?

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?

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++)?

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.

↑ ↓ move↵ openTab switch type