Nexus Learn

Lecture 15: Nested Loops

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

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. A Loop Inside a Loop
  3. 2. Tracing a Nested Loop
  4. 3. Counting the Rounds
  5. 4. Tables and Grids
  6. 5. Star and Number Patterns
  7. 6. Traps
  8. 7. Word Bank (Key Terms)
  9. 8. Self-Check Questions

UNIVERSITY OF AGRICULTURE, FAISALABAD

Department of Computer Science

LECTURE 15 · STUDENT NOTES

Nested Loops

CS-307 Programming Fundamentals · BSCS Semester 1

Week

Slides

Linked practical

Reference ID

6

Lecture15_Nested_Loops.pptx

Practical 10

N15

Learning Outcomes

SLO

By the end of this lecture you can…

Builds CLO

Bloom level

SLO 15.1

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

CLO4: Describe the syntax and semantics of the C++ programming language.

C3

SLO 15.2

Write nested loops to print tables, grids and star patterns

CLO4: Describe the syntax and semantics of the C++ programming language.

C3

SLO 15.3

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

CLO1: Identify the necessary properties of good problem-solving techniques.

C4

How to use these notes

Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste.

1. A Loop Inside a Loop

A nested loop is a loop placed inside the body of another loop (nested means 'placed inside'). For every round of the outer loop, the inner loop runs completely, starting again from its start value. (Slide L15-S05)

Real-world: a clock. The hour is the outer loop and the minutes are the inner loop. For each hour the minutes go from 0 to 59, then the hour moves on and the minutes start again from 0. In one day: 24 × 60 = 1440 minutes.

Shape

for (start; test; update) { // outer

for (start; test; update) { // inner

// inner body

}

// after one full inner loop

}

2. Tracing a Nested Loop

(Slide L15-S06)

nested_trace.cpp

#include <iostream>

using namespace std;

int main() {

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

}

cout << "Inner body ran " << rounds << " times" << endl;

return 0;

}

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

i

j takes the values

Then

1

1 2 3 4 (j = 5 fails)

endl, i++

2

1 2 3 4

endl, i++

3

1 2 3 4

endl, i++

4

—

outer test fails, loop ends

Key idea

j restarts at 1 for every new value of i. The pair (i, j) tells you where you are, like row and column in a table.

3. Counting the Rounds

You can work out how many times the inner body runs before running the program. (Slide L15-S07)

Outer loop

Inner loop

Rounds per row

Total

i = 1 … 3

j = 1 … 4

4, 4, 4

3 × 4 = 12

h = 0 … 23

m = 0 … 59

60 each

24 × 60 = 1440

i = 1 … 4

j = 1 … i

1, 2, 3, 4

1+2+3+4 = 10

i = 1 … 4

j = 1 … 5 - i

4, 3, 2, 1

4+3+2+1 = 10

  • Fixed inner loop: multiply — outer rounds × inner rounds.
  • Inner loop depends on i: add the rounds of each row. For j = 1 … i with n rows the total is 1 + 2 + … + n = n(n + 1) / 2.

4. Tables and Grids

4.1 Multiplication table

Outer loop = row, inner loop = column, each value = row × col. setw(4) from <iomanip> gives every number 4 places so the columns line up. (Slide L15-S08)

times_table.cpp

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

for (int row = 1; row <= 5; row++) {

for (int col = 1; col <= 5; col++) {

cout << setw(4) << row * col;

}

cout << endl;

}

return 0;

}

Output

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

4.2 Exam hall seating plan

The outer counter is a char. Characters are stored as numbers, so r++ moves from 'A' to 'B' to 'C'. (Slide L15-S09)

seats.cpp

#include <iostream>

using namespace std;

int main() {

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;

return 0;

}

Output

A1 A2 A3 A4

B1 B2 B3 B4

C1 C2 C3 C4

Total seats: 12

5. Star and Number Patterns

