Professional Documents
Culture Documents
Loops: The Increment and Decrement Operators: Val++ Val Val + 1 ++val Val++
Loops: The Increment and Decrement Operators: Val++ Val Val + 1 ++val Val++
1
int num, val = 12;
cout << val++; // displays 12,
// val is now 13;
cout << ++val; // sets val to 14,
// then displays it
num = --val; // sets val to 13,
// stores 13 in num
num = val--; // stores 13 in num,
// sets val to 12
Note: if val = 12
Introduction to Loops:
Loop: a control structure that causes a statement or statements to repeat
Types:
while
do-while
for
expression is evaluated
if TRUE then statement is executed, and expression is
evaluated AGAIN
2
if FALSE then the loop is finished and program statements following
statement execute
Prog 1:
3
Flowchart of the while Loop
4
Watch Out for Infinite Loops
The loop must contain code to make expression become false
int number = 1;
expression is forever TRUE, since
number is always to 1
while (number <= 5)
{ Loop body keeps displaying “Hello “
cout << "Hello\n"; infinite number of times.
}
The while loop can be used to create input routines that reject invalid
data, and repeat until valid data is entered.
{
Display an error message.
Read the input again.
}// end while
5
//Example of Input validation
6
Prog 2
7
Counters (check Prog1 above)
Counter: a variable that is incremented or decremented each time a
loop repeats
Can be used to control execution of the loop (also known as the loop
control variable)
counter = 1; //initialization
while(counter <=10) //test
{ .
. //repeated actions
.
counter++; //update variable
General Format:
do
statement; // or block in { }
while (expression);
8
The Logic of a do-while Loop
9
Prog 3
10
The for Loop
Counter-controlled loop
Pretest loop -tests its expression BEFORE each iteration
General Format:
for(initialization; test condition; update)
statement; // or block in { }
1) Perform initialization
2) Evaluate test expression
If true, execute statement
If false, terminate loop execution
3) Execute update, then re-evaluate test expression
11
Flowchart demonstrating for loop
12
When to Use the for Loop
In any situation that clearly requires
an initialization
a false condition to stop the loop
an update to occur at the end of each iteration
You can also have multiple statements in the update expression. Separate
the statements with a comma:
update expressions
int x, y;
for (x = 1, y = 1; x <= 5; x++, y++ )
{
cout << x <<" +" << y<< "= " <<(x + y)<< endl;
13
You can omit the initialization expression if it has already been
done:
int sum = 0, num = 1;
for (; num <= 10; num++)
sum += num;
14
Logic for Keeping a Running Total
Sentinels
sentinel: value in a list of values that indicates end of data
Special value that cannot be confused with a valid value, e.g., -999
for a test score
Used to terminate input when user may not know how many values
will be entered
Prog 5
15
Deciding Which Loop to Use
The while loop is a conditional pretest loop
Iterates as long as a certain condition exists
Validating input
Reading lists of data terminated by a sentinel
Nested Loops
A nested loop is a loop inside the body of another loop
Inner loop goes through all repetitions for each repetition of outer loop
16
Total number of repetitions for inner loop is product of number of
repetitions of the two loops.
Prog 6
17
Breaking Out of a Loop
if (num == 5 || num == 6 )
continue;//skips to the end of loop, starts new iteration
sum += num;
}//end while
18
Prog 7
19