Do While Loop
Do While Loop
PRESEN TA T I O N
Do-while loop:
The do-while loop is an iterative control in C++
The loop is executes one or more statements while the given
condition is true. The condition in this loop checked after the
body of loop.
The do while loop checks the condition at the end of the loop.
This means that the statements inside the loop body will be executed at
least once even if the condition is never true.
FLOWCHART
Syntax:
The syntax of do while loop is as follows Loop body
do {
true
// code block to be executed
Condition
}
while (condition);
False
do while loop:
Do:
It is the keyword that indicate the beginning of the loop.
Condition:
The condition is given as a relational expression. The loop continues
only the given condition is true. If the condition is true . If the condition is
false ,the loop is terminated
Statement :
Statement is the instruction that is executed when condition
is true .two more statement are written in braces{}.it is called the
body of the loop.
Working of do-while loop:
• The body of the loop is executed at first. Then the condition is
evaluated.
• If the condition evaluates to true, the body of the loop inside the
do statement is executed again.
• The condition is evaluated once again.
• If the condition evaluates to true, the body of the loop inside
the do statement is executed again.
• This process continues until the condition evaluates to false. Then the
loop stops.
Do while loop Program Output:
12345
#include <iostream>
using namespace std;
int main()
{
int i = 1;
// do...while loop from 1 to 5
do { cout << i << " "; ++i; }
while (i <= 5);
}
include <iostream>
output
using namespace std;
Enter a number: 6
int main() Enter a number: 12
{ Enter a number: 7
Int number = 0; Enter a number: 0
Enter a number: -2
int sum = 0; The sum is 25
do { sum += number; cout << "Enter a number: "; cin >> number; }
while (number >= 0);
cout << "\nThe sum is " << sum << endl;
}