How do...while loop works?
- The codes inside the body of loop is executed at least once. Then, only the test expression is checked.
- If the test expression is true, the body of loop is executed. This process continues until the test expression becomes false.
- When the test expression is false, do...while loop is terminated.
Flowchart of do...while Loop
Flowchart of do...while Loop
#include<iostream.h>
void main()
{
int k;
k=1;
do
{
cout<<k<<endl;
k++;
}while(k<=10);
}
comparison Chart
BASIS FOR COMPARISON WHILE DO-WHILE General Form while ( condition) {
statements; //body of loop
}do{
.
statements; // body of loop.
.
} while( Condition );Controlling Condition In 'while' loop the controlling condition appears at the start of the loop. In 'do-while' loop the controlling condition appears at the end of the loop. Iterations The iterations do not occur if, the condition at the first iteration, appears false. The iteration occurs at least once even if the condition is false at the first iteration.
0 Comments