| Programming Fundamentals | Beginner Level |
This lecture builds on the for loop and helps students understand how to use one loop inside another to create star patterns, number patterns, and tables. It also introduces break and continue.
By the end of this lecture, students will be able to:
break to terminate a loop.continue to skip an iteration.A nested loop is a loop placed inside another loop.
Think of a classroom with 3 rows of students, with 4 students in each row. The outer loop represents the rows, while the inner loop represents the students in each row.
| [Inspiration for Classroom Seating Arrangements | Teach Starter](https://images.openai.com/static-rsc-4/hrwgAfjyfV575J51kC9zTaqDesDuDb_UeOTp2CMrv60uY2w2yJbZOEEUU-P1rxo-VRvo2UsFkX_iqSeaasgl7qz1gloZbd_WqsBaKYwQPsm6Eq5CxFTmKHmqVGEl7SnVLvKkZly1hYBbS7jXBKHbWNRuRrVINmBKOTfuHjlg6tM?purpose=inline) |
The outer loop runs once for each row. For every outer-loop iteration, the inner loop completes all its iterations.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 4; j++)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
Output:
* * * *
* * * *
* * * *
i) runs 3 times, producing 3 rows.j) runs 4 times for every row, printing 4 stars.cout << endl; moves to the next line after each row.Total stars: \(3 \times 4 = 12\)
Outer loop i |
Inner loop j |
Action |
|---|---|---|
| 1 | 1, 2, 3, 4 | Print 4 stars |
| 2 | 1, 2, 3, 4 | Print 4 stars |
| 3 | 1, 2, 3, 4 | Print 4 stars |
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
Output:
*
* *
* * *
* * * *
* * * * *
The key statement is:
for (int j = 1; j <= i; j++)
Unlike the previous example, the inner loop depends on the outer loop’s counter i.
i = 1, print 1 star.i = 2, print 2 stars.i = 3, print 3 stars.* with #.1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= i; j++)
{
cout << i << " ";
}
cout << endl;
}
return 0;
}
Output:
1
2 2
3 3 3
4 4 4 4
Notice the difference between i and j:
i determines the row number and the value printed.j determines how many times that value is printed.Produce this pattern:
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
Hint: Keep the inner loop’s condition independent of i, so every row contains four numbers.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 3; i++)
{
cout << "Table of " << i << endl;
for (int j = 1; j <= 5; j++)
{
cout << i << " x " << j
<< " = " << i * j << endl;
}
cout << endl;
}
return 0;
}
Sample output:
Table of 1
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
Table of 2
2 x 1 = 2
2 x 2 = 4
...
The outer loop selects the table. The inner loop calculates five multiplication results for each table.
break StatementThe break statement immediately terminates the nearest enclosing loop. Execution continues with the statement after that loop.
Microsoft Learn
+1
Real-life example: A student is searching a list for a particular student ID. Once the ID is found, there is no need to continue searching.
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4
i becomes 5, the condition i == 5 is true.break terminates the loop immediately.break executes before cout.i == 7.continue StatementThe continue statement skips the remaining statements in the current iteration and moves to the next iteration. In a for loop, the update expression still executes.
Break and continue – Learn C++
+1
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue;
}
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
When i becomes 5, continue skips the cout statement for that iteration. The loop then moves to the next value, 6.
break and continuebreak |
continue |
|---|---|
| Terminates the loop. | Skips the current iteration. |
| No further iterations occur in that loop. | The loop continues with the next iteration. |
| Useful when a task is complete or a stopping condition is met. | Useful when certain values should be ignored. |
Easy way to remember:
break = Stop.continue = Skip and continue.break in Nested LoopsA break inside an inner loop terminates only the inner loop. The outer loop continues normally.
Programiz
+1
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 4; j++)
{
if (j == 3)
{
break;
}
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output:
1 2
1 2
1 2
The inner loop stops whenever j reaches 3. The outer loop still creates all three rows.
Practice tracker
0/6 completed
Basic
1. Rectangle of stars
Use nested loops to print 4 rows, with 6 stars in each row.
Basic
2. Number triangle
Print a triangle where row 1 contains 1, row 2 contains 1 2, and so on up to row 6.
Intermediate
3. Multiplication tables
Print the multiplication tables from 2 to 5, each up to 10.
Intermediate
4. Skip multiples
Print numbers from 1 to 50 but use continue to skip multiples of 3.
Intermediate
5. Stop at a value
Print numbers from 1 to 100 but use break to stop when the counter reaches 25.
Challenge
6. User-defined pattern
Ask the user for the number of rows and print a right-angle triangle of stars.
1. What does a nested loop mean?
A loop inside another loop
A loop without a condition
A loop that executes only once
2. What does `break` do?
Skips one output
Stops the nearest enclosing loop
Restarts the program
3. What does `continue` do in a `for` loop?
Ends the entire program
Skips the current iteration and proceeds to the next
Repeats the same value forever
4. If the outer loop runs 3 times and the inner loop prints 4 stars each time, how many stars are printed?
7
12
16
Check Answers
break stops the nearest enclosing loop.continue skips the current iteration.Recommended next lesson: Loop applications and problem-solving — factorials, number sums, digit counting, and menu-driven programs.