Home | | Computer Science 11th std | C++ do-while loop

Chapter: 11th Computer Science : Chapter 10 : Flow of Control

C++ do-while loop

The do-while loop is an exit-controlled loop. In do-while loop, the condition is evaluated at the bottom of the loop after executing the body of the loop.

do-while loop

 

The do-while loop is an exit-controlled loop. In do-while loop, the condition is evaluated at the bottom of the loop after executing the body of the loop. This means that the body of the loop is executed at least once, even when the condition evaluates false during the first iteration.

The do-while loop syntax is:

do

{

      Body of the loop;

} while(condition);

The flow control and flow chart do-while loop is shown below


Flowchart 10.8 : do-while loop control flow and do-while loop flowchart

 

Illustration 10.16 C++ program to display number from 10 to 1 using do-while loop

#include <iostream>

using namespace std;

int main ()

{

int n = 10;

do

{

      cout<<n<<", ";

      n--;

}while (n>0) ;

}

Output

10, 9, 8, 7, 6, 5, 4, 3, 2, 1

 

In the above program, the integer variable n is initialized to 10. Next the value of n is displayed as 10 and n is decremented by 1. Now, the condition is evaluated, since 9 > 0, again is displayed and n is decremented to 8. This continues, until n becomes equal to 0, at which point, the condition n > 0 will evaluate to false and the do-while loop terminates.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 10 : Flow of Control : C++ do-while loop |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.