Nexus Learn

Lecture 15: Nested Loops

Lecture Slides CS-307 Programming Fundamentals 7 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Slide 2

Quick Recap of Lecture 14

5 minutes · homework check

Homework 2

  • int found = 0;
  • for (int n = 100; n <= 200; n++) {
  • if (n % 3 == 0 && n % 7 == 0) {
  • cout << n << " ";
  • found++;
  • if (found == 5) break;
  • }
  • }
  • Today's question
  • For every 1 hour, the minute hand completes 60 minutes. How many minutes in 24 hours?

Output

105 126 147 168 189

Recap

Programming Fundamentals · Lecture 15

Slide 3

Today's Plan

50 minutes · 8 parts

Recap & homework check

5 min

Learning outcomes

3 min

How a nested loop runs

10 min

Tables & grids

8 min

Star & number patterns

12 min

Class activity

7 min

Traps & recap quiz

4 min

Homework & next lecture

1 min

Programming Fundamentals · Lecture 15

Slide 4

Learning Outcomes (SLOs)

By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)

Trace a nested loop and work out how many times the inner body runs

SLO 15.1 → CLO4 · C3

Write nested loops to print tables, grids and star patterns

SLO 15.2 → CLO4 · C3

SLO 15.3 → CLO1 · C4

Design a nested loop by deciding what the outer and the inner loop control

  • CLO1 Identify the necessary properties of good problem-solving techniques.
  • CLO4 Describe the syntax and semantics of the C++ programming language.

Programming Fundamentals · Lecture 15

Slide 5

A Loop Inside a Loop

For EVERY round of the outer loop, the inner loop runs completely

Shape

  • Clock example
  • Outer loop: hours 0 … 23
  • Inner loop: minutes 0 … 59
  • Minutes restart at 0 every hour
  • Total: 24 × 60 = 1440 rounds
  • for (start; test; update) { // outer
  • for (start; test; update) { // inner
  • // inner body
  • }
  • // after one full inner loop
  • }
  • Remember
  • The inner loop starts again from its start value in every round of the outer loop.

Module 1 · How a Nested Loop Runs

Programming Fundamentals · Lecture 15

Slide 6

Tracing a Nested Loop

3 rows × 4 columns = 12 rounds of the inner body

nested_trace.cpp (main part)

  • int rounds = 0;
  • for (int i = 1; i <= 3; i++) { // outer loop: rows
  • for (int j = 1; j <= 4; j++) { // inner loop: columns
  • cout << "(" << i << "," << j << ") ";
  • rounds++;
  • }
  • cout << endl; // after each row
  • }

j restarts at 1 for every new i.

Output

  • (1,1) (1,2) (1,3) (1,4)
  • (2,1) (2,2) (2,3) (2,4)
  • (3,1) (3,2) (3,3) (3,4)
  • Inner body ran 12 times

Module 1 · How a Nested Loop Runs

Programming Fundamentals · Lecture 15

Slide 7

How Many Times Does the Inner Body Run?

Count the rounds before you run the program

  • Fixed inner loop
  • Multiply: outer × inner.
  • Inner depends on i
  • Add the rounds of each row.

Module 1 · How a Nested Loop Runs

Programming Fundamentals · Lecture 15

Slide 8

Multiplication Table

Outer loop = row, inner loop = column, value = row × col

times_table.cpp (main part)

Output

  • for (int row = 1; row <= 5; row++) {
  • for (int col = 1; col <= 5; col++) {
  • cout << setw(4) << row * col;
  • }
  • cout << endl;
  • }
  • 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
  • Look at
  • Row 3 is the table of 3.
  • The diagonal holds squares: 1 4 9 16 25.

Needs #include <iomanip> for setw.

Module 2 · Tables & Grids

Programming Fundamentals · Lecture 15

Slide 9

Real-World Grid: Exam Hall Seating Plan

Rows A, B, C × seats 1 to 4

seats.cpp (main part)

Output

  • int rows = 3, seats = 4;
  • for (char r = 'A'; r < 'A' + rows; r++) {
  • for (int s = 1; s <= seats; s++) {
  • cout << r << s << " ";
  • }
  • cout << endl;
  • }
  • cout << "Total seats: " << rows * seats << endl;
  • A1 A2 A3 A4
  • B1 B2 B3 B4
  • C1 C2 C3 C4
  • Total seats: 12
  • char loop
  • 'A' + 1 is 'B'. A char can be a loop counter.
  • Real-world
  • Exam hall, cinema hall or a bus: rows (outer) and seats in a row (inner).

