Chapter 5 Programming I
Chapter 5 Programming I
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain
1
product or service or otherwise on a password-protected website for classroom
Objectives (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 2
product or service or otherwise on a password-protected website for classroom use.
Objectives (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 3
product or service or otherwise on a password-protected website for classroom use.
Why Is Repetition Needed?
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 4
product or service or otherwise on a password-protected website for classroom use.
while Looping (Repetition) Structure (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 5
product or service or otherwise on a password-protected website for classroom use.
while Looping (Repetition) Structure (2 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 6
product or service or otherwise on a password-protected website for classroom use.
while Looping (Repetition) Structure (3 of 3)
EXAMPLE 5-1
Consider the following C++ program segment:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 7
product or service or otherwise on a password-protected website for classroom use.
while Looping (Repetition) Structure (cont’d.)
EXAMPLE 5-2
Consider the following C++ program segment:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 8
product or service or otherwise on a password-protected website for classroom use.
Case 1: Counter-Controlled while Loops
• When you know exactly how many times the statements need to be executed
• Use a counter-controlled while loop
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 9
product or service or otherwise on a password-protected website for classroom use.
Case 2: Sentinel-Controlled while Loops
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 10
product or service or otherwise on a password-protected website for classroom use.
Example 5-5: Telephone Digits
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 11
product or service or otherwise on a password-protected website for classroom use.
Case 3: Flag-Controlled while Loops
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 12
product or service or otherwise on a password-protected website for classroom use.
Number Guessing Game
• To convert to an integer
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 13
product or service or otherwise on a password-protected website for classroom use.
Case 4: EOF-Controlled while Loops (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 14
product or service or otherwise on a password-protected website for classroom use.
Case 4: EOF-Controlled while Loops (2 of 2)
EXAMPLE 5-7
The following code uses an EOF-controlled while loop to find the sum of a set of numbers:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 15
product or service or otherwise on a password-protected website for classroom use.
eof Function
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 16
product or service or otherwise on a password-protected website for classroom use.
More on Expressions in while Statements
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 17
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Fibonacci Number (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 18
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Fibonacci Number (2 of 3)
• Fibonacci sequence
• nth Fibonacci number
• a2 = 1
• a1 = 1
• Determine the nth number an, n >= 3
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 19
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Fibonacci Number (3 of 3)
• Suppose a2 = 6 and a1 = 3
• a3 = a2 + a1 = 6 + 3 = 9
• a4 = a3 + a2 = 9 + 6 = 15
• Write a program that determines the nth Fibonacci number, given the first two
numbers
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 20
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Input and Output
• Input: first two Fibonacci numbers and the desired Fibonacci number
• Output: nth Fibonacci number
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 21
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Problem Analysis and Algorithm Design
• Algorithm
• Get the first two Fibonacci numbers
• Get the desired Fibonacci number
- Get the position, n, of the number in the sequence
• Calculate the next Fibonacci number
- Add the previous two elements of the sequence
• Repeat Step 3 until the nth Fibonacci number is found
• Output the nth Fibonacci number
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 22
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Variables
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 23
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Main Algorithm (1 of 4)
• Prompt the user for the first two numbers—that is, previous1 and
previous2
• Read (input) the first two numbers into previous1 and previous2
• Output the first two Fibonacci numbers
• Prompt the user for the position of the desired Fibonacci number
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 24
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Main Algorithm (2 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 25
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Main Algorithm (3 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 26
product or service or otherwise on a password-protected website for classroom use.
Programming Example: Main Algorithm (4 of 4)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 27
product or service or otherwise on a password-protected website for classroom use.
for Looping (Repetition) Structure (1 of 7)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 28
product or service or otherwise on a password-protected website for classroom use.
for Looping (Repetition) Structure (2 of 7)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 29
product or service or otherwise on a password-protected website for classroom use.
for Looping (Repetition) Structure (3 of 7)
EXAMPLE 5-9
The following for loop prints the first 10 nonnegative integers:
The initial statement, i = 0;, initializes the int variable i to 0. Next, the
loop condition, i < 10, is evaluated. Because 0 < 10 is true, the print statement
executes and outputs 0. The update statement, i++, then executes, which sets
the value of i to 1. Once again, the loop condition is evaluated, which is still true,
and so on. When i becomes 10, the loop condition evaluates to false, the for
loop terminates, and the first statement following the for loop executes.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 30
product or service or otherwise on a password-protected website for classroom use.
for Looping (Repetition) Structure (4 of 7)
EXAMPLE 5-10
1. The following for loop outputs Hello! and a star (on separate lines) five times:
The semicolon at the end of the for statement (before the output statement, Line
1)terminates the for loop. The action of this for loop is empty, that is, null. As in
Example 5-10(2), the indentation of Line 2 is
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 32
product or service or otherwise on a password-protected website for classroom use.
for Looping (Repetition) Structure (6 of 7)
EXAMPLE 5-12
You can count backward using a for loop if the for loop control expressions are set
correctly.
For example, consider the following for loop:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 33
product or service or otherwise on a password-protected website for classroom use.
for Looping (Repetition) Structure (7 of 7)
EXAMPLE 5-13
You can increment (or decrement) the loop control variable by any fixed number. In the
following for loop, the variable is initialized to 1; at the end of the for loop, i is
incremented by 2. This for loop outputs the first 10 positive odd integers.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 34
product or service or otherwise on a password-protected website for classroom use.
do…while Looping (Repetition) Structure (1 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 35
product or service or otherwise on a password-protected website for classroom use.
do…while Looping (Repetition) Structure (2 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 36
product or service or otherwise on a password-protected website for classroom use.
do…while Looping (Repetition) Structure (3 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 37
product or service or otherwise on a password-protected website for classroom use.
do…while Looping (Repetition) Structure (4 of 6)
EXAMPLE 5-18
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 38
product or service or otherwise on a password-protected website for classroom use.
do…while Looping (Repetition) Structure (5 of 6)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 39
product or service or otherwise on a password-protected website for classroom use.
do…while Looping (Repetition) Structure (6 of 6)
EXAMPLE 5-19
Consider the following two loops:
In (a), the while loop produces nothing, the statement never executes. In (b), the
do. . .while loop outputs the number 11 and also changes the value of i to 16.
This is expected because in a do. . .while, the statement must always execute at
least once.
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 40
product or service or otherwise on a password-protected website for classroom use.
Choosing the Right Looping Structure
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 41
product or service or otherwise on a password-protected website for classroom use.
break and continue Statements (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 42
product or service or otherwise on a password-protected website for classroom use.
break and continue Statements (2 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 43
product or service or otherwise on a password-protected website for classroom use.
Nested Control Structures (1 of 2)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 44
product or service or otherwise on a password-protected website for classroom use.
Nested Control Structures (2 of 2)
• What is the result if we replace the first for statement with this?
• Answer:
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 45
product or service or otherwise on a password-protected website for classroom use.
Avoiding Bugs by Avoiding Patches
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 46
product or service or otherwise on a password-protected website for classroom use.
Debugging Loops
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 47
product or service or otherwise on a password-protected website for classroom use.
Quick Review (1 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 48
product or service or otherwise on a password-protected website for classroom use.
Quick Review (2 of 3)
• In a while loop:
• The expression is the decision maker
• The statement is the body of the loop
• A while loop can be:
• Counter-controlled
• Sentinel-controlled
• EOF-controlled
• In the Windows console environment, the end-of-file marker is entered using
Ctrl+z
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 49
product or service or otherwise on a password-protected website for classroom use.
Quick Review (3 of 3)
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 50
product or service or otherwise on a password-protected website for classroom use.