While loop:
In the while loop, the looping statement has been executed repeatedly as long as the test condition is true. It is generally used when the number of iterations is unknown.
While loop is known as entry controlled loop because the test condition is checked before the entry of the loop.
Syntax:
while ( Test expression ){ //Body of the loop; }
Flow chart:
Problem statement: Print sum of first 5 natural numbers using while loop in c++.
Code:
C++ Code
#include<iostream>
using namespace std;
void while_loop() {
int i = 1, sum = 0;
while (i <= 5) {
sum += i;
i += 1;
}
cout << "The sum of first 5 natural number is " << sum;
}
int main() {
while_loop();
return 0;
}
Output:- The sum of first 5 natural number is 15
Explanation:
On the 1st iteration, when i=1 the while loop condition will check if i is less than or equal to 5 the answer is true so, the body of the loop will execute that is the sum value get added by i and i value get incremented by 1.
Similar to when i equal to 2,3,4,5
On the 6th iteration, when i=6 while loop condition will become false because i value is not less than or equal to 5 and the loop will terminate.
do-while loop:
The do-while loop is similar to the while loop. In the do-while loop also the looping statement has been executed repeatedly as long as the test condition is true. But the difference is do-while loop is an exit-controlled loop. The test condition is checked at the end of the looping statement.
This means that the body of the loop is executed at least once, even when the condition evaluates false during the 1st iteration.
Syntax:
do { //Body of the loop; } while (condition);
Flow chart:
Example of the do-while loop:
C++ Code
#include<iostream>
using namespace std;
void dowhile_loop() {
int input;
do {
cout << "Welcome to TakeUForawrd" << '\n';
cout << "TakeUForawrd is the best place to "
<< "learn data structures, algorithms,"
<< " most asked coding interview questions, "
<< "real interview experiences free of cost." << '\n';
cout << "enter -1 to exit or any other key to "
<< "see this message again" << '\n';
cin >> input;
} while (input != -1);
}
int main() {
dowhile_loop();
return 0;
}
Output:-
Welcome to TakeUForawrd
takeuforward is the best place to learn data structures, algorithms, most asked coding interview questions, real interview experiences free of cost.
enter -1 to exit or any other key to see this message again
-1
Explanation:
In the above program, the body of the do-while loop is executed once, and asking the user to enter the input if we want to again execute the body of the loop means to enter any key other than 1 or enter 1 to quit. This process continues until the test expression becomes false.
Note:- Even though the do-while loop condition evaluates to false at 1st iteration means also the body of the loop definitely executes one time.
Special thanks to JAIGANESH R S for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article