Nested while, do-while and for loops

A nested loop is a loop inside a loop: the inner one finishes ALL its rounds for each single step of the outer one, exactly like a clock's minute hand, and it powers tables, grids and the exam's beloved star patterns.

11 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Rows and columns want two loops

The marks program now loops through 60 students. New requirement: each student has 4 subjects.

So the repetition itself repeats: FOR each student, FOR each subject, read one mark. A loop inside a loop.

Nested loops also power the question every VNSGU paper seems to love: print this star pattern. By the end of this page, patterns become a formula, not a puzzle.

Theory

The clock

Watch a clock: for the hour hand to move ONE step, the minute hand completes sixty full steps. Outer loop = hour hand, inner loop = minute hand. The inner loop lives its entire life, start to finish, inside every single tick of the outer one. One hour-tick, sixty minute-ticks; two hour-ticks, one hundred twenty. Counts multiply.

Theory

Nested loops, formally

Any loop may sit inside any other (for in for, while in for...). The law:

For EACH outer iteration, the inner loop runs COMPLETELY.

for (i = 1; i <= 3; i++) /* students */

for (j = 1; j <= 4; j++) /* subjects */

...read mark...

The body runs 3 × 4 = 12 times: i=1 with j=1,2,3,4; then i=2 with j=1,2,3,4; then i=3 likewise. Two rules of hygiene: different counters (i and j), and know which loop owns which job: outer = rows, inner = columns.

Practical

The exam classic: a star triangle

#include <stdio.h>

int main() {
    int i, j;
    for (i = 1; i <= 4; i++) {      /* rows    */
        for (j = 1; j <= i; j++) {  /* columns */
            printf("*");
        }
        printf("\n");               /* end the row */
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Predict the pattern

Trace the code: for each i from 1 to 4, the inner loop prints stars while j <= i, then a newline. Draw the exact output on paper first.

Show the answer

*

**

***

****

Row i gets exactly i stars because the inner condition is j <= i: row 1 allows one star, row 4 allows four. Every star-pattern question is this one skeleton with a different inner condition, decode j's limit and you have decoded the pattern.

Quiz

In the nested loops for(i=1; i<=3; i++) { for(j=1; j<=4; j++) { body } }, how many times does the body execute?

  1. 12 times: 3 outer × 4 inner
  2. 7 times: 3 + 4
  3. 4 times: the inner loop wins
  4. 3 times: the outer loop wins
Show the answer

12 times: 3 outer × 4 inner

The inner loop runs its full 4 rounds for EACH of the 3 outer rounds: 3 × 4 = 12. Adding (option B) is the common slip, remember the clock: two hours is one hundred twenty minute-ticks, not sixty-two. Multiplication of counts is the single most-tested nested-loop fact.

Quiz

In the star-triangle code, why does printf("\n") sit AFTER the inner loop but INSIDE the outer loop?

  1. Each row's stars finish first, then the row is closed with one newline
  2. It is a style choice; anywhere works the same
  3. The newline must run once per star
  4. C requires \n at the end of every loop
Show the answer

Each row's stars finish first, then the row is closed with one newline

The newline's job is "end this ROW", so it belongs to the outer (row) loop, running once per row. Move it inside the inner loop and every star lands on its own line; move it outside both and all ten stars crowd one line. Placement of \n is exactly where pattern answers go wrong.

Watch out

Where marks leak

Reusing i for both loops: the inner loop mangles the outer counter and the program loops strangely or forever; always i and j. Swapped jobs: outer is rows, inner is columns; reverse them and the pattern comes out transposed. The \n position (see the quiz). In trace questions, write the (i, j) pairs in order: (1,1) (1,2)... nothing untangles nesting faster.

Theory

Where nesting pays off next

Students × subjects is a grid, and Unit 3's two-dimensional arrays store exactly such grids, walked by exactly these two counters (i for rows, j for columns). Even the matrix operations you did in BCA102 become nested loops in C. Master the clock now; the grid lessons will feel pre-learned. First, though: the loop emergency exits, break and continue.

Summary

Key takeaways

  • A nested loop: the inner loop runs COMPLETELY for each outer iteration.
  • Total body runs = outer count × inner count (3×4 = 12), multiply, never add.
  • Outer loop owns rows; inner loop owns columns.
  • Use different counters (i, j); reusing one is a classic bug.
  • Star patterns: row i prints while j <= i; the \n closes each row after the inner loop.
  • Memory hook: the clock, one hour-tick, sixty minute-ticks.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Decision Making and Iterative statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Nested while, do-while and for loops · Computer Programming and Programming Methodology (CPPM) · Gri-Learn