Upload
reijen-inciong
View
218
Download
0
Embed Size (px)
Citation preview
8/3/2019 Programming Lecture 5
1/33
Java Programming: From Problem
Analysis to Program Design, 3e
Chapter 5
Control Structures II: Repetition
8/3/2019 Programming Lecture 5
2/33
2Java Programming: From Problem Analysis to Program Design, 3e
Chapter Objectives Learn about repetition (looping) control structures
Explore how to construct and use count-
controlled, sentinel-controlled, flag-controlled,and EOF-controlled repetition structures
Examine break and continue statements
Discover how to form and use nested controlstructures
8/3/2019 Programming Lecture 5
3/33
3Java Programming: From Problem Analysis to Program Design, 3e
Why Is Repetition Needed? There are many situations in which the
same statements need to be executed several
times
Example
Formulas used to find average grades for
students in a class
8/3/2019 Programming Lecture 5
4/33
4Java Programming: From Problem Analysis to Program Design, 3e
The while Looping (Repetition)
Structure Syntaxwhile (expression)
statement
Expression is always true in an infinite loop
Statements must change value of expression to
false
8/3/2019 Programming Lecture 5
5/33
5Java Programming: From Problem Analysis to Program Design, 3e
The while Looping (Repetition)
Structure (continued)
8/3/2019 Programming Lecture 5
6/33
6Java Programming: From Problem Analysis to Program Design, 3e
The while Looping (Repetition)
Structure (continued)Example 5-1
i = 0; //Line 1
while (i
8/3/2019 Programming Lecture 5
7/33
7Java Programming: From Problem Analysis to Program Design, 3e
The while Looping (Repetition)
Structure (continued) Typically, while loops are written in the
following form:
8/3/2019 Programming Lecture 5
8/33
8Java Programming: From Problem Analysis to Program Design, 3e
Counter-Controlled while Loop
Used when exact number of data or entry piecesis known
General form:
8/3/2019 Programming Lecture 5
9/33
9Java Programming: From Problem Analysis to Program Design, 3e
Sentinel-Controlled while Loop
Used when exact number of entry pieces isunknown but last entry (special/sentinel value) isknown
General form:
8/3/2019 Programming Lecture 5
10/33
10Java Programming: From Problem Analysis to Program Design, 3e
Flag-Controlled while Loop
Boolean value used to control loop
General form:
8/3/2019 Programming Lecture 5
11/33
11Java Programming: From Problem Analysis to Program Design, 3e
EOF(End of File)-Controlled while
Loop Used when input is from files
Sentinel value is not always appropriate
In an EOF-controlled while loop that usesthe Scanner object console to inputdata, console acts at the loop controlvariable
The method hasNext, of the classScanner, returns true if there is an inputin the input stream; otherwise it returnsfalse
8/3/2019 Programming Lecture 5
12/33
12
Java Programming: From Problem Analysis to Program Design, 3e
EOF(End of File)-Controlledwhile Loop (continued)
The expression console.hasNext() evaluatesto true if there is an input in the input stream;otherwise it returns false
Expressions such as console.nextInt() act
as the loop condition A general form of the EOF-controlled while loop
that uses the Scanner object console to inputdata is of the form:
8/3/2019 Programming Lecture 5
13/33
13Java Programming: From Problem Analysis to Program Design, 3e
EOF(End of File)-Controlled
while Loop (continued) Suppose that inFile is a Scanner object
initialized to the input file; in this case, the EOF-controlled while loop takes the following form:
8/3/2019 Programming Lecture 5
14/33
14
Java Programming: From Problem Analysis to Program Design, 3e
Programming Example:
Checking Account Balance Input file: customers account number,
account balance at beginning of month,
transaction type (withdrawal, deposit,interest), transaction amount
Output: account number, beginning balance,ending balance, total interest paid, totalamount deposited, number of deposits, totalamount withdrawn, number of withdrawals
8/3/2019 Programming Lecture 5
15/33
15
Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Checking
Account Balance (continued)
Solution
Read data
EOF-controlled loop
switch structure of transaction types
Determine action (add to balance or subtract
from balance depending on transaction type)
8/3/2019 Programming Lecture 5
16/33
16
Java Programming: From Problem Analysis to Program Design, 3e
Programming Example:
Fibonacci Number Fibonacci formula for any Fibonacci sequence:
an = an-1 + an-2
Input: first two Fibonacci numbers in sequence,position in sequence of desired Fibonacci number
(n) int previous1 = Fibonacci number 1
int previous2 = Fibonacci number2
int nthFibonacci = position of nth Fibonacci number
Output: nth Fibonacci number
8/3/2019 Programming Lecture 5
17/33
17
Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Fibonacci
Number (Solution)if (nthFibonacci == 1)
current = previous1;
else if (nthFibonacci == 2)
current = previous2;
else
{
counter = 3;
while (counter
8/3/2019 Programming Lecture 5
18/33
18
Java Programming: From Problem Analysis to Program Design, 3e
The for Looping (Repetition)
Structure Specialized form ofwhile loop
Simplifies the writing of count-controlled loops
Syntax:for (initial expression; logical expression;
update expression)
statement
8/3/2019 Programming Lecture 5
19/33
19
Java Programming: From Problem Analysis to Program Design, 3e
The for Looping (Repetition)
Structure (continued) Execution
Initial statement executes
The loop condition evaluated
If loop condition evaluates to true, executefor loop statement and execute updatestatement
Repeat until loop condition is false
8/3/2019 Programming Lecture 5
20/33
20Java Programming: From Problem Analysis to Program Design, 3e
The for Looping (Repetition)
Structure (continued)
8/3/2019 Programming Lecture 5
21/33
21Java Programming: From Problem Analysis to Program Design, 3e
The for Looping (Repetition)
Structure (continued)
Example 5-8
The following for loop prints the first 10 nonnegative
integers:
for (i = 0; i < 10; i++)
System.out.print(i + " ");
System.out.println();
8/3/2019 Programming Lecture 5
22/33
22
Java Programming: From Problem Analysis to Program Design, 3e
The for Looping (Repetition)
Structure (continued)Example 5-91. The following for loop outputs the word Hello and a star (on separate
lines) five times:
for (i = 1; i
8/3/2019 Programming Lecture 5
23/33
2
3Java Programming: From Problem Analysis to Program Design, 3e
The for Looping (Repetition)
Structure (continued) Does not execute if initial condition isfalse
Update expression changes value of loopcontrol variable, eventually making it false
If logical expression is always true, resultis an infinite loop
Infinite loop can be specified by omitting allthree control statements
8/3/2019 Programming Lecture 5
24/33
The for Looping (Repetition)
Structure (continued)
If logical expression is omitted, it isassumed to be true
for statement ending in semicolon isempty
Java Programming: From Problem Analysis to Program Design, 3e24
8/3/2019 Programming Lecture 5
25/33
25Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Classify
Numbers Input: N integers (positive, negative, and zeros)
int N = 20; //N easily modified
Output: number of0s, number of even integers,
number of odd integers
8/3/2019 Programming Lecture 5
26/33
26Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Classify
Numbers (Solution)for (counter = 1; counter
8/3/2019 Programming Lecture 5
27/33
27Java Programming: From Problem Analysis to Program Design, 3e
The dowhile Loop
(Repetition) Structure Syntax:
Statements executed first, then logical
expression evaluated Statement(s) executed at least once then
continued iflogical expression is true
8/3/2019 Programming Lecture 5
28/33
28Java Programming: From Problem Analysis to Program Design, 3e
dowhile Loop (Post-test Loop)
8/3/2019 Programming Lecture 5
29/33
29Java Programming: From Problem Analysis to Program Design, 3e
breakStatements
Used to exit early from a loop
Used to skip remainder ofswitch
structure
Can be placed within if statement of a loop
If condition is met, loop exited immediately
8/3/2019 Programming Lecture 5
30/33
30Java Programming: From Problem Analysis to Program Design, 3e
continue Statements
Used in while, for, and do...whilestructures
When executed in a loop, the remaining
statements in the loop are skipped; proceedswith the next iteration of the loop
When executed in a while/dowhilestructure, expression evaluated immediately
aftercontinue statement In a for structure, the update expression isexecuted after the continue statement;then the loop condition executes
8/3/2019 Programming Lecture 5
31/33
31Java Programming: From Problem Analysis to Program Design, 3e
Nested Control Structures Provides new power, subtlety, and
complexity
if, ifelse, and switch structures can
be placed within while loops
for loops can be found within otherfor
loops
8/3/2019 Programming Lecture 5
32/33
32Java Programming: From Problem Analysis to Program Design, 3e
Nested Control Structures (Example)
for (int i = 1; i
8/3/2019 Programming Lecture 5
33/33
33Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary
Looping Mechanisms Counter-controlled while loop
Sentinel-controlled while loop
Flag-controlled while loop EOF-controlled while loop
for loop
dowhile loop
break statements continue statements
Nested control structures