You are on page 1of 56

BITG 1113:

Control Technique
(Repetition)

LECTURE 5
The Objectives
1. To use while, do-while, and for loops to execute
statements repeatedly.
2. To understand the flow of control in loops.
3. To use Boolean expressions to control loops.
4. To write nested loops.
5. To know the similarities and differences of three types of
loops.
6. (Optional) To implement program control with break
and continue.

BITG 1113 : LECTURE 5 2


Why is Repetition Needed ?
 Ability to repeat an operation or a series of operations
many times.
 An action or a series of actions.
 C++ has three repetition , or looping structures that allow
us to repeat a set of statements until certain conditions are
met:
 while
 do-while
 for

BITG 1113 : LECTURE 5 3


while Looping Structure

BITG 1113 : LECTURE 5 4


while Loop

BITG 1113 : LECTURE 5 5


Trace while Loop
Initialize count
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 6


Trace while Loop, Cont.
(count < 2) is true
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 7


Trace while Loop, Cont.
Print Welcome to C++
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 8


Trace while Loop, Cont.
Increase count by 1
int count = 0; count is 1 now
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 9


Trace while Loop, Cont.
(count < 2) is still true since
int count = 0; count is 1
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 10


Trace while Loop, Cont.
Print Welcome to C++
int count = 0;
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 11


Trace while Loop, Cont.
Increase count by 1
int count = 0; count is 2 now
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 12


Trace while Loop, Cont.
(count < 2) is false since count is
int count = 0; 2 now
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 13


Trace while Loop, Cont.
The loop exits. Execute the next
int count = 0; statement after the loop.
while (count < 2)
{
cout << "Welcome to C++!";
count++;
}

BITG 1113 : LECTURE 5 14


Controlling while Loop
 When a program needs to be tested again and again on
certain data it will operate a large amount of data.
 To overcome the problem, we have four cases:
 Counter controlled while loops
 Sentinel controlled while loops
 Flag controlled while loops
 User controlled while loops
 EOF-Controlled while loops

BITG 1113 : LECTURE 5 15


Counter-controlled while loops
 We know how many pieces of data need to be read.
 Use a counter, by initializing the value to 0.
 If counter < N, the body of the while statement
executes until the value of counter >= N
 The structure :
counter = 0; //initialize the loop control variable
while (counter < N) //test the loop control variable
{ …

counter ++ //update the loop control variable

}

BITG 1113 : LECTURE 5 16


Example of a while loop : Counter-controlled
#include <iostream>
using namespace std;

int main ()
{
double score, sum=0,average;
int studProc;

studProc = 0; //initialization
while (studProc < 10) //condition
{
cout << “Enter your score:”;
cin>>score;
sum += score;
studProc++; //updating
}
average = sum/10;
cout << “The average is : “ << average;
}

BITG 1113 : LECTURE 5 17


Example of a while loop : Counter-controlled

 Output :

Enter your score:60


Enter your score:50
Enter your score:90
Enter your score:80
Enter your score:55
Enter your score:97
Enter your score:86
Enter your score:40
Enter your score:65
Enter your score:73
The average is :69

BITG 1113 : LECTURE 5 18


Sentinel-Controlled while loops
 We do not know how many data needs to be read.
 The last entry called sentinel, is a special value.
 A sentinel value is a value that is not a legitimate data
value for a particular problem (but is of proper type) that is
used as a “stopping” value.
 The while loop continues to execute as long as the program
has not read the sentinel.
 General structure :
cin >> variable;
while (variable != -1)
{
… Sentinel Value
cin>>variable;

}

BITG 1113 : LECTURE 5 19


Example of a while loop : Sentinel-controlled

BITG 1113 : LECTURE 5 20


Example of a while loop : Sentinel-controlled

BITG 1113 : LECTURE 5 21


User Controlled while Loops
 Uses a user predefined variable to control the loop

char continueLoop = 'Y';


