Introduction to C++ | while Loop
C++ while Loop
The while loop in C++ is a control structure that allows you to repeatedly execute a block of code as long as a given condition remains true. Unlike the for
loop, the while
loop is typically used when the number of iterations is not known beforehand, and the loop continues until a specified condition is no longer true.
What is a while Loop?
The while loop works by first checking the condition, and if the condition is true, it enters the loop and executes the code. After each iteration, it checks the condition again and repeats the process as long as the condition remains true. If the condition is false at the start, the loop is skipped altogether.
Syntax:
Syntax of a while Loop
while (condition) {
// code to be executed while condition is true
}
Example of while Loop
Here’s an example where a while
loop is used to print numbers from 1 to 5:
Code Example
#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop to print numbers from 1 to 5
while (i <= 5) {
cout << "Number: " << i << endl;
i++; // Incrementing i by 1
}
return 0;
}
Output
Number: 2
Number: 3
Number: 4
Number: 5
Infinite Loop
If the condition in the while
loop is always true, it results in an infinite loop, which will keep executing forever unless interrupted. Be careful with the conditions inside the loop to avoid this situation.
Example of an infinite loop:
Code Example: Infinite while Loop
#include <iostream>
using namespace std;
int main() {
int i = 1;
// Infinite while loop
while (true) {
cout << "This loop will run forever." << endl;
i++; // The condition is always true, so the loop never ends
}
return 0;
}
Output
(Output will repeat indefinitely)
Using break in while Loop
You can control the flow of a while
loop using the break
statement. The break
statement immediately exits the loop, even if the condition is still true.
Example of using break
in a while loop:
Code Example: Using break
#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop with break statement
while (i <= 10) {
if (i == 6) {
break; // Exit the loop when i equals 6
}
cout << "i = " << i << endl;
i++;
}
return 0;
}
Output
i = 2
i = 3
i = 4
i = 5
Using continue in while Loop
You can also use the continue
statement in a while
loop. The continue
statement skips the current iteration of the loop and moves to the next iteration.
Example of using continue
in a while loop:
Code Example: Using continue
#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop with continue statement
while (i <= 10) {
if (i == 5) {
i++; // Increment before continue to avoid infinite loop
continue; // Skip the iteration when i is 5
}
cout << "i = " << i << endl;
i++;
}
return 0;
}
Output
i = 2
i = 3
i = 4
i = 6
i = 7
i = 8
i = 9
i = 10
Pro Tip:
💡 Pro Tip
While loops are useful when you do not know in advance how many times the loop should run. However, be mindful of the condition in the while loop, as it can result in infinite loops if not handled correctly. Always ensure there is a way for the condition to eventually become false, or use a break
statement to exit the loop early if necessary.