8/2/2019 Lecture 05 Loops 2010
1/25
Introduction to Programming
Environments (C++/UNIX)
IPE 115, Semester 1, 2010
Lecture 05
8/2/2019 Lecture 05 Loops 2010
2/25
Repetition Structure
8/2/2019 Lecture 05 Loops 2010
3/25
3
INTRODUCTION TO PROGRAMMING
Types of Repetition Structures in C++
while
for
do-while
8/2/2019 Lecture 05 Loops 2010
4/25
4
INTRODUCTION TO PROGRAMMING
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.
8/2/2019 Lecture 05 Loops 2010
5/25
5
INTRODUCTION TO PROGRAMMING
The While Loop
Syntax of While Loop
while (boolean expr)
statement;
next statement;
while (boolean expr)
{
statement 1;statement 2;
}
next statement;
boolean
expr
next statementstatements
false
true
8/2/2019 Lecture 05 Loops 2010
6/25
6
INTRODUCTION TO PROGRAMMING
Notes on semantics of While Loop
All variables in the boolean expression must be initializedprior to the loop.
At least one of the variables in the boolean expressionmust be assigned a new value inside the loop. In other
words the boolean expression must change its valueinside the loop.
The boolean expression is tested prior to entering the loopand before each repetition of the loop body.
The entire loop body is executed if the boolean expressionis true.
It is possible not to execute the loop body, this occurswhen the boolean expression is initially false.
8/2/2019 Lecture 05 Loops 2010
7/25
7
INTRODUCTION TO PROGRAMMING
Example- Print Numbers from 1 to 10
int count =1;
while (count
8/2/2019 Lecture 05 Loops 2010
8/25
8
INTRODUCTION TO PROGRAMMING
Infinite Loops
An infinite loop is one in which the condition isinitially satisfied, so the loop is entered, but thecondition for exiting the loop is never met.
Generally an infinite loop is caused by failing tomodify a variable involved in the condition withinthe loop body.
To break out of a malfunctioning program press
ctrl-C on Linux or ctrl-break, on an DOS orWindows machine.
8/2/2019 Lecture 05 Loops 2010
9/25
9
INTRODUCTION TO PROGRAMMING
What Does This Loop Print?
int I =1;
while(I < 6 )
cout
8/2/2019 Lecture 05 Loops 2010
10/25
10
INTRODUCTION TO PROGRAMMING
Interactive I/O and Looping
The sentinel or trailer technique uses aspecial end-of data value to indicate theend of meaningful data.
Using the while loop, we place an inputstatement before the loop to read the firstvalue and an input statement at thebottom of the loop to read the next value.
The loop condition tests that the value isnot equal to the sentinel value.
8/2/2019 Lecture 05 Loops 2010
11/25
11
INTRODUCTION TO PROGRAMMING
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.
8/2/2019 Lecture 05 Loops 2010
12/25
12
INTRODUCTION TO PROGRAMMING
#include
using namespace std;
int main()
{
int number, sum, cnt; //Initialization
cout > 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
8/2/2019 Lecture 05 Loops 2010
13/25
13
INTRODUCTION TO PROGRAMMING
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.
8/2/2019 Lecture 05 Loops 2010
14/25
14
INTRODUCTION TO PROGRAMMING
Comparison of For and While Loops
for(i=1; i
8/2/2019 Lecture 05 Loops 2010
15/25
15
INTRODUCTION TO PROGRAMMING
Example
Print the numbers from 100 to 10 as
follows;
100 90 80 70 --------- 10
for ( int i=100 ; i>=10 ; I = i-10 )
{cout
8/2/2019 Lecture 05 Loops 2010
16/25
16
INTRODUCTION TO PROGRAMMING
Comments on the for loop
All three expressions that are part of thefor loop are optional. The semicolons arenot optional.
If the boolean expression is omitted youhave an infinite loop.
Use the for loop when you know exactly
how many times the loop body is to beexecuted, either as a specific value or asan expression.
8/2/2019 Lecture 05 Loops 2010
17/25
17
INTRODUCTION TO PROGRAMMING
All parts of the for loop are optional
i = 0;
for(; i
8/2/2019 Lecture 05 Loops 2010
18/25
18
INTRODUCTION TO PROGRAMMING
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 butnot the exiting of the loop.
8/2/2019 Lecture 05 Loops 2010
19/25
19
INTRODUCTION TO PROGRAMMING
Nesting of Loops
The statements in the loop body may be any C+
+ statement including another looping statement.
When a forloop is entered from the top, the
initialization occurs and then the booleanexpressions 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.
8/2/2019 Lecture 05 Loops 2010
20/25
20
INTRODUCTION TO PROGRAMMING
Nested For Loops
for (I=1; I
8/2/2019 Lecture 05 Loops 2010
21/25
21
INTRODUCTION TO PROGRAMMING
Loop with Post test Syntax to Do-WhileLoop
do
statement;
while (bool expr);
do
{statement 1;
statement 2;
}while (bool expr);
statement
bool
exprNext statement
true false
8/2/2019 Lecture 05 Loops 2010
22/25
22
INTRODUCTION TO PROGRAMMING
An example of the Do While Loop
int i=1;
do
{
cout
8/2/2019 Lecture 05 Loops 2010
23/25
23
INTRODUCTION TO PROGRAMMING
Notes on semantics of
Do While Loop
At least one of the variables in the booleanexpression must be assigned a new value insidethe loop. In other words the boolean expressionmust change value inside the loop.
The boolean expression is tested at the end ofthe loop body after each execution of the loopbody.
The entire loop body is executed if the booleanexpression is true.
The loop body is always executed at least once.
8/2/2019 Lecture 05 Loops 2010
24/25
24
INTRODUCTION TO PROGRAMMING
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.
8/2/2019 Lecture 05 Loops 2010
25/25
25
INTRODUCTION TO PROGRAMMING
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 wherethe next Fibonacci number is the sum of
the two previous Fibonacci numbers.
Top Related