All lessons

4. Loops

Nested loops

0 of 5 activities0%

Reading 1

Loops inside loops

Open

The inner loop finishes all of its iterations for each single step of the outer loop.

Nested loops are common for tables, grids, and patterns. Watch the counters: the outer variable changes slowly; the inner one resets often.

If the outer loop runs R times and the inner runs C times each pass, the inner body runs about R * C times.

for (int r = 1; r <= 3; r++) {
    for (int c = 1; c <= 2; c++) {
        cout << r << "," << c << " ";
    }
    cout << endl;
}

Check 2

How many prints

Open

Outer runs 3 times, inner runs 4 times each. How many times does the inner cout run?

Fill in 3

Structure

Open

A loop placed inside another loop is called a

Try it 4

Star rows

Open

Print a triangle of stars. Change the height.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Multiplication row

Open

Read n. Print the products n*1, n*2, ..., n*5 separated by spaces on one line (no trailing space required beyond normal cout spacing — print each with a space after, ending with newline). Example for 3: 3 6 9 12 15

main.cpp
Loading editor…