Introduction
The while
loop in C++ is a control flow statement that allows code to be executed repeatedly based on a given condition. The loop continues to execute as long as the specified condition is true. The while
loop is useful when the number of iterations is not known in advance and depends on a condition.
Syntax
The basic syntax of a while
loop is as follows:
while (condition) {
// code to be executed
}
Key Points
- Condition: Evaluated before each iteration. If
true
, the loop body is executed. Iffalse
, the loop terminates. - Loop Body: The block of code that is executed repeatedly as long as the condition is true.
Example: Basic while Loop
#include <iostream>
using namespace std;
int main() {
int i = 0; // Initialization of loop control variable
while (i < 5) { // Condition
cout << "i = " << i << endl; // Print the value of i
i++; // Increment the loop control variable
}
return 0;
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
Explanation
- Initialization:
int i = 0
initializes the loop control variablei
to 0. - Condition:
i < 5
checks ifi
is less than 5. Iftrue
, the loop body executes. - Loop Body:
cout << "i = " << i << endl;
prints the value ofi
. - Increment:
i++
incrementsi
by 1 after each iteration.
Example: Sum of First N Natural Numbers
A while
loop can be used to calculate the sum of the first N natural numbers.
#include <iostream>
using namespace std;
int main() {
int n, sum = 0, i = 1; // Initialization of variables
cout << "Enter a positive integer: ";
cin >> n; // Input the value of n
while (i <= n) { // Condition
sum += i; // Add i to sum
i++; // Increment i
}
cout << "Sum of the first " << n << " natural numbers is: " << sum << endl;
return 0;
}
Output
Enter a positive integer: 10
Sum of the first 10 natural numbers is: 55
Explanation
- Initialization:
int n, sum = 0, i = 1
initializes the variables. - Input:
cin >> n
takes the input value ofn
. - Condition:
i <= n
checks ifi
is less than or equal ton
. Iftrue
, the loop body executes. - Loop Body:
sum += i
addsi
tosum
.i++
incrementsi
by 1 after each iteration.
Example: Find Factorial of a Number
A while
loop can be used to calculate the factorial of a given number.
#include <iostream>
using namespace std;
int main() {
int n, factorial = 1, i = 1; // Initialization of variables
cout << "Enter a positive integer: ";
cin >> n; // Input the value of n
while (i <= n) { // Condition
factorial *= i; // Multiply i to factorial
i++; // Increment i
}
cout << "Factorial of " << n << " is: " << factorial << endl;
return 0;
}
Output
Enter a positive integer: 5
Factorial of 5 is: 120
Explanation
- Initialization:
int n, factorial = 1, i = 1
initializes the variables. - Input:
cin >> n
takes the input value ofn
. - Condition:
i <= n
checks ifi
is less than or equal ton
. Iftrue
, the loop body executes. - Loop Body:
factorial *= i
multipliesi
tofactorial
.i++
incrementsi
by 1 after each iteration.
Example: Reverse Digits of a Number
A while
loop can be used to reverse the digits of a given number.
#include <iostream>
using namespace std;
int main() {
int number, reversedNumber = 0; // Initialization of variables
cout << "Enter an integer: ";
cin >> number; // Input the value of number
while (number != 0) { // Condition
int remainder = number % 10; // Get the last digit
reversedNumber = reversedNumber * 10 + remainder; // Build the reversed number
number /= 10; // Remove the last digit
}
cout << "Reversed Number: " << reversedNumber << endl;
return 0;
}
Output
Enter an integer: 1234
Reversed Number: 4321
Explanation
- Initialization:
int number, reversedNumber = 0
initializes the variables. - Input:
cin >> number
takes the input value ofnumber
. - Condition:
number != 0
checks ifnumber
is not equal to 0. Iftrue
, the loop body executes. - Loop Body:
int remainder = number % 10
gets the last digit ofnumber
.reversedNumber = reversedNumber * 10 + remainder
builds the reversed number.number /= 10
removes the last digit fromnumber
.
Conclusion
The while
loop is a powerful and flexible control flow statement in C++ that allows you to execute a block of code repeatedly based on a given condition. This chapter covered the basic syntax of the while
loop, examples of calculating the sum of the first N natural numbers, finding the factorial of a number, and reversing the digits of a number. Understanding how to use the while
loop effectively will help you write more efficient and readable code. In the next chapter, we will explore the do-while
loop in C++.