340 likes | 703 Views
Java Programming: Program Design Including Data Structures. 2. Chapter Objectives. Learn about repetition (looping) control structuresExplore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structuresExamine break and continue statemen
E N D
1. Chapter 5: Control Structures II Java Programming:
Program Design Including Data Structures
2. Java Programming: Program Design Including Data Structures 2 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 control structures
3. Java Programming: Program Design Including Data Structures 3 Why Is Repetition Needed? Many situations require the same statement to be executed several times
Example:
Finding average grades for students in a class
4. Java Programming: Program Design Including Data Structures 4 The while Looping (Repetition) Structure Syntax:
while (expression)
statement
Expression is always true in an infinite loop
Statements must change value of expression to false
5. Java Programming: Program Design Including Data Structures 5 The while Looping (Repetition) Structure (continued) Example 5-1
i = 0; //Line 1
while (i <= 20) //Line 2
{
System.out.print(i + " "); //Line 3
i = i + 5; //Line 4
}
System.out.println(); //Line 5
Output
0 5 10 15 20
6. Java Programming: Program Design Including Data Structures 6 The while Looping (Repetition) Structure (continued) while loops are written in the following form:
//initialize the loop control variable(s)
while (expression) //expression tests control
{ //variable
.
//update the loop control variable(s)
.
.
}
7. Java Programming: Program Design Including Data Structures 7 Counter-Controlled while Loop Used when exact number of entry pieces is known
General form:
int N = //value input by user or specified
//in program
int counter = 0;
while (counter < N)
{
.
.
.
counter++;
.
.
.
}
8. Java Programming: Program Design Including Data Structures 8 Sentinel-Controlled while Loop Used when exact number of entry pieces is unknown, but last entry (special/sentinel value) is known
General form:
Input the first data item into variable;
while (variable != sentinel)
{
.
.
.
input a data item into variable;
.
.
.
}
9. Java Programming: Program Design Including Data Structures 9 Flag-Controlled while Loop Boolean value used to control loop
General form:
boolean found = false;
while (!found)
{
.
.
.
if (expression)
found = true;
.
.
.
}
10. Java Programming: Program Design Including Data Structures 10 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 uses the Scanner object console to input data, console acts at the loop control variable
11. Java Programming: Program Design Including Data Structures 11 EOF (End of File)-Controlled while Loop (continued) The method hasNext, of the class Scanner, returns true if there is an input in the input stream; otherwise, it returns false
The expression console.hasNext() acts as the loop condition
Expressions such as console.nextInt() update the value of the loop condition
12. Java Programming: Program Design Including Data Structures 12 EOF-Controlled while Loop (continued) General form of the EOF-controlled while loop that uses the Scanner object console to input data:
while (console.hasNext())
{
//Get the next input and store in an
//appropriate variable
//Process data
}
13. Java Programming: Program Design Including Data Structures 13 EOF-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:
while (inFile.hasNext())
{
//Get the next input and store in an
//appropriate variable
//Process data
}
14. Java Programming: Program Design Including Data Structures 14 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, total amount deposited, number of deposits, total amount withdrawn, number of withdrawals
15. Java Programming: Program Design Including Data Structures 15 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)
16. Java Programming: Program Design Including Data Structures 16 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 number 2
int nthFibonacci = Position of nth Fibonacci number
Output: nth Fibonacci number
17. Java Programming: Program Design Including Data Structures 17 Programming Example: Fibonacci Number (Solution) if (nthFibonacci == 1)
current = previous1;
else if (nthFibonacci == 2)
current = previous2;
else
{
counter = 3;
while (counter <= nthFibonacci)
{
current = previous2 + previous1;
previous1 = previous2;
previous2 = current;
counter++;
}
}
18. Java Programming: Program Design Including Data Structures 18 The for Looping (Repetition) Structure Specialized form of while loop
Simplifies the writing of count-controlled loops
Syntax:
for (initial statement; loop condition;
update statement)
statement
19. Java Programming: Program Design Including Data Structures 19 The for Looping (Repetition) Structure (continued)
Execution:
Initial statement executes
Loop condition is evaluated
If loop condition evaluates to true, execute for loop statement and execute update statement
Repeat until loop condition is false
20. Java Programming: Program Design Including Data Structures 20 The for Looping (Repetition) Structure (continued)
21. Java Programming: Program Design Including Data Structures 21 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();
22. Java Programming: Program Design Including Data Structures 22 The for Looping (Repetition) Structure (continued)
Example 5-9
The following for loop outputs the word Hello and a star (on separate lines) five times:
for (i = 1; i <= 5; i++)
{
System.out.println("Hello");
System.out.println("*");
}
23. Java Programming: Program Design Including Data Structures 23 The for Looping (Repetition) Structure (continued) 2. The following for loop outputs the word Hello five times and the star only once:
for (i = 1; i <= 5; i++)
System.out.println("Hello");
System.out.println("*");
24. Java Programming: Program Design Including Data Structures 24 The for Looping (Repetition) Structure (continued) Does not execute if initial condition is false
Update expression changes value of loop control variable, eventually making it false
If loop condition is always true, result is an infinite loop
25. Java Programming: Program Design Including Data Structures 25 The for Looping (Repetition) Structure (continued) Infinite loop can be specified by omitting all three control statements
If loop condition is omitted, it is assumed to be true
for statement ending in semicolon is empty
26. Java Programming: Program Design Including Data Structures 26 Programming Example: Classify Numbers Input: N integers (positive, negative, and zeros)
int N = 20; //N easily modified
Output: Number of 0s, number of even integers, number of odd integers
27. Java Programming: Program Design Including Data Structures 27 Programming Example: Classify Numbers (Solution) for (counter = 1; counter <= N; counter++)
{
number = console.nextInt();
System.out.print(number + " ");
switch (number % 2)
{
case 0: evens++;
if (number == 0)
zeros++;
break;
case 1:
case -1: odds++;
} //end switch
} //end for loop
28. Java Programming: Program Design Including Data Structures 28 The do
while Loop (Repetition) Structure Syntax:
do
statement
while (expression);
Statements are executed before expression
Statements are executed at least once and then continued if expression is true
29. Java Programming: Program Design Including Data Structures 29 do
while Loop (Post-Test Loop)
30. Java Programming: Program Design Including Data Structures 30 break Statements Used to exit early from a loop
Used to skip remainder of switch structure
Can be placed within if statement of a loop
If condition is met, loop is exited immediately
31. Java Programming: Program Design Including Data Structures 31 continue Statements Used in while, for, and do...while structures
When executed in a loop:
Remaining statements in the loop are skipped
Proceeds with the next iteration of the loop
When executed in a while/do
while structure, expression is evaluated after continue
In a for structure, the update statement is executed after continue; the loop condition then executes
32. Java Programming: Program Design Including Data Structures 32 Nested Control Structures Provides new power, subtlety, and complexity
if, if
else, and switch structures can be placed within while loops
for loops can be found within other for loops
33. Java Programming: Program Design Including Data Structures 33 Nested Control Structures (Example) for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
System.out.print(" *");
System.out.println();
}
Output:
*
**
***
****
*****
34. Java Programming: Program Design Including Data Structures 34 Chapter Summary Looping mechanisms:
Counter-controlled while loop
Sentinel-controlled while loop
Flag-controlled while loop
EOF-controlled while loop
for loop
do
while loop
break statement
continue statement
Nested control structures