0% found this document useful (0 votes)
16 views11 pages

IM ch05

Chapter 5 of 'Java Programming: From Problem Analysis to Program Design' focuses on control structures related to repetition, including various types of loops such as while, for, and do...while. It covers how to use and nest these structures, along with break and continue statements, while providing teaching tips, examples, and quizzes to reinforce learning. The chapter also emphasizes the importance of avoiding bugs and understanding when to use different looping structures based on the known conditions of repetition.

Uploaded by

Uclm Engineering
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

IM ch05

Chapter 5 of 'Java Programming: From Problem Analysis to Program Design' focuses on control structures related to repetition, including various types of loops such as while, for, and do...while. It covers how to use and nest these structures, along with break and continue statements, while providing teaching tips, examples, and quizzes to reinforce learning. The chapter also emphasizes the importance of avoiding bugs and understanding when to use different looping structures based on the known conditions of repetition.

Uploaded by

Uclm Engineering
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Programming: From Problem Analysis to Program Design, 5th Edition 5-1

Chapter 5

Control Structures II: Repetition

At a Glance

Instructor’s Manual Table of Contents


• Overview

• Chapter Objectives

• Teaching Tips

• Quick Quizzes

• Class Discussion Topics

• Additional Projects

• Additional Resources

• Key Terms
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-2

Lecture Notes

Overview
In this chapter, students will learn about other control structures in more detail, mainly the
control structures that implement repetition in a program. These include looping, counter-
controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetitions. The student
will learn how to use and nest these structures, as well as use break and continue
statements.

Chapter Objectives
In this chapter, students will:
• Learn about repetition (looping) control structures
• Explore how to construct and use counter-controlled, sentinel-controlled, flag-controlled,
and EOF-controlled repetition structures
• Examine break and continue statements
• Learn how to avoid bugs by avoiding patches
• Discover how to form and use nested control structures

Teaching Tips
Why Is Repetition Needed?
1. Explain that there are many situations in which it is necessary to repeat a set of
statements, and give examples of these types of situations.

2. Explain that Java has three repetition, or looping, structures that let you repeat
statements over and over again until certain conditions are met: while, for, and
do…while.

while Looping (Repetition) Structure

1. Explain that the reserved word while can be used to repeat a set of statements until a
certain condition is met, and explain the syntax for the while loop.

2. Explain that the loop condition acts as a decision maker and is a logical expression. This
logical expression provides an entry condition for the loop.

3. Explain that the statements in the body of the loop can be either simple or compound
statements.
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-3

4. Explain that if the loop condition initially evaluates to true, the statement executes. Then
the loop condition is reevaluated, and the body of the loop is executed until the condition
evaluates to false. Use Figure 5-1 to show the flow of execution of a while loop.

Teaching Point out that in while loops, if the loop condition is false initially, the loop
Tip body will never execute.

Teaching It may be helpful to students to explain looping concepts using a concrete


Tip example.

5. Point out that a loop that continues to execute endlessly is called an infinite loop.

6. Explain that a variable in the loop condition that is used to determine whether the body
of the loop will execute is called a loop control variable.

7. Walk through Example 5-1 to explain how a while loop works.

Designing while Loops

1. Explain that the body of a while loop executes only when the expression in the while
statement evaluates to true. Typically, the expression checks whether a variable(s),
called the loop control variable (LCV), satisfies certain conditions.

2. Explain that the LCV must be properly initialized before the while loop, and its value
should eventually make the logical expression evaluate to false. We do this by updating
the LCV in the body of the while loop.

3. Walk through Example 5-2.

Counter-Controlled while Loops

1. Explain that a counter-controlled while loop is appropriate when the number of loop
executions is known in advance.

2. Explain that to make a counter-controlled while loop, a counter is initialized to 0


before the while statement and then incremented in the loop body to keep track of how
many times the body has been executed.

3. Explain the syntax for a counter-controlled while loop, and walk through Example 5-3.

Sentinel-Controlled while Loops

1. Explain what a loop sentinel is, and give an example of when it might be appropriate to
use a sentinel-controlled while loop.
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-4

2. Walk through Examples 5-4 and 5-5.

Flag-Controlled while Loops

1. Explain that a flag-controlled while loop uses a boolean variable to control the loop,
and explain the syntax of flag-controlled while loops.

2. Walk through Example 5-6.

Teaching Take time to explain the difference between a sentinel-controlled loop and a flag-
Tip controlled loop.

EOF-Controlled while Loops

1. Explain that if a loop is reading data from a file and a sentinel is not known, an EOF
(End Of File)-controlled while loop is appropriate.

2. Explain that in Java, the form of the EOF-controlled while loop depends on the type of
stream object used to input data into a program.

3. Explain the syntax of an EOF-controlled while loop that uses the Scanner object
console to input data, and walk through Examples 5-7 and 5-8.

Teaching Take time to explain the situations in which each type of loop would be most
Tip appropriate.

