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
#include <iostream>
using namespace std;
int main ()
{
int n = 10;
do
{
cout<<n<<", ";
n--;
}while (n>0) ;
}
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 9 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.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.