You are on page 1of 15

While and do while loop

While loop
• Syntax of while Loop

while (test expression)


{
statement/s to be executed.
}
While loop
• The while loop checks whether the test
expression is true or not.
• If it is true, code/s inside the body of while
loop is executed, that is, code/s inside the
braces { } are executed.
• Then again the test expression is checked
whether test expression is true or not.
• This process continues until the test
expression becomes false.
• The while loop is preferred to the for loop if
the limit of iterations is unknown
Flow chat of the while loop
#include <iostream>
using namespace std;
int main()
{
string man;
cout << "Enter your name: ";
cin>>man; The loop will run so long as
while(man == "bongo") The while condition is true
{
cout<<"You are not lost: "<<endl;
cout<<"Enter name again: ";
cin>>man;
}
cout<<"You typed "<<man<<" not bongo";
This runs when while
return 0;
Condition is false
Example
#include <iostream>

using namespace std;

int main()
{
int num, i = 1, factorial = 1;

cout<<"Enter a positive number: ";


cin>>num;

while (i<=num)
{
factorial *=i;
i++;
}
cout<< "Factorial of "<<num<<" is "<<factorial;
return 0;
}
Using break to stop the while loop
while (nam1 !=nam2)
#include <iostream> {
cout<<"You entered a wrong name\n\n";
#include <string> i ++;
cout<<"Try again: ";
using namespace std; cin>>nam1;

int main() if (nam1==nam2)


{
{ cout<<"You got it right this time \n\n";
break;
string nam1, nam2 = }
if(i == 3)
"araba"; {
cout<<"Sorry you done ";
cout<<"Enter your name: "; break;
}
cin>> nam1; }
cout<<endl<<endl;
cout<<endl<<endl; cout<<"You entered: "<<nam1<<endl;

int i =1 ; }
return 0;
do while loop
• Unlike for and while loops, which test the loop
condition at the top of the loop,
the do...while loop checks its condition at the
bottom of the loop.
• A do...while loop is similar to a while loop,
except that a do...while loop is guaranteed to
execute at least one time
The syntax of a do...while loop in C++ is
• do
{
statement(s);
}while( condition );

.
do while loop cont.
• Notice that the conditional expression
appears at the end of the loop, so the
statement(s) in the loop execute once before
the condition is tested
• If the condition is true, the flow of control
jumps back up to do, and the statement(s) in
the loop execute again. This process repeats
until the given condition becomes false
#include <iostream> Example
using namespace std;
Value of a: 10
int main () Value of a: 11
{ Value of a: 12
// Local variable declaration:
int a = 10; Value of a: 13
// do loop execution Value of a: 14
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 15 );

return 0;
}
Condition is not met but code executes once
#include <iostream>
using namespace std; Value of a: 10

int main ()
{
int a = 10;
// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 9 );
return 0;
#include <iostream>
Example 2
using namespace std; If the grade entered is ‘b’
You got distinction would
int main ()
{
be displayed before the
char grade; code terminates
// do loop execution
do
{
cout<<"please enter your grade: ";
cin>>grade;
cout<<"You got a distintion \n";
}while( grade == 'a' );

return 0;
Do while, cont, do
{
#include <iostream> cout<<"Enter your name: ";
using namespace std; cin>>name;

cout<<"You are welcome "<<name;


int main () counter++;
{ cout<<endl;
string name; if(name != "Amanda")
cout<<"Try again"<<endl;
int counter = 0;
if(counter == 4)
{
// do loop execution cout<<"You have entered " <<counter<<" times";
break;
}

}while(name != "Amanda");
return 0;
}
Assignment
• What is the output of the program when embedded
in a correct program with x declared to be of
variable type int.
• X = 10
while (x > 0)
{ cout<<x<<endl;
X -=3;
}
What output would be produced if > is changed to <?

You might also like