IT101, Semester 1
Lecture 5
Repetition Structure
Types of Repetition Structures in C++
while
for
do-while
Pre-test and Post-test loops
Pre-test loops are entrance controlled loops.
You execute the loop body after evaluating the
test.
Loop body can be executed zero or more times.
Post-test loops are exit controlled loops.
You test the loop after executing the entire loop
body.
Loop body can be executed one or more times.
4
The While Loop
Syntax of While Loop
while (boolean expr)
statement;
next statement;
boolean
expr
false
true
while (boolean expr)
{
statement 1;
statement 2;
statements
next statement
}
next statement;
5
Notes on semantics of While Loop
All variables in the boolean expression must be
initialized prior to the loop.
At least one of the variables in the boolean
expression must be assigned a new value inside the
loop. In other words the boolean expression must
change its value inside the loop.
The boolean expression is tested prior to entering
the loop and before each repetition of the loop body.
The entire loop body is executed if the boolean
expression is true.
It is possible not to execute the loop body, this
occurs when the boolean expression is initially false.
6
Example- Print Numbers
from 1 to 10
int count =1;
while (count <= 10 )
{
cout<< count <<endl;
count ++;
Infinite Loops
An infinite loop is one in which the
condition is initially satisfied, so the loop is
entered, but the condition for exiting the
loop is never met.
Generally an infinite loop is caused by
failing to modify a variable involved in the
condition within the loop body.
To break out of a malfunctioning program
press ctrl-C on Linux or ctrl-break, on an
DOS or Windows machine.
8
What Does This Loop Print?
int I =1;
while(I < 6 )
cout<<Line number<<I<<endl;
How should it be fixed to print the numbers
between 1 and 5 inclusive in a column?
Interactive I/O and Looping
The sentinel or trailer technique uses a
special end-of data value to indicate the end
of meaningful data.
Using the while loop, we place an input
statement before the loop to read the first
value and an input statement at the bottom of
the loop to read the next value.
The loop condition tests that the value is not
equal to the sentinel value.
10
An Example of Sentinel Input
Input a list of positive numbers from the
keyboard and find the average. The list is
terminated with the value -99.
11
#include <iostream>
using namespace std;
int main()
{
int number, sum, cnt;
//Initialization
cout << Enter a list of integers terminated by 99;
cin >> number;
//first number
sum = 0;
cnt = 0;
while (number != -99) //Loop to sum and count values
{
sum += number;
++cnt;
cin >> number;
//Read next number
} //while
//Calculate and print average
cout << The average is << sum/float(cnt);
return 0;
} //main
12
Counter Controlled Loops
A counter controlled loop is a looping control
structure in which a loop variable manages the
repetition by counting.
The syntax for a counter controlled loop in C and C+
+ is:
for(inti expr; boolean expr; increment expr)
statement;
A loop body of more than one statement, must be
enclosed in curly braces.
13
Comparison of For and While
Loops
for(i=1; i <= 10; i++)
cout << i << endl;
i=1;
while (i <= 10)
{
cout << i << endl;
i++;
}
14
Example
Print the numbers from 100 to 10 as follows;
10090
80
70
--------- 10
for ( int i=100 ; i>=10 ; I = i-10 )
{
cout<< i << ;
}
15
Comments on the for loop
All three expressions that are part of the for
loop are optional. The semicolons are not
optional.
If the boolean expression is omitted you have
an infinite loop.
Use the for loop when you know exactly how
many times the loop body is to be executed,
either as a specific value or as an expression.
16
All parts of the for loop are
optional
i = 0;
for(; i<5; i++)
cout << i << endl;
for(i=0; i<5; )
{
i++;
cout << i << endl;
}
for(i=0; i<5; i++)
;
for( ; ; )
17
break and continue
Statements with Loops
The break statement causes the
immediate termination of the execution of
the loop body and the continuation of
execution with the first statement after the
loop.
The continue statement causes the
immediate termination of the execution of
the loop body but not the exiting of the
loop.
18
Nesting of Loops
The statements in the loop body may be any
C++ statement including another looping
statement.
When a for loop is entered from the top, the
initialization occurs and then the boolean
expressions are executed.
When it is entered as a result of completing
execution of the loop body the increment
and then the boolean expressions are
executed.
19
Nested For Loops
I=1
for (I=1; I<=5; I++)
for(K=1; K<=3; K++)
cout <<I<< , <<K<< endl;
{next statement}
OUTPUT:
1,1
1,2
1,3
2,1
2,2
2,3
3,1
.
5,3
I=I+1
I <=5
next statement
K=1
K=K+1
K <=3
cout<<I<<,<<K
20
Loop with Post test
Syntax to Do-While
Loop
statement
do
statement;
while (bool expr);
true
bool
expr
false
Next statement
do
{
statement 1;
statement 2;
}while (bool expr);
21
An example of the Do While Loop
int i=1;
do
{
cout << Line number << i << endl;
i++;
}while (i < 6);
22
Notes on semantics of
Do While Loop
At least one of the variables in the boolean
expression must be assigned a new value
inside the loop. In other words the boolean
expression must change value inside the loop.
The boolean expression is tested at the end of
the loop body after each execution of the loop
body.
The entire loop body is executed if the
boolean expression is true.
The loop body is always executed at least
once.
23
Determining The Loop To Use
If the statements of the loop may not be
executed at all, use a while loop.
If the statements of the loop must be
executed at least once either loop may be
used but the do-while loop is preferable.
24
Example involving While Loop
Write a program to calculate the value of nth
Fibonacci number. The first and second
Fibonacci numbers are 1; the third Fibonacci
number is 2; and so forth where the next
Fibonacci number is the sum of the two
previous Fibonacci numbers.
25