C++ Loops

Sometimes a program needs to repeat the same task multiple times.

For example, you may want to:

  • Print numbers from 1 to 10
  • Display a message several times
  • Calculate the total of multiple numbers
  • Go through all elements of an array

Instead of writing the same code again and again, C++ provides loops.

The main C++ loops are:

  • for loop
  • while loop
  • do...while loop

The for Loop

A for loop is commonly used when you know how many times you want to repeat something.

Syntax

for (initialization; condition; update) {
    // code to repeat
}

For example:

#include <iostream>
using namespace std;

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

    return 0;
}

Output:

1
2
3
4
5

Here:

  • int i = 1 starts the loop at 1.
  • i <= 5 keeps the loop running while i is 5 or less.
  • i++ increases i by 1 after every iteration.

The while Loop

A while loop repeats code while a condition is true.

Syntax

while (condition) {
    // code to repeat
}

Example:

#include <iostream>
using namespace std;

int main() {
    int i = 1;

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

    return 0;
}

Output:

1
2
3
4
5

The variable i increases after each iteration. When i becomes 6, the condition i <= 5 becomes false and the loop stops.

The do...while Loop

A do...while loop is similar to a while loop, but it runs the code at least once before checking the condition.

Syntax

do {
    // code to repeat
} while (condition);

Example:

#include <iostream>
using namespace std;

int main() {
    int i = 1;

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

    return 0;
}

Output:

1
2
3
4
5

The important difference is that the do...while loop executes its code before checking the condition.

break Statement

The break statement is used to stop a loop immediately.

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            break;
        }

        cout << i << "\n";
    }

    return 0;
}

Output:

1
2
3
4

When i becomes 5, break stops the loop.

continue Statement

The continue statement skips the current iteration and moves to the next one.

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue;
        }

        cout << i << "\n";
    }

    return 0;
}

Output:

1
2
4
5

When i is 3, continue skips that iteration, so 3 is not printed.

Nested Loops

A loop inside another loop is called a nested loop.

For example:

#include <iostream>
using namespace std;

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

        cout << "\n";
    }

    return 0;
}

Output:

* * *
* * *
* * *

Nested loops are commonly used for patterns, tables, and multidimensional data.

A Simple Example

Let’s use a loop to print a multiplication table.

#include <iostream>
using namespace std;

int main() {
    int number = 5;

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

    return 0;
}

Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Without a loop, you would need to write ten separate cout statements. The loop makes the program much shorter and easier to manage.

Key Points to Remember

  • Loops are used to repeat code.
  • Use a for loop when the number of repetitions is known.
  • Use a while loop when repetition depends on a condition.
  • A do...while loop runs at least once.
  • break stops a loop.
  • continue skips the current iteration.
  • A loop inside another loop is called a nested loop.

Mini Quiz

0%

C++ PROGRAMMING - QUIZ 7

1 / 5

Q1. Which C++ loop is commonly used when you know how many times you want to repeat something?

2 / 5

Q2. Which loop executes its code at least once before checking the condition?

3 / 5

Q3. What does the break statement do inside a loop?

4 / 5

Q4. What does the continue statement do?

5 / 5

Q5. What is a loop inside another loop called?

Your score is

The average score is 0%

0%

Mini Project

Mini Project: Number Pattern Generator

Question:

Write a C++ program that uses loops to print a simple number pattern.

  • Ask the user to enter the number of rows.
  • Use a nested for loop to print the pattern.
  • Each row should contain numbers starting from 1.
  • The number of values should increase with each row.
  • Use \n to move to the next row.
  • Use continue to skip printing the number 3.
  • Display the final pattern on the screen.
View Answer Code
#include <iostream>
using namespace std;

int main() {
    int rows;

    cout << "Enter number of rows: ";
    cin >> rows;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            if (j == 3) {
                continue;
            }

            cout << j << " ";
        }

        cout << "\n";
    }

    return 0;
}

What’s Next?

In the next chapter, we will learn about C++ Arrays and see how to store multiple values of the same type in a single variable.


Frequently Asked Questions (FAQs)

Q1. What are C++ Loops?

C++ Loops are used to repeat a block of code multiple times without writing the same statements repeatedly.

Q2. When should you use a C++ for loop?

A C++ for loop is commonly used when you know how many times a block of code needs to run, such as printing numbers from 1 to 10.

Q3. How does a C++ while loop work?

A C++ while loop continues running as long as its condition remains true. The condition is checked before each iteration.

Q4. What is a C++ do while loop?

A C++ do while loop executes its code first and checks the condition afterward. Therefore, the loop runs at least once.

Q5. What are C++ nested loops used for?

C++ nested loops are loops placed inside another loop. They are commonly used for patterns, tables, matrices, and multidimensional data.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top