COMPUTER
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
C++ has three looping control structures:
while loop
do-while loop
The for loop.
The difference between these structures is how they
control the repetition.
THE WHILE LOOP – HOW IT WORKS
while (expression)
statement;
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
Otherwise, the loop will have no way of stopping
Such a loop is called an infinite loop, because it will
repeat an infinite number of times
EXAMPLE OF AN INFINITE LOOP
int number = 1;
while (number <= 5)
{
cout << "Hello\n";
}
ACTIVITY 1
Write a program that display “Pakistan for five times
using while loop.
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
Write a program that displays counting from 1 to 10
using while loop.
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
Write a program that display first five numbers with their
squares using While loop.
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)
do: It is the keyword that indicate the beginning of the
loop.
Statement: It represent the body of the loop.
While(condition): It indicate the last statement
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
while do… while
• It is pre tested loop as the • It is post test loop as
condition s checked before condition is checked after
the body of the loop. the body of the loop.
• The loop body is never • The loop body is executed
executed if the condition is at least once even if
false in the beginning. condition is false.
• The semicolon is not used • The semicolon is used after
after the condition. the condition.
• It is called entry controlled • It is called exit controlled
loop. loop.
23
Activity 5
• Write a program that display a back counting
from 10 to 1 using do…. while loop.
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;
}