5.1 Rectangle and right triangle

(Slide L15-S10) Only the inner loop's limit changes: the rectangle uses a fixed limit (5); the triangle uses j <= i because row i has i stars.

stars_rect.cpp

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 5; j++) {

cout << "* ";

}

cout << endl;

}

return 0;

}

Output

* * * * *

* * * * *

* * * * *

stars_triangle.cpp

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Rows: ";

cin >> n;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) { // i stars

cout << "* ";

}

cout << endl;

}

return 0;

}

Session

Rows: 5

*

* *

* * *

* * * *

* * * * *

5.2 A method for designing any pattern

(Slide L15-S11)

1. How many rows (lines)? → this is the outer loop.

2. How many items in each row? Write them down in a small table.

3. Find the rule that links the count to i → this is the inner loop's limit.

4. What is printed: a star, j, or i?

5. Put endl after the inner loop, inside the outer loop.

Pattern (n = 4)

Items per row

Inner limit

Prints

Rectangle

5, 5, 5, 5

j <= 5

*

Right triangle

1, 2, 3, 4

j <= i

*

Inverted triangle

4, 3, 2, 1

j <= n - i + 1

*

Number triangle

1, 2, 3, 4

j <= i

j

Repeated digits 1 / 22 / 333

1, 2, 3, 4

j <= i

i

5.3 Number triangle and inverted triangle

(Slide L15-S12)

number_triangle.cpp

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 4; i++) {

for (int j = 1; j <= i; j++) {

cout << j << " ";

}

cout << endl;

}

return 0;

}

Output

1

1 2

1 2 3

1 2 3 4

inverted.cpp

#include <iostream>

using namespace std;

int main() {

int n = 4;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n - i + 1; j++) { // 4, 3, 2, 1 stars

cout << "* ";

}

cout << endl;

}

return 0;

}

Output

* * * *

* * *

* *

*

6. Traps

(Slide L15-S14)

endl_inside_bug.cpp

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 2; i++) {

for (int j = 1; j <= 3; j++) {

cout << "* " << endl; // wrong loop!

}

}

return 0;

}

Prints six lines with one star each, because endl runs inside the inner loop. Put endl after the inner loop.

reuse_i_bug.cpp

#include <iostream>

using namespace std;

int main() {

int i;

for (i = 1; i <= 3; i++) {

for (i = 1; i <= 2; i++) { // same i!

cout << "Hi ";

}

cout << endl;

}

return 0;

}

Output — only one row instead of three

Hi Hi

Both loops change the same variable. When the inner loop ends, i is 3; the outer i++ makes it 4, so the outer loop stops. Use different names (i and j).

break_inner.cpp

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 5; j++) {

if (j == 3) {

break; // inner only

}

cout << i << j << " ";

}

cout << "| ";

}

cout << endl;

return 0;

}

Output

11 12 | 21 22 | 31 32 |

break leaves only the innermost loop. The outer loop carries on with the next row.

7. Word Bank (Key Terms)

Word

Simple meaning

Nested loop

A loop inside the body of another loop

Outer loop

The loop on the outside; usually controls rows

Inner loop

The loop on the inside; usually controls items in one row

Round

One run of a loop body (also called an iteration)

Pattern

A shape printed with characters, row by row

setw(n)

Gives the next value n places on the screen (needs <iomanip>)

8. Self-Check Questions

1. Outer loop 5 rounds, inner loop 6 rounds: how many times does the inner body run?

2. for i = 1…4, for j = 1…i: c++. What is c?

3. What does this print? for i = 1…4 { for j = 1…3: cout << i; endl }

4. Write loops that print 0101 / 1010 / 0101.

5. Where must endl go in a pattern program?

6. A break inside the inner loop — which loop ends?

Answers: 30; 10; 111 / 222 / 333 / 444; print (i + j) % 2; §6; §6. Homework is on Slide L15-S16.

← All CS-307 material