0% found this document useful (0 votes)
306 views

C++ Do While Loop

While and do-while are looping statements in C++. The while loop checks the condition first before executing the statements in the loop body. This means the statements may never run if the condition is false from the start. The do-while loop checks the condition after executing the statements at least once, ensuring the body runs at least once before evaluating the condition. Sample programs demonstrate a while loop that repeats until the user enters 0, and a do-while loop that counts numbers between 100-200 before the user enters 0 to quit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
306 views

C++ Do While Loop

While and do-while are looping statements in C++. The while loop checks the condition first before executing the statements in the loop body. This means the statements may never run if the condition is false from the start. The do-while loop checks the condition after executing the statements at least once, ensuring the body runs at least once before evaluating the condition. Sample programs demonstrate a while loop that repeats until the user enters 0, and a do-while loop that counts numbers between 100-200 before the user enters 0 to quit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 7

WHILE AND DO-WHILE

LOOPING STATEMENT
Its format is:
while (expression)
{
statements;
}
and its function is simply to repeat statement while
expression is true.

The while loop performs the conditional test first and


then executes the loop, so the statements within a loop
may never be executed.
 The while construct consists of a block of code
and a condition.

 The condition is evaluated, and if the condition is


true, the code within the block is executed.

 This repeats until the condition becomes false.

 Because while loops check the condition before


the block is executed, the control structure is
often also known as a pre-test loop.
The general form of a do...while loop is:
do
{
statements;
}
while(test_condition);

The do...while loop is a variant of the while loop with one


important difference. The body of do...while loop is
executed once before the test expression is checked.
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
Sample Program 1
#include<iostream.h>
int main()
{
int input;
cout<<"Enter a number (0 to quit):
";
cin>>input;
while (input != 0)
{
cout << "Tripled: " << input * 3
<< endl;
cout << "Enter a number (0 to
quit): ";
cin >> input;
}
return(0);
}
Sample Program 2

#include <iostream.h>
#include <conio.h>
int main()
{
int input, numberBetween=0;
do
{
cout<< "Enter a number (0 to quit): ";
cin>> input;
if (input >= 100 && input <= 200)
numberBetween++;
} while (input != 0);
cout << "Numbers between 100 and 200:" <<
numberBetween;
getch();
return 0;
}

You might also like