Title: Chapter 5' Looping
1Chapter 5. Looping
25.1 The Increment and Decrement Operators
- and -- are operators that add and subtract one
from their operands. - Num Num 1
- Num 1
- Num
3The Difference between Postfix and Prefix
The cout statement 1) displays Num and 2) causes
Num to be incremented after it is displayed.
This is postfix. Num 4 cout ltlt Num // 4
is displayed The cout statement 1) displays Num
and 2) causes Num to be incremented before it is
displayed. This is prefix. Num 4 cout ltlt
Num // 5 is displayed
4Using and -- in Mathematical Expressions
- X 2
- Y 5
- Z X Y
- cout ltlt X ltlt ltlt Y ltlt ltlt Z
-
- Results 2 6 10
5Using and -- in Relational Expressions
- X 10
- if ( X gt 10)
- cout ltlt X is greater than 10.\n
- Two operations are happening
- the value in X is tested to determine if it is
greater than 10 - then X is incremented
6 The while Loop
- A loop is part of a program that repeats.
- A while loop is a pre test loop - the
expression is tested before the loop is executed - while (expression)
- statement
7WHILE is a PRE-TEST LOOP int number
10 while( number ! 99 ) cin gtgt number
number ! 99
cin gtgt number
YES
while( number ! 99 ) cin gtgt number
NO
8while LOOPS
General format while( expr ) statement(s)
// end while Next statement
9Terminating a Loop
- A loop that does not have a way of stopping is
called an infinite loop - int Test 0
- while (Test lt 10)
- cout ltlt Hello\n
- A null statement is also an infinite loop, but it
does nothing forever - while (Test lt 10)
10 // This is ok int test 0 while (Test lt 10
) cout ltlt Hello\n Test // BUT - Dont
do this! int test 0 while ( Test lt 10
) cout ltlt Hello\n Test
11Dont do this either! while( Remainder 1 ) //
assignment operator cout ltlt Enter a number
cin gtgt Num Remainder Num 2
12INFINITE LOOPS
// This is an infinite loop while( 1 ) // always
true cout ltlt Howdy doo to you! ltlt endl
13Programming Style and the while Loop
- If there is only one statement repeated by the
loop, it should appear on the line after the
while statement and be indented one additional
level - If the loop repeats a block, the block should
begin on the line after the while statement and
each line inside the braces should be indented
14 Counters
- A counter is a variable that is incremented or
decremented each time a loop iterates.
15User Controlled Loops
- Loops can be designed to repeat until the user
enters a particular value.
16 Keep a Running Total
- A running total is a sum of numbers that
accumulates with each iteration of a loop. The
variable used to keep the running total is called
an accumulator.
17 Sentinels
- A sentinel is a special value that marks the end
of a list of values.
18DO-WHILE is a POST-TEST LOOP int number 0 do
// loop until zero cout ltlt Enter a
non-zero number cin gtgt number while(
number ! 0 )
cout ltlt cin gtgt number
number ! 0
YES
NO
19The for Loop
- Ideal for situations that require a counter
because it has built-in expressions that
initialize and update variables. - for (initialization test update)
-
- statement(s)
20for (Num 1 Num lt 10 Num) cout ltlt Num ltlt
"\t\t" ltlt (Num Num) ltlt endl
num 1
YES
cout statement
num
NO
21Omitting the for Loops Expressions
- int Num 1
- for ( Num lt 10 Num)
- cout ltlt Num ltlt \t\t ltlt (Num Num) ltlt
endl
int Num 1 for ( Num lt 10 ) cout
ltlt Num ltlt \t\t ltlt (Num Num) ltlt endl
Num
// Infinite loop for ( ) cout ltlt
Hello World!\n
22LOOK OUT BELOW!!!
THIS LOOP WONT EXECUTE! for( x 11 x lt 10
x ) cout ltlt x ltlt endl WATCH THE DOUBLE
INCREMENT HERE!! for( x 1 x lt 10 x
) cout ltlt x ltlt ( x x ) ltlt endl x DONT
DO THIS!!!! NOBODY!!! NO BODY!!! for( x 11
x lt 10 x ) cout ltlt x ltlt endl
23Focus on Software Engineering Deciding Which
Loop to Use
- The while Loop
- A pre-test loop.
- Use when you do not want the loop to iterate if
the condition is false from the beginning. - Ideal if you want to use a sentinel.
- The do-while Loop
- A post-test loop.
- Use if you always want the loop to iterate at
least once. - The for Loop
- A pre-test loop.
- Automatically executes an update expression at
the end of each iteration. - Ideal for situations where a counter variable is
needed. - Used when the exact number of required iterations
is known.
24OTHER FORMS OF THE UPDATE EXPRESSION
for( num 2 num lt 100 num 2) for( num
10 num gt 0 num-- ) Scarey? for( num 1
num lt 10 cout ltlt Num ltlt endl) for( num 1
num lt 10 num, cout ltlt pow(num, 2) ) for( int
num 1 num lt 10 cout ltlt Num ltlt endl) int
num 1 for( num lt 100) Cout ltlt Num ltlt
\t\t ltlt (num num ) ltlt endl Num for(
) cout ltlt Hello World\n // Infinite loop
25The comma Operator
expr1, expr2 // expr1 is evaluated first int a
0, b 1 // left to right associativity for(
int sum 0, i 1 i lt n i) sum i //
Another way to do the same thing for( int sum
0, i 1 i lt n sum i, i) // NOT the
same thing for( int sum 0, i 1 i lt n
i, sum i)
26THE for STATEMENT
// This is valid, but dont do it! for() for()
for() statement // This is better use
those braces!! for() for() for()
statement
27Nested Loops
- A loop that is inside another loop is called a
nested loop. - while(expr1)
-
- while(expr2)
-
- statements
- // end while doing expr2
- // end while doing expr1
28 Breaking Out of a Loop
- The break statement causes a loop to terminate
early.
29The continue Statement
- The continue statement causes a loop to stop its
current iteration and begin the next one.
30Using Loops for Input Validation
- Loops can be used to create input routines that
repeat until acceptable data is entered.
31THE break AND continue STATEMENTS
for( expr1 expr2 expr3 ) expr1 while(
expr2 ) statements continue goto
next more statements more statements n
ext expr3