While Loop
While Loop
PROGRAMMING
REPETITION STRUCTURE
1
CONTROL STRUCTURES
All programs can be written in terms of 3 control
structures
1. Sequence Structure
2. Selection Structure
If (Single Selection Statement)
If-else (Double Selection Statement)
Switch (Multiple Selection Statement)
3. Repetition Structure
For
While
Do-while
2
INTRODUCTION TO LOOPS
Loop: a control structure that causes a statement or
statements to repeat
expression is evaluated
if true, then statement is executed, and expression
is evaluated again
if false, then the loop is finished and program statements
following statement execute
THE LOGIC OF A WHILE LOOP
THE WHILE LOOP IN PROGRAM 5-3
HOW THE WHILE LOOP IN PROGRAM 5-3
LINES 9 THROUGH 13 WORKS
FLOWCHART OF THE WHILE LOOP IN
PROGRAM 5-3
THE WHILE LOOP IS A PRETEST LOOP
expression is evaluated before the
loop executes. The following loop will
never execute:
int number = 6;
while (number <= 5)
{
cout << "Hello\n";
number++;
}
WATCH OUT FOR INFINITE LOOPS
The loop must contain code to make expression
become false
int number = 1;
while (number <= 5)
{
cout << "Hello\n";
}
ACTIVITY 1
12
ACTIVITY SOLUTION
#include<iostream>
using namespace std;
int main ( )
{
int n;
n=1;
while (n <= 5 )
{
cout<<"Pakistan"<<endl;
n++;
}
13
return 0;
}
ACTIVITY 2
14
ACTIVITY SOLUTION
#include<iostream>
using namespace std;
int main ( )
{
int n;
n=1;
while (n <= 10)
{
cout<<n<<endl;
n++;
}
15
return 0;
}
ACTIVITY 3
Write a program that display first five number and their sum
using while loop.
16
ACTIVITY SOLUTION
#include<iostream>
using namespace std;
int main ( )
{
int c, sum;
c=1;
sum = 0;
while (c <= 5)
{
cout<<c<<endl;
sum = sum +c;
c=c+1;
}
cout<< “sum is”<< sum;
17
return 0;
}
ACTIVITY 4
18
ACTIVITY SOLUTION
#include<iostream>
using namespace std;
int main ( )
{
int n;
n=1;
while (n <= 5)
{
cout<<n<< “ ”<<n*n<<endl;
n++;
}
19
return 0;
}
DO WHILE LOOP
The do while is an iterative control in C++ language. This loop executes
one or more statements while the given condition is true.
The condition on this loop is checked after the body of the loop. That is
way, it is executed at least once.
Syntax
do
{
statement 1;
statement 2;
.
.
statement n;
20
}
while(condition);
DO WHILE LOOP (FLOWCHART)
of do….while loop.
21
DO WHILE LOOP (EXAMPLE)
int main()
{
int counter = 1; // initialize counter
do
{
cout << counter << “ “ <<endl; // display counter
++counter;
}
while ( counter <= 10 ); // end do/while
22
} 1 2 3 4 5 6 7 8 9 10
Difference between while and do… while
24
Activity solution
int main ( )
{
int c;
c= 10;
do
{
cout<< c<<endl;
c =c+1;
}
while (c >=1);
return 0;
}
25
ACTIVITY 6
Write a program that gets two numbers from the user and
display the result of first number raise to the power of
second number using do…. While loop.
26
ACTIVITY SOLUTION
#include<iostream>
using namespace std;
int main ( )
{
int a, b, c, r;
cout<<“Enter the number”;
cin>>a;
cout<<“Enter the number”;
cin>>b;
c =1;
r=1;
do
{
r= r*a;
c=c+1;
}
while (c <= b);
cout<<“Result is”<<r; 27
return 0;
}