while (continueLoop == 'Y')
{
// Execute body once
// Prompt the user for confirmation
cout << "Enter Y to continue and N to
quit: ";
cin >> continueLoop;
}

BITG 1113 : LECTURE 5 22


Example of a while loop : User-controlled
#include <iostream>
using namespace std;

int main ()
{
int count=0;
double score, sum=0,average;
char answer=‘y’; //initialization

while (answer==‘y’|| answer==‘Y’) //condition


{ cout << “Enter your score:”;
cin>>score;
sum += score;
count++;
cout<<“Do you want to continue?y/Y for Yes:”;
cin>>answer; //updating
}
average = sum/count;
cout << “The average is : “ << average << endl;
}

BITG 1113 : LECTURE 5 23


Example of a while loop : User-controlled

 Output

Enter your score:60


Do you want to continue?y/Y for Yes:y
Enter your score:50
Do you want to continue?y/Y for Yes:y
Enter your score:90
Do you want to continue?y/Y for Yes:y
Enter your score:80
Do you want to continue?y/Y for Yes:y
Enter your score:55
Do you want to continue?y/Y for Yes:n
The average is :67

BITG 1113 : LECTURE 5 24


Flag-Controlled while Loops
 Uses a bool variable to control the loop
 Example :
found = false;//initialize the loop control
variable
while (!found) //test the loop control variable
{

if (expression)
found = true //re-initialize the loop control
variable

}

BITG 1113 : LECTURE 5 25


Example of a while loop : Flag-controlled
#include <iostream>
using namespace std;

int main()
{
char letter;
bool found = false;//initialization
while (!found) //condition
{
cout<<"Enter a letter. Press x to stop : ";
cin >>letter;
if (letter=='x')
{
found = true; //updating
cout <<"\nSTOP\n";
}
}
}

BITG 1113 : LECTURE 5 26


Example of a while loop : Flag-controlled
 Output

Enter a letter. Press x to stop : s


Enter a letter. Press x to stop : x

STOP

BITG 1113 : LECTURE 5 27


EOF-Controlled while Loops
 Is used when the data file is frequently altered, added or
deleted
 Example :

cin >> variable; //initialize the loop control


variable
while (cin)
{
……
cin >> variable; //re-initialize the loop

control variable
……
}
 The loop stops when the system detects the end-of-file
signal( by pressing <Ctrl-Z>).
BITG 1113 : LECTURE 5 28
Example of a while loop : EOF-controlled
#include <iostream>
using namespace std;

int main( )
{
int n = 0;
float sum = 0;
cout<<"Enter your numbers:^Z to stop.\n";
while (cin >> n)
sum += n;
cout <<"SUM = "<< sum<<endl;
}

BITG 1113 : LECTURE 5 29


Example of a while loop : EOF-controlled
 Output :

Enter your numbers:^Z to stop.


1 2 3 4 5 ^Z
SUM = 15
Press any key to continue . . .

BITG 1113 : LECTURE 5 30


do…while Looping Structure

BITG 1113 : LECTURE 5 31


do…while Loop

BITG 1113 : LECTURE 5 32


do…while Loop
#include <iostream>
using namespace std;

int main()
{
int a; //initialization
do
{
cout << "To stop enter a number between 10 & 20 : ";
cin >> a; //updating
} while (a < 10 || a > 20); //condition
cout <<"END"<<endl;
}

BITG 1113 : LECTURE 5 33


Output :
To stop enter a number between 10 & 20 : 3
To stop enter a number between 10 & 20 : 10
END

BITG 1113 : LECTURE 5 34


for Looping Structure

BITG 1113 : LECTURE 5 35


for Loop

BITG 1113 : LECTURE 5 36


Trace for Loop
Declare i

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 37


Trace for Loop, Cont.
Execute initializer
i is now 0

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 38


Trace for Loop, Cont.
(i < 2) is true
since i is 0

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 39


