In computer programming, loops are essential tools that allow you to repeat a block of code multiple times. One of the most commonly used loops in C++ is the while loop. It provides a simple & effective way to execute a set of instructions as long as a specific condition remains true.
In this article, we will learn about the basics of the while loop, understand its syntax. We will also see its flowchart to understand how it works and end our article with few examples.
Syntax
The syntax of a while loop in C++ is straightforward. Here's how it looks:
while (condition) {
// code block to be executed
}
The while keyword is followed by a condition enclosed in parentheses. The condition is an expression that evaluates to either true or false. If the condition is true, the code block inside the curly braces {} is executed. After the code block is executed, the condition is checked again. If it is still true, the code block is executed once more. This process continues until the condition becomes false.
How does a While loop execute?
The condition is evaluated before entering the loop. If the condition is true, the code block is executed. If the condition is false from the beginning, the code block is skipped entirely, and the program continues with the next statement after the loop.
Inside the loop, the code block is executed statement by statement. These statements can include variable assignments, function calls, or any other valid C++ code.
After executing the last statement in the code block, the program goes back to the beginning of the loop and re-evaluates the condition.
If the condition is still true, the code block is executed again. This process repeats until the condition becomes false.
Once the condition becomes false, the loop is terminated, and the program continues with the next statement after the loop.
Note : It's crucial to ensure that the condition eventually becomes false; otherwise, the loop will continue indefinitely, resulting in an infinite loop.
Flow Diagram of while loop
The flow diagram showing the following steps:
The condition is evaluated. If it is true, the code block is executed. If it is false, the loop is terminated, and the program continues with the next statement after the loop.
After executing the code block, the program goes back to the condition and re-evaluates it.
If the condition is still true, the code block is executed again. This process repeats until the condition becomes false.
Once the condition becomes false, the loop is terminated, and the program continues with the next statement after the loop.
Examples
Example 1: Printing numbers from 1 to 5
C++
C++
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
return 0;
}
You can also try this code with Online C++ Compiler
In this example, the variable i is initialized to 1. The while loop continues to execute as long as i is less than or equal to 5. Inside the loop, the value of i is printed, and then i is incremented by 1. The loop terminates when i becomes 6.
Example 2: Sum of numbers until the user enters 0
C++
C++
#include <iostream>
using namespace std;
int main() {
int number, sum = 0;
cout << "Enter numbers (enter 0 to stop): ";
while (true) {
cin >> number;
if (number == 0)
break;
sum += number;
}
cout << "Sum = " << sum << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Enter numbers (enter 0 to stop): 5 8 3 2 0
Sum = 18
In this example, we use a while loop with a true condition to keep accepting numbers from the user until they enter 0. Inside the loop, we check if the entered number is 0. If it is, we use the break statement to exit the loop. Otherwise, we add the number to the sum variable. After the loop ends, we print the sum of all the entered numbers.
Frequently Asked Questions
What happens if the condition in a while loop is always true?
If the condition in a while loop is always true, it becomes an infinite loop, and the program will continue to execute indefinitely unless there is a break statement or some other way to exit the loop.
Can we have multiple statements inside the while loop?
Yes, you can have multiple statements inside the while loop. All the statements inside the loop will be executed repeatedly as long as the condition remains true.
Is it necessary to have an increment or decrement statement inside the while loop?
No, it's not necessary to have an increment or decrement statement inside the while loop. However, it's important to ensure that the condition eventually becomes false to avoid an infinite loop.
Conclusion
In this article, we learned about the while loop in C++. We discussed its syntax, understood how it executes, and looked at its flow diagram. We also saw practical examples of using the while loop to perform repetitive tasks. The while loop is a powerful tool that allows you to execute a block of code repeatedly based on a condition.