Unit 5 PowerPoint Slides

advertisement
EGR 2261 Unit 5
Control Structures II: Repetition



Read Malik, Chapter 5.
Homework #5 and Lab #5 due next
week.
Exam #1 next week.
Why Is Repetition Needed?
• Repetition allows efficient use of variables.
• It lets you process many values using a small
number of variables.
• For example, to add five numbers:
– Inefficient way: Declare a variable for each
number, input each number and add them.
– Efficient way: Create a loop that reads a number
into a variable and adds it to another variable that
contains the running total of the numbers.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
2
Example: Inefficient Way of Adding
Five Numbers
Example: Efficient Way of Adding
Five Numbers
Three Kinds of Loops
while Loop
• Syntax of the while statement:
• statement can be simple or compound.
• expression acts as a decision maker and is
usually a logical expression.
• statement is called the body of the loop.
• The parentheses are part of the syntax.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
6
while Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
7
while Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
8
Single-Stepping
•Instead of using Ctrl+F5 to run a program at
full speed, you can use F10 (or Debug > Step
Over) to single-step a program.
•This executes the program one statement at a
time, and lets you examine the effect of each
statement before going on to the next one.
•It's a powerful debugging technique for any
program, particularly for programs that contain
loops.
•To quit single-stepping, use Shift+F5 (or
Debug > Stop Debugging).
Terminology: Loop Control Variable,
Infinite Loop
• The variable i in Example 5-1 is called the
loop control variable.
• An infinite loop continues to execute
endlessly. Infinite loops are generally
undesirable.
– They can be avoided by including statements in
the loop body that assure that expression is
eventually false.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
10
while Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
11
Four Common Ways of Using
while Loops
•There are many ways to use while loops.
•The following slides discuss four common
ways. These differ mainly in the value that is
used for the loop control variable:
•Counter-controlled while loops
•Sentinel-controlled while loops
•Flag-controlled while loops
•EOF-controlled while loops
Case 1: Counter-Controlled while
Loops
• Use a counter-controlled while loop when
you know exactly how many times the
statements need to be executed. Previous
examples demonstrated this kind of loop.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
13
Case 2: Sentinel-Controlled while
Loops
• In a sentinel-controlled while loop, the
condition checks to see whether a variable is
equal to a special value called the sentinel
value.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
14
Example 5-5: Telephone Digits
• The textbook’s Example 5-5 (p. 278) provides
an example of a sentinel-controlled loop.
• The program converts uppercase letters to
their corresponding telephone digit. It loops
until the users enters ‘#’ to indicate that she
wants to quit the program.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
15
Case 3: Flag-Controlled while Loops
• Flag-controlled while loop: uses a bool
variable to control the loop.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
16
Number Guessing Game
• The textbook’s Example 5-6 (p. 282)
implements a number guessing game using a
flag-controlled while loop.
• It also uses the function rand of the header
file cstdlib to generate a random number.
– rand() returns an int value between 0 and
32767
– To convert to an integer >= 0 and < 100:
• rand() % 100
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
17
Case 4: EOF-Controlled while Loops
• EOF stands for end-of-file.
• An EOF-controlled while loop reads data
from a file until it encounters a special end-offile character that is automatically included at
the end every file on a disk.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
18
eof Function
• The eof function looks in an input stream for
the end-of-file character.
• Syntax for the eof function:
inputFile.eof();
• The expression above is false as long as we
haven’t reached the end-of-file character;
otherwise the expression is true.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
19
Example: EOF-Controlled While
Loop
More on Expressions in while
Statements
• The expression in a while statement can be
complex.
– Example:
while (i < 20 || !infile.eof())
{
. . .
}
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
21
Three Kinds of Loops
for Loop
• for loop: also called a counted loop.
• Syntax of the for statement:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
23
for Loop (cont.)
• The initial statement is executed only once,
when execution of the for loop begins.
• The loop condition is checked at the beginning
of each repetition. If it is true, the loop is repeated.
If it is false, control passes to the first statement after
the end of the loop.
• The update statement is executed at the end of
each repetition.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
24
for Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
25
for Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
26
for Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
27
for Loop (cont’d.)
• The following is a legal (but infinite) for loop:
for (;;)
cout << "Hello" << endl;
• The following is also legal, but probably not what the
programmer wanted:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
28
for Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
29
for Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
30
Three Kinds of Loops
do…while Loop
• Syntax of a do...while loop:
• The statement executes first, and
then the expression is evaluated.
• The statement can be simple or compound.
• The statement always executes at least once.
• As long as expression is true, loop continues.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
32
do…while Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
33
do…while Loop (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
34
Three Kinds of Loops
Choosing the Right Kind of Loop
• All three loops have their place in C++.
– If you can determine in advance the number of
repetitions needed, the for loop is the best
choice.
– If you cannot determine in advance the number of
repetitions needed, and it could be zero, use a
while loop.
– If you cannot determine in advance the number of
repetitions needed, and it is at least one, use a
do...while loop.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
36
break and continue Statements
• break and continue alter the flow of
execution.
• break statement is used for two purposes:
– To skip the remainder of a switch structure.
– To exit early from a loop.
• Can eliminate the use of certain (flag) variables.
• After break executes, the program continues
with the first statement after the structure.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
37
Example: Using break in a while
Loop
break and continue Statements
(cont’d.)
• When continue is executed in a loop, it
skips the remaining statements in the loop
and proceeds with the loop’s next iteration.
• continue can be used in any kind of loop-while, for, or do…while.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
39
Example: Using continue in a
while Loop
Nested Loops
• To create the following pattern:
*
**
***
****
*****
• We can use the following code:
for (i = 1; i <= 5 ; i++)
{
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
41
Debugging Loops
• Loops are harder to debug than sequence and
selection structures.
• While debugging, insert temporary cout
statements in your loop so that you can see
how the values of variables are changing as
the loop repeats—or use single-stepping.
• The most common error associated with loops
is the off-by-one error (doing a counted loop
one too many times or one too few times).
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
42
Download