C++ Loops
Imagine you want to print a message 10 times. Writing the same cout statement 10 times would make your program unnecessarily long. This is where loops are useful.
Loops in C++ allow you to repeat a block of code as long as a particular condition is true. They are commonly used when you need to perform the same task multiple times.
C++ provides three main types of loops: while, do-while, and for. You can also use nested loops when one loop needs to run inside another. Along with loops, break, continue, and goto can change the normal flow of execution.
while Loop in C++
The while loop is useful when you want to repeat something but don’t know exactly how many times it should run. The condition is checked before each repetition.
Syntax
while (condition)
{
// code
}
Example
int i = 1;
while (i <= 5)
{
cout << i << endl;
i++;
}
Output
1
2
3
4
5
Here, the loop starts with i = 1. C++ checks whether i is less than or equal to 5. If the condition is true, the code runs and i increases by 1.
The loop stops when the condition becomes false.
do while Loop in C++
The do-while loop is similar to the while loop, but there is one important difference. The code inside the loop runs at least once, even when the condition is false.
Syntax
do
{
// code
}
while (condition);
Example
int i = 1;
do
{
cout << i << endl;
i++;
}
while (i <= 5);
Output
1
2
3
4
5
Notice that the condition is written after the code. This means C++ executes the code first and checks the condition afterward.
For example:
int i = 10;
do
{
cout << i;
}
while (i < 5);
Even though i < 5 is false, 10 will still be printed once.
for Loop in C++
The for loop in C++ is commonly used when you already know how many times you want to repeat something.
Syntax
for (initialization; condition; update)
{
// code
}
Example
for (int i = 1; i <= 5; i++)
{
cout << i << endl;
}
Output
1
2
3
4
5
Here:
int i = 1sets the starting value.i <= 5decides whether the loop should continue.i++increases the value after every repetition.
The for loop keeps these three parts together, which makes it convenient for counting tasks.
Nested Loops in C++
Sometimes you need one loop to run inside another loop. This is called Nested Loops in C++.
For example, suppose you want to print a small pattern:
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
cout << "* ";
}
cout << endl;
}
Output
* * *
* * *
* * *
The outer loop controls the rows, while the inner loop controls the columns.
Nested loops are commonly used for patterns, tables, matrices, and other problems where one repetition depends on another.
break Statement
The break statement is used when you want to stop a loop immediately.
Example
for (int i = 1; i <= 10; i++)
{
if (i == 5)
break;
cout << i << endl;
}
Output
1
2
3
4
When i becomes 5, break stops the loop completely.
continue Statement
The continue statement skips the current repetition and moves to the next one.
Example
for (int i = 1; i <= 5; i++)
{
if (i == 3)
continue;
cout << i << endl;
}
Output
1
2
4
5
Here, when i becomes 3, continue skips that repetition. The loop then continues with 4.
goto Statement
The goto statement moves program execution to a labelled statement.
Example
int i = 1;
start:
cout << i << endl;
i++;
if (i <= 3)
goto start;
Output
1
2
3
Although goto can be used to repeat code, it is generally avoided in modern C++ because it can make programs difficult to follow. for, while, and do-while loops are usually better choices.
Frequently Asked Questions (FAQs)
Q1. Which loop should I use in C++?
Use a for loop in C++ when the number of iterations is known, a while loop when repetition depends on a condition, and a do-while loop when the code must run at least once.
Q2. Can a for loop run without a condition in C++?
Yes. Omitting the condition creates an infinite for loop, which can be stopped using a break statement or another control mechanism.
Q3. How do I stop a loop early in C++?
Use break to immediately terminate the loop, even when its original condition is still true.
Q4. What is the difference between continue and break in C++?
continue skips the remaining statements in the current iteration, while break completely terminates the loop.
Q5. When are nested loops used in C++?
Nested Loops in C++ are useful for working with patterns, tables, matrices, and problems that require one repetition process inside another.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