Teaching In the Windows console environment, the end-of-file marker is entered using
Tip CTRL+z. (Hold the CTRL key and press z.) In the UNIX environment, the end-
of-file marker is entered using CTRL+d. (Hold the CTRL key and press d.)

More on Expressions in while Statements

1. Explain that there are situations in which the expression in the while statement may be
controlled by multiple variables, and relational operators are often used to create more
complex logical conditions.

2. Give examples of complex expressions in while loop control statements.


Java Programming: From Problem Analysis to Program Design, 5th Edition 5-5

Programming Example: Fibonacci Number


1. Explain what a Fibonacci sequence is and the formula for finding the Fibonacci
sequence.

2. Explain that this program determines the nth Fibonacci number given the first two, and
the input to this program is the first two Fibonacci numbers and the position in the
sequence of the desired Fibonacci number (n).

3. Explain that the output to this program is the nth Fibonacci number.

4. Walk through the program analysis and algorithm design, and use the code example to
illustrate the concepts presented earlier in the chapter.

Quick Quiz 1
1. A loop is a control structure used for __________ of certain statements.
Answer: repetition

2. True or False: The statements found within a while loop always execute.
Answer: False

3. True or False: A flag-controlled while loop uses a boolean variable to control the
loop.
Answer: True

4. A loop that continues to execute endlessly is called a(n) ____ loop.


Answer: infinite

for Looping (Repetition) Structure

1. Explain the syntax for a for loop, and use Figure 5-4 to illustrate the loop execution.

2. Explain that a for loop is an implementation of a counter-controlled while loop, and


for this reason, the for loop is typically called a counted or indexed for loop.

3. Walk through Examples 5-9 through 5-14 to explain different aspects of for loops.

4. Review the update to the Fibonacci number program using a for loop in Example 5-15.
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-6

Teaching Emphasize that a while loop is general enough that it can be used to implement
Tip any other looping structure, and show an example of implementing a for loop
with a while loop.

Programming Example: Classify Numbers


1. Explain that this program reads a given set of integers and prints the number of odd and
even integers, as well as the number of zeros.

2. Explain that the input to this program is 20 integers (positive, negative, and zeros), but
the number of integers read can be easily modified, and the output is the number of
zeros, even numbers, and odd numbers.

3. Walk through the problem analysis and algorithm design, and use the program code to
illustrate for loops.

do…while Looping (Repetition) Structure

1. Explain the syntax of a do…while loop, and use Figure 5-5 to illustrate the flow of
execution of a do…while loop.

2. Explain that in a do…while loop, the loop body executes first, and then the loop
condition is evaluated.

3. Explain that if the loop condition evaluates to true, the body executes again, and as long
as the condition in a do...while statement is true, the body executes.

4. Emphasize that to avoid an infinite loop, you must make sure that the body of the loop
contains a statement that ultimately makes the expression evaluate to false and assures
that it exits properly.

5. Explain that in a while or for loop, the loop condition is evaluated before executing
the body of the loop and is called a pretest loop.

6. Explain that the loop condition in a do...while loop is evaluated after executing the
body of the loop, and do...while loops are called post-test loops.

7. Walk through Examples 5-16 through 5-18 to illustrate do…while loops.


Java Programming: From Problem Analysis to Program Design, 5th Edition 5-7

Take time to emphasize that in while and for loops, the body of the loop may
Teaching never execute, whereas in a do…while loop, the loop body is guaranteed to
Tip execute at least once. Provide examples of situations in which a do…while loop
would be preferable.

Choosing the Right Looping Structure

1. Explain that if you know or the program can determine in advance the number of
repetitions needed, the for loop is the correct choice.

2. Mention that if you do not know and the program cannot determine in advance the
number of repetitions needed, and it could be zero, the while loop is the right choice.

3. Explain that if you do not know and the program cannot determine in advance the
number of repetitions needed, and it is at least one, the do...while loop is the right
choice.

Quick Quiz 2
1. True or False: Any for loop can be written as a while loop.
Answer: True

2. In a pretest loop, the loop body __________.


a.) is guaranteed to execute at least once
b.) executes initially if the loop condition is true
b.) executes initially if the loop condition is false
c.) always uses a counter
Answer: b

4. In a post-test loop, the loop body __________.


a.) is guaranteed to execute at least once
b.) executes initially if the loop condition is true
c.) executes initially if the loop condition is false
d.) always uses a counter
Answer: a

5. True or False: A do…while loop is a pretest loop.


Answer: False

6. do…while loops are called __________ loops because the condition is evaluated after
the body of the loop is executed.
Answer: post-test
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-8

break and continue Statements

1. Explain that the break and continue statements alter the flow of control in a
program, and remind students they have used the break statement in the switch
structure.

2. Explain that the break statement is typically used to exit early from a loop and to skip
the remainder of a switch structure.

3. Explain that after the break statement executes, the program continues to execute with
the first statement after the structure.

