SrcForge

Lesson 12

Looping Statements

Covers loop types, nested loops, break, continue.

Lesson content

What are looping statements?

Loops repeat code while condition stays true.

cout << 1 << '\n';
cout << 2 << '\n';
cout << 3 << '\n';
cout << 4 << '\n';
cout << 5 << '\n';

// vs

for (int i = 1; i <= 5; i++)
{
    cout << i << '\n';
}

Loops shorten and simplify programs.

Teacher calling attendance, repeated for each student.

Why do loops exist?

  • Printing multiplication tables
  • Processing thousands of records
  • Reading input until valid
  • Searching through data
  • Displaying menu options repeatedly

Real-world use cases

  • Games
  • Banking applications
  • Data processing
  • Inventory management
  • Student record systems
  • File processing
  • Menu-driven programs
  • Searching and sorting algorithms

Core concepts

Loop       | Best Used When                  | Condition Check
---------- | ------------------------------- | ----------------------
for        | Number of iterations is known   | Before each iteration
while      | Number of iterations is unknown | Before each iteration
do-while   | Code must execute at least once | After each iteration

1. for loop

Best when iteration count is known upfront.

for (initialization; condition; update)
{
    // Statements
}
  • Initialization: runs once before loop
  • Condition: decides if loop continues
  • Update: changes loop variable each pass

2. while loop

Runs while its condition stays true.

while (condition)
{
    // Statements
}

Condition checked before every iteration.

3. do-while loop

Runs body once, checks condition after.

do
{
    // Statements
}
while (condition);

Nested loops

A loop placed inside another loop.

  • Printing patterns
  • Matrix operations
  • Working with two-dimensional arrays

Loop control statements

break exits the nearest loop immediately.

continue skips rest, jumps to next iteration.

Example 1: for loop

#include <iostream>
using namespace std;

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        cout << i << '\n';
    }

    return 0;
}

Example 2: while loop

#include <iostream>
using namespace std;

int main()
{
    int count = 1;

    while (count <= 5)
    {
        cout << count << '\n';
        count++;
    }

    return 0;
}

Example 3: do-while loop

#include <iostream>
using namespace std;

int main()
{
    int number = 1;

    do
    {
        cout << number << '\n';
        number++;
    }
    while (number <= 5);

    return 0;
}

Example 4: Multiplication table

#include <iostream>
using namespace std;

int main()
{
    int number;

    cout << "Enter a number: ";
    cin >> number;

    for (int i = 1; i <= 10; i++)
    {
        cout << number << " x " << i
             << " = " << number * i
             << '\n';
    }

    return 0;
}

Example 5: Nested loops

#include <iostream>
using namespace std;

int main()
{
    for (int row = 1; row <= 3; row++)
    {
        for (int col = 1; col <= 3; col++)
        {
            cout << "* ";
        }

        cout << '\n';
    }

    return 0;
}

Common mistakes and pitfalls

  • Forgetting to update loop var. Fix: increment or decrement it
  • Using = instead of ==. Fix: use == for comparing
  • Off-by-one errors. Fix: check boundaries carefully
  • Modifying loop var unexpectedly. Fix: update only when needed
  • Using break too often. Fix: let loop finish naturally
// Wrong
int i = 1;

while (i <= 5)
{
    cout << i << '\n';
}
// Correct
int i = 1;

while (i <= 5)
{
    cout << i << '\n';
    i++;
}

Best practices

  • Use for when iterations count is known
  • Use while for condition-based repetition
  • Use do-while for at least one run
  • Keep loop bodies short and focused
  • Avoid modifying counter unexpectedly
  • Minimize nested loops when possible
  • Use clear names like index or row

When not to use this

  • When std::find or for_each fits better
  • Deeply nested loops for complex problems
  • Recursion-friendly problems like tree traversal

Summary

  • Loops repeat code blocks automatically
  • C++ has for, while, do-while loops
  • for fits known iteration counts
  • while fits condition-based repetition
  • do-while runs body at least once
  • Nested loops handle patterns and matrices
  • break exits, continue skips current iteration
  • Ensure conditions eventually turn false

Frequently asked questions

What are looping statements in C++?

They repeat code until condition turns false.

What is the difference between for and while loops?

For fits known counts. While fits unknown counts.

When should I use a do-while loop?

Use when body must run at least once.

What is an infinite loop?

Loop that never stops because condition stays true.

What is the difference between break and continue?

Break exits loop. Continue skips to next iteration.