Module 2 · Tables & Grids

Programming Fundamentals · Lecture 15

Slide 10

Star Patterns: Rectangle and Triangle

Only the inner loop's limit changes

stars_rect.cpp

stars_triangle.cpp (n = 5)

  • for (int i = 1; i <= 3; i++) {
  • for (int j = 1; j <= 5; j++) {
  • cout << "* ";
  • }
  • cout << endl;
  • }
  • for (int i = 1; i <= n; i++) {
  • for (int j = 1; j <= i; j++) { // i stars
  • cout << "* ";
  • }
  • cout << endl;
  • }

Output

Output

  • * * * * *
  • * * * * *
  • * * * * *
  • *
  • * *
  • * * *
  • * * * *
  • * * * * *

Module 3 · Star & Number Patterns

Programming Fundamentals · Lecture 15

Slide 11

A Method for Designing Any Pattern

Make a small table first, then write the loops

  • Five steps
  • 1 How many rows? → outer loop
  • 2 Items in each row? → write them down
  • 3 Rule using i → inner limit
  • 4 What to print? (*, j or i)
  • 5 endl after the inner loop

Pattern with n = 4 → j <= n - i + 1

Outer loop = rows (lines). Inner loop = items in one row.

Module 3 · Star & Number Patterns

Programming Fundamentals · Lecture 15

Slide 12

Number Triangle and Inverted Triangle

Change what you print, or change the limit

number_triangle.cpp

inverted.cpp

  • for (int i = 1; i <= 4; i++) {
  • for (int j = 1; j <= i; j++) {
  • cout << j << " ";
  • }
  • cout << endl;
  • }
  • int n = 4;
  • for (int i = 1; i <= n; i++) {
  • for (int j = 1; j <= n - i + 1; j++) {
  • cout << "* ";
  • }
  • cout << endl;
  • }

Output

Output

  • 1
  • 1 2
  • 1 2 3
  • 1 2 3 4
  • * * * *
  • * * *
  • * *
  • *

Module 3 · Star & Number Patterns

Programming Fundamentals · Lecture 15

Slide 13

CLASS ACTIVITY · 7 MIN · IN PAIRS

Predict, Then Write

Questions 1–2: predict without running. Question 3: write the loops using the five steps.

Tasks

Hints

  • 1. for i = 1…4, for j = 1…i: c++. What is c?
  • 2. What does this print?
  • for i = 1…4 { for j = 1…3: cout << i; endl }
  • 3. Write loops that print a 3 × 4 board:
  • 0101
  • 1010
  • 0101
  • Q1: add the rounds of each row
  • Q2: print i, not j
  • Q3: (i + j) % 2

Slide 14

Traps

endl in the wrong place · reusing the counter · break leaves one loop

1 endl_inside_bug.cpp (inner part)

3 break_inner.cpp

  • for (int j = 1; j <= 3; j++) {
  • cout << "* " << endl; // wrong loop!
  • }
  • for (int i = 1; i <= 3; i++) {
  • for (int j = 1; j <= 5; j++) {
  • if (j == 3) {
  • break; // inner only
  • }
  • cout << i << j << " ";
  • }
  • cout << "| ";
  • }

2 reuse_i_bug.cpp → prints Hi Hi once

  • int i;
  • for (i = 1; i <= 3; i++) {
  • for (i = 1; i <= 2; i++) { // same i!
  • cout << "Hi ";
  • }

Output

11 12 | 21 22 | 31 32 |

Use different names (i, j) · endl after the inner loop · break leaves one loop only.

Programming Fundamentals · Lecture 15

Slide 15

Quick Recap

Answer aloud

Q1

Outer loop 5 rounds, inner loop 6 rounds. How many times does the inner body run?

Q2

In a triangle pattern, what is the limit of the inner loop?

Q3

Where does endl go in a pattern program?

Q4

A break inside the inner loop — which loop ends?

Programming Fundamentals · Lecture 15

Slide 16

Homework

Print the tables of 2, 3 and 4 (1 to 10), one table per line, using nested loops.

Print the inverted number triangle for n = 5: 1 2 3 4 5 / 1 2 3 4 / … / 1.

Print a pyramid of 4 rows (1, 3, 5, 7 stars, centred). Hint: one loop for spaces, one for stars.

Without running: for i = 1…5, for j = i…5. How many times does the inner body run?

Next lecture: Loop-based problem solving: digits, primes and series

← All CS-307 material