4. Walk through the examples on pages 294-295 to illustrate how a break statement can
be used in place of an if statement within a loop.

Emphasize that when a break statement is used in a loop, the loop exits, and
Teaching program control continues after the end of the loop body. This is a good place to
Tip discuss when to use a break statement and when to write the code such that the
break statement is unnecessary.

5. Explain that the continue statement is used in while, for, and do...while
structures.

6. Explain that when the continue statement is executed in a loop, it skips the remaining
statements in the loop and proceeds with the next iteration of the loop.

7. Explain that in a while and do...while structure, the loop condition is evaluated
immediately after the continue statement.

8. Explain that in a for structure, the update statement is executed after the continue
statement, and then the loop condition executes.

Teaching
Tip Explain that the continue statement cannot be used in a switch statement.

Take time to explain the subtle difference between using a continue statement
Teaching in a while loop and using a continue statement in a for loop. Specifically,
Tip in a while loop, when the continue statement is executed, if the update
statement appears after the continue statement, the update statement is not
executed. In a for loop, the update statement always executes.
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-9

Teaching Note that Java also supports labeled break and labeled continue statements.
Tip (A link to more information is in the Additional Resources section of this
Instructor’s Manual.)

Avoiding Bugs by Avoiding Patches


1. Define a software patch as a piece of code written on top of an existing piece of code
intended to remedy a deficiency in the original code.

2. Explain that determining and correcting the original deficiency is a much better
approach.

3. Walk through the examples on page 296 to illustrate your explanation.

Debugging Loops

1. Discuss common loop-related errors, such as being off by one, and infinite loops.

Nested Control Structures


1. Walk through Examples 5-19 through 5-22 to illustrate how to nest looping structures,
and discuss why this might be useful.

Quick Quiz 3
1. True or False: continue and break statements should be used as often as possible to
make the code more understandable and easier to maintain.
Answer: False

2. In a while loop, the __________ statement causes the program flow to continue after
the end of the loop body.
Answer: break

3. In a for loop, the __________ statement causes the loop iteration to terminate, and
program flow continues at the next loop iteration.
Answer: continue

4. True or False: do…while loops cannot be nested.


Answer: False
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-10

Class Discussion Topics


1. When would it be appropriate to use a continue statement?

2. Why was Java designed such that using a continue statement in a for loop behaves
slightly differently than the continue statement in a while loop?

3. How much is too much when using break and continue statements? Why should
they be used in moderation?

4. If all loops can be written using a while loop, why does the language have other types
of loops?

Additional Projects
1. Write a program to take a number as input and determine if it is a prime number. (It
may be wise to limit the size of the input number.)

2. Write a program to find the least common denominator of two given numbers.

3. Write a program that determines whether two numbers are relatively prime.

Additional Resources
1. A tutorial on while and do…while loops:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/while.html

2. Infinite loops:
www.java-examples.com/infinite-loop-example

3. Information on the Java labeled break and labeled continue statements:


www.dickbaldwin.com/java/Java026.htm#thebreakandcontinuestatements

4. Web site about prime numbers that may be helpful with the Additional Projects:
http://primes.utm.edu/

Key Terms
¾ condition—see loop condition
¾ control expressions—Expressions that are enclosed within parentheses and control the
body of a for statement.
¾ counted for loop—A for loop that is used to implement counter-controlled loops.
¾ counter-controlled loop—A while loop that uses a counter to control the loop.
Java Programming: From Problem Analysis to Program Design, 5th Edition 5-11

¾ divisor—The number m if another number n=mt for some integer t; that is, when m
divides n, the remainder is 0.
¾ EOF (End of File) controlled while loop—A while loop that continues to execute
until the end-of-file marker is reached.
¾ Fibonacci number—A number in the Fibonacci sequence.
¾ Fibonacci sequence—A sequence of numbers such that the next number is the sum of
the previous two: 0,1,1,2,3,5,8,13,21,…
¾ flag-controlled loop—A while loop that uses a boolean variable to control the loop.
¾ flag variable—A variable that is used to control the execution of the while loop.
¾ indexed for loop—see counted for loop
¾ loop condition—An expression which is tested on each iteration of the loop. If the loop
condition evaluates to true, the body of the loop is executed. If the loop condition
evaluates to false, the loop terminates.
¾ loop control variable—A variable such as a sentinel variable or a counter variable that
is used in the loop condition.
¾ nesting—The action of putting one control structure statement inside another.
¾ pretest loop—A loop in which the loop condition is evaluated before executing the body
of the loop.
¾ post-test loop—A loop in which the loop condition is evaluated after executing the body
of the loop the first time. A do…while loop is an example of a post-test loop.
¾ sentinel—A variable that controls the execution of a loop by causing the loop to
terminate when the variable takes on a specific value.
¾ sentinel-controlled loop—A while loop that uses a sentinel to control the while
loop.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy