C Tutorial

C Nested Loops

A loop inside a loop walks rows and columns: tables, grids, and multiplication.

A loop inside a loop

Nested loops means the body of one loop contains another loop. The outer loop runs once per row. For each of those passes, the inner loop runs all the way through. If the outer loop has 3 passes and the inner loop has 4, the inner body runs 12 times.

That product is the point. One loop walks a line. Two loops walk a rectangle: seats in a theater, cells in a table, pairs of numbers. Keep the counters on different names. This tutorial uses row andcol, or i and j.

Print every cell of a grid

This program does not store a table. It only prints coordinates. The outer loop picks the row. The inner loop prints every column on that row, then a newline starts the next line. Read the output as a 3 by 4 grid.

Example

#include <stdio.h>

int main(void) {
  int row;
  int col;
  for (row = 0; row < 3; row++) {
    for (col = 0; col < 4; col++) {
      printf("(%d,%d) ", row, col);
    }
    printf("\n");
  }
  return 0;
}

Run this in /c/try. gcc compiles it there —. Change3 or 4 and watch the rectangle grow. The inner loop restarts fromcol = 0 on every new row.

A multiplication table

Each cell is the product of its row number and its column number. The outer loop is the left factor. The inner loop is the right factor. A newline after the inner loop finishes one row of the table.

Example

#include <stdio.h>

int main(void) {
  int row;
  int col;
  for (row = 1; row <= 5; row++) {
    for (col = 1; col <= 5; col++) {
      printf("%4d", row * col);
    }
    printf("\n");
  }
  return 0;
}

The loops count from 1 this time because people read a times table that way. Array indexes still start at 0; this chapter is about nested repetition, not storage. %4d prints each product in a four-character field so the columns line up.

Read the counters the same way every time

NameJob
Outer counterPicks the row. Changes slowly.
Inner counterPicks the column. Resets on every outer pass.
Inner bodyRuns once per cell.

Declare each counter in its own loop. Then col is the inner name and row is the outer name. Reusing one name for both loops is a common source of off-by-one bugs.

A triangle of stars

The inner loop does not have to run a fixed number of times. Here it runs once on the first row, twice on the second, and so on. The inner bound depends on the outer counter.

Example

#include <stdio.h>

int main(void) {
  int row;
  int col;
  for (row = 1; row <= 5; row++) {
    for (col = 1; col <= row; col++) {
      printf("*");
    }
    printf("\n");
  }
  return 0;
}

break leaves only the inner loop

break ends the loop it sits in. Inside the inner loop, it stops that row and the outer loop continues with the next row. It does not jump out of both loops. If you need to stop the whole nest, set a flag and test it in the outer condition, or return from a function.

A nested loop with a large inner count is still a product. Ten thousand times ten thousand is a hundred million inner-body runs. Keep the bounds small while you learn, and know why the inner work repeats.

Nested loops print a table. They also fill a 2D array and read one. Next: const, so names you only mean to read stay that way.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Maths

A multiplication grid

Times tables are a Cartesian product: every r with every c. The inner loop finishes a row. The outer loop starts the next. %4d keeps the columns lined up.

Example

#include <stdio.h>

int main(void) {
  int r, c;
  for (r = 1; r <= 4; r++) {
    for (c = 1; c <= 4; c++) {
      printf("%4d", r * c);
    }
    printf("\n");
  }
  return 0;
}

Chemistry

Every mix in a small design

Two acid concentrations and three temperatures give six experiments. Nested loops walk every pair. That is a factorial design with two factors, not a reaction mechanism.

Example

#include <stdio.h>

int main(void) {
  int acid, temp;
  for (acid = 1; acid <= 2; acid++) {
    for (temp = 20; temp <= 40; temp += 10) {
      printf("acid %d @ %d C\n", acid, temp);
    }
  }
  return 0;
}

FAQ: C Nested Loops

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c nested loops examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c nested loops in this C C lesson (C Nested Loops).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.