Trace for Loop, Cont.
Print Welcome to C++!

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 40


Trace for Loop, Cont.
Execute adjustment statement
i now is 1

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 41


Trace for Loop, Cont.
(i < 2) is still true
since i is 1

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 42


Trace for Loop, Cont.
Print Welcome to C++

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 43


Trace for Loop, Cont.
Execute adjustment statement
i now is 2

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 44


Trace for Loop, Cont.
(i < 2) is false since i is 2

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 45


Trace for Loop, Cont.
Exit the loop. Execute the next
statement after the loop

int i;
for (i = 0; i < 2; i++)
{
cout << "Welcome to C++!";
}

BITG 1113 : LECTURE 5 46


Comparing for and while Loops

BITG 1113 : LECTURE 5 47


Comparing for and while Loops
int sum = 0; sum = 0;
int i = 1;
1 for(int i = 1;
1 i <=20;
<=20 i++)
i++
while (i <=20)
<=20 {
{ cin >> a;
cin >> a; sum += a;
sum += a; }
i++;
i++
}

BITG 1113 : LECTURE 5 48


Comparing while , do..while & for loops
int i = 2;
2 int i = 2;
2
while (i <=10)
<=10 do
{ {
cout <<i<<“ “; cout <<i<<“ “;
i+=2;
i+=2 i+=2;
i+=2

} } while (i <=10);
<=10
while loops do while loops
for (int i = 2;
2 i <=10;
<=10 i+=2)
i+=2
{
cout<<i<<“ “;
for loops
}

Output : 2 4 6 8 10
BITG 1113 : LECTURE 5 49
Recommendations
 Use the one that is most intuitive and comfortable for you.
 In general, a for loop may be used if the number of
repetitions is known, as, for example, when you need to
print a message 100 times.
 A while loop may be used if the number of repetitions is
not known, as in the case of reading the numbers until the
input is 0.
 A do-while loop can be used to replace a while loop if
the loop body has to be executed before testing the
continuation condition.

BITG 1113 : LECTURE 5 50


Nested for Loop
// Print numbers on a line

#include <iostream.h>
#include <iomanip>

int main (void)


{
for (int i = 1; i <= 3; i++)
{
cout << “Row” << i <<“ : ”;
for (int j = 1; j <= 5; j++)
cout << setw(3) << j;
cout << endl;
}
return 0;
}//main

Output :

Row 1 : 1 2 3 4 5
Row 2 : 1 2 3 4 5
Row 3 : 1 2 3 4 5

BITG 1113 : LECTURE 5 51


Other Statement Related to Looping
 break
 causes a loop to terminate
 terminate only the inner loop in nested loop

 continue
 transfers to the testing expression in while and
do…while statement
 transfers to the updating expression in a for statement.

BITG 1113 : LECTURE 5 52


The continue Statement
while (limit test) do
{ {
… …

continue; continue;

… …
} while (limit test)
}
for (initialize;limit test;update)
{

continue;

}
BITG 1113 : LECTURE 5 53
The break Example
int main ( )
{
int x = 5 ;
while ( x < = 20)
{
if (x == 10 )
break;
cout << x << “ “;
x ++ ;
}
cout << “\n Value of x now is “
<< x << endl;
}

Output :
5 6 7 8 9
Value of x now is 10

BITG 1113 : LECTURE 5 54


The continue Example
#include <iostream>
using namespace std;

int main( )
{
int count = 0;
int n = 0;
float sum = 0;
cout<<"Enter some numbers. Press ^Z to stop.\n";
while (cin >> n)
{
if (n == 0)
continue;
sum += n;
count++;
}
cout <<"Sum/Count = "<<(sum/count)<<endl;
}

BITG 1113 : LECTURE 5 55


Output
Enter some numbers. Press ^Z to stop.
2
0
3
^Z
Sum/Count = 2.5

BITG 1113 : LECTURE 5 56

You might also like