Your SlideShare is downloading. ×
0
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition
Chapter Objectives <ul><li>Learn about repetition (looping) control structures </li></ul><ul><li>Explore how to construct ...
Why Is Repetition Needed? <ul><li>There are many situations in which the same statements need to be executed several times...
The  while  Looping (Repetition) Structure <ul><li>Syntax </li></ul><ul><li>while  (expression) </li></ul><ul><li>  statem...
The  while  Looping (Repetition) Structure (continued) Java Programming: From Problem Analysis to Program Design, 3e
The  while  Looping (Repetition) Structure (continued) <ul><li>Example 5-1 </li></ul><ul><li>i = 0;  //Line 1 </li></ul><u...
The  while  Looping (Repetition) Structure (continued) <ul><li>Typically,  while  loops are written in the following form:...
Counter-Controlled  while  Loop <ul><li>Used when exact number of data or entry pieces is known </li></ul><ul><li>General ...
Sentinel-Controlled  while  Loop <ul><li>Used when exact number of entry pieces is unknown but last entry (special/sentine...
Flag-Controlled  while  Loop <ul><li>Boolean value used to control loop </li></ul><ul><li>General form: </li></ul>Java Pro...
EOF(End of File)-Controlled  while  Loop <ul><li>Used when input is from files </li></ul><ul><li>Sentinel value is not alw...
EOF(End of File)-Controlled  while  Loop (continued) <ul><li>The expression  console.hasNext()  evaluates to  true  if the...
EOF(End of File)-Controlled  while  Loop (continued) <ul><li>Suppose that  inFile  is a  Scanner  object initialized to th...
Programming Example: Checking Account Balance <ul><li>Input file:  customer’s account number, account balance at beginning...
Programming Example: Checking Account Balance (continued) <ul><li>Solution   </li></ul><ul><ul><li>Read data </li></ul></u...
Programming Example: Fibonacci Number <ul><li>Fibonacci formula for any Fibonacci sequence: </li></ul><ul><li>a n  =  a n ...
Programming Example: Fibonacci Number (Solution) <ul><li>if  (nthFibonacci == 1) </li></ul><ul><li>current = previous1; </...
The  for  Looping (Repetition) Structure <ul><li>Specialized form of  while  loop </li></ul><ul><li>Simplifies the writing...
The  for  Looping (Repetition) Structure (continued) <ul><li>Execution </li></ul><ul><ul><li>Initial statement executes   ...
The  for  Looping (Repetition) Structure (continued) Java Programming: From Problem Analysis to Program Design, 3e
The  for  Looping (Repetition) Structure (continued) <ul><li>Example 5-8 </li></ul><ul><li>The following for loop prints t...
The  for  Looping (Repetition) Structure (continued) <ul><li>Example 5-9 </li></ul><ul><li>The following  for  loop output...
The  for  Looping (Repetition) Structure (continued) <ul><li>Does not execute if initial condition is  false </li></ul><ul...
The  for  Looping (Repetition) Structure (continued) <ul><li>If logical expression is omitted, it is assumed to be  true <...
Programming Example: Classify Numbers <ul><li>Input:  N   integers (positive, negative, and zeros)  </li></ul><ul><ul><li>...
Programming Example: Classify Numbers (Solution) <ul><li>for  (counter = 1; counter <= N; counter++) </li></ul><ul><li>{ <...
The  do…while  Loop (Repetition) Structure <ul><li>Syntax: </li></ul><ul><li>Statements executed first, then  logical   ex...
do…while  Loop (Post-test Loop) Java Programming: From Problem Analysis to Program Design, 3e
break  Statements <ul><li>Used to  exit early from a loop   </li></ul><ul><li>Used to  skip remainder of  switch  structur...
continue  Statements <ul><li>Used in  while ,  for , and  do ... while  structures </li></ul><ul><li>When executed in a lo...
Nested Control Structures <ul><li>Provides new power, subtlety, and complexity </li></ul><ul><li>if ,  if … else , and  sw...
Nested Control Structures (Example) <ul><li>for  ( int  i = 1; i <= 5; i++) </li></ul><ul><li>{ </li></ul><ul><li>for  ( i...
Chapter Summary <ul><li>Looping Mechanisms </li></ul><ul><ul><li>Counter-controlled  while  loop </li></ul></ul><ul><ul><l...
Upcoming SlideShare
Loading in...5
×

Chap05

836

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
836
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Chap05"

  1. 1. Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition
  2. 2. Chapter Objectives <ul><li>Learn about repetition (looping) control structures </li></ul><ul><li>Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and EOF-controlled repetition structures </li></ul><ul><li>Examine break and continue statements </li></ul><ul><li>Discover how to form and use nested control structures </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  3. 3. Why Is Repetition Needed? <ul><li>There are many situations in which the same statements need to be executed several times </li></ul><ul><li>Example </li></ul><ul><ul><li>Formulas used to find average grades for students in a class </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  4. 4. The while Looping (Repetition) Structure <ul><li>Syntax </li></ul><ul><li>while (expression) </li></ul><ul><li> statement </li></ul><ul><li>Expression is always true in an infinite loop </li></ul><ul><li>Statements must change value of expression to false </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  5. 5. The while Looping (Repetition) Structure (continued) Java Programming: From Problem Analysis to Program Design, 3e
  6. 6. The while Looping (Repetition) Structure (continued) <ul><li>Example 5-1 </li></ul><ul><li>i = 0; //Line 1 </li></ul><ul><li>while (i <= 20) //Line 2 </li></ul><ul><li>{ </li></ul><ul><li>System.out.print(i + &quot; &quot;); //Line 3 </li></ul><ul><li>i = i + 5; //Line 4 </li></ul><ul><li>} </li></ul><ul><li>System.out.println(); //Line 5 </li></ul><ul><li>Output: </li></ul><ul><li>0 5 10 15 20 </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  7. 7. The while Looping (Repetition) Structure (continued) <ul><li>Typically, while loops are written in the following form: </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  8. 8. Counter-Controlled while Loop <ul><li>Used when exact number of data or entry pieces is known </li></ul><ul><li>General form: </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  9. 9. Sentinel-Controlled while Loop <ul><li>Used when exact number of entry pieces is unknown but last entry (special/sentinel value) is known </li></ul><ul><li>General form: </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  10. 10. Flag-Controlled while Loop <ul><li>Boolean value used to control loop </li></ul><ul><li>General form: </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  11. 11. EOF(End of File)-Controlled while Loop <ul><li>Used when input is from files </li></ul><ul><li>Sentinel value is not always appropriate </li></ul><ul><li>In an EOF-controlled while loop that uses the Scanner object console to input data, console acts at the loop control variable </li></ul><ul><li>The method hasNext , of the class Scanner , returns true if there is an input in the input stream; otherwise it returns false </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  12. 12. EOF(End of File)-Controlled while Loop (continued) <ul><li>The expression console.hasNext() evaluates to true if there is an input in the input stream; otherwise it returns false </li></ul><ul><li>Expressions such as console.nextInt() act as the loop condition </li></ul><ul><li>A general form of the EOF-controlled while loop that uses the Scanner object console to input data is of the form: </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  13. 13. EOF(End of File)-Controlled while Loop (continued) <ul><li>Suppose that inFile is a Scanner object initialized to the input file; in this case, the EOF-controlled while loop takes the following form: </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  14. 14. Programming Example: Checking Account Balance <ul><li>Input file: customer’s account number, account balance at beginning of month, transaction type (withdrawal, deposit, interest), transaction amount </li></ul><ul><li>Output: account number, beginning balance, ending balance, total interest paid, total amount deposited, number of deposits, total amount withdrawn, number of withdrawals </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  15. 15. Programming Example: Checking Account Balance (continued) <ul><li>Solution </li></ul><ul><ul><li>Read data </li></ul></ul><ul><ul><li>EOF-controlled loop </li></ul></ul><ul><ul><li>switch structure of transaction types </li></ul></ul><ul><ul><li>Determine action (add to balance or subtract from balance depending on transaction type) </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  16. 16. Programming Example: Fibonacci Number <ul><li>Fibonacci formula for any Fibonacci sequence: </li></ul><ul><li>a n = a n -1 + a n -2 </li></ul><ul><li>Input: first two Fibonacci numbers in sequence, position in sequence of desired Fibonacci number (n) </li></ul><ul><ul><li>int previous1 = Fibonacci number 1 </li></ul></ul><ul><ul><li>int previous2 = Fibonacci number 2 </li></ul></ul><ul><ul><li>int nthFibonacci = position of nth Fibonacci number </li></ul></ul><ul><li>Output: nth Fibonacci number </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  17. 17. Programming Example: Fibonacci Number (Solution) <ul><li>if (nthFibonacci == 1) </li></ul><ul><li>current = previous1; </li></ul><ul><li>else if (nthFibonacci == 2) </li></ul><ul><li>current = previous2; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>counter = 3; </li></ul><ul><li>while (counter <= nthFibonacci) </li></ul><ul><li>{ </li></ul><ul><li>current = previous2 + previous1; </li></ul><ul><li>previous1 = previous2; </li></ul><ul><li>previous2 = current; </li></ul><ul><li>counter++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Final result found in last value of current </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  18. 18. The for Looping (Repetition) Structure <ul><li>Specialized form of while loop </li></ul><ul><li>Simplifies the writing of count-controlled loops </li></ul><ul><li>Syntax: </li></ul><ul><li>for (initial expression; logical expression; </li></ul><ul><li>update expression) </li></ul><ul><li>statement </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  19. 19. The for Looping (Repetition) Structure (continued) <ul><li>Execution </li></ul><ul><ul><li>Initial statement executes </li></ul></ul><ul><ul><li>The loop condition evaluated </li></ul></ul><ul><ul><li>If loop condition evaluates to true, execute for loop statement and execute update statement </li></ul></ul><ul><ul><li>Repeat until loop condition is false </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  20. 20. The for Looping (Repetition) Structure (continued) Java Programming: From Problem Analysis to Program Design, 3e
  21. 21. The for Looping (Repetition) Structure (continued) <ul><li>Example 5-8 </li></ul><ul><li>The following for loop prints the first 10 nonnegative integers: </li></ul><ul><li>for (i = 0; i < 10; i++) </li></ul><ul><li>System.out.print(i + &quot; &quot;); </li></ul><ul><li>System.out.println(); </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  22. 22. The for Looping (Repetition) Structure (continued) <ul><li>Example 5-9 </li></ul><ul><li>The following for loop outputs the word Hello and a star (on separate lines) five times: </li></ul><ul><li>for (i = 1; i <= 5; i++) </li></ul><ul><li>{ </li></ul><ul><li> System.out.println(&quot;Hello&quot;); </li></ul><ul><li> System.out.println(&quot;*&quot;); </li></ul><ul><li>} </li></ul><ul><li>2. </li></ul><ul><li> for (i = 1; i <= 5; i++) </li></ul><ul><li>System.out.println(&quot;Hello&quot;); </li></ul><ul><li>System.out.println(&quot;*&quot;); </li></ul><ul><li>This loop outputs the word Hello five times and the star only once </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  23. 23. The for Looping (Repetition) Structure (continued) <ul><li>Does not execute if initial condition is false </li></ul><ul><li>Update expression changes value of loop control variable, eventually making it false </li></ul><ul><li>If logical expression is always true , result is an infinite loop </li></ul><ul><li>Infinite loop can be specified by omitting all three control statements </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  24. 24. The for Looping (Repetition) Structure (continued) <ul><li>If logical expression is omitted, it is assumed to be true </li></ul><ul><li>for statement ending in semicolon is empty </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  25. 25. Programming Example: Classify Numbers <ul><li>Input: N integers (positive, negative, and zeros) </li></ul><ul><ul><li>int N = 20; //N easily modified </li></ul></ul><ul><li>Output: number of 0 s, number of even integers, number of odd integers </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  26. 26. Programming Example: Classify Numbers (Solution) <ul><li>for (counter = 1; counter <= N; counter++) </li></ul><ul><li>{ </li></ul><ul><li>number = console.nextInt(); </li></ul><ul><li>System.out.print(number + &quot; &quot;); </li></ul><ul><li>switch (number % 2) </li></ul><ul><li>{ </li></ul><ul><li>case 0: </li></ul><ul><li>evens++; </li></ul><ul><li>if (number == 0) </li></ul><ul><li>zeros++; </li></ul><ul><li>break ; </li></ul><ul><li>case 1: </li></ul><ul><li>case -1: </li></ul><ul><li>odds++; </li></ul><ul><li>} //end switch </li></ul><ul><li>} //end for loop </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  27. 27. The do…while Loop (Repetition) Structure <ul><li>Syntax: </li></ul><ul><li>Statements executed first, then logical expression evaluated </li></ul><ul><li>Statement(s) executed at least once then continued if logical expression is true </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  28. 28. do…while Loop (Post-test Loop) Java Programming: From Problem Analysis to Program Design, 3e
  29. 29. break Statements <ul><li>Used to exit early from a loop </li></ul><ul><li>Used to skip remainder of switch structure </li></ul><ul><li>Can be placed within if statement of a loop </li></ul><ul><ul><li>If condition is met, loop exited immediately </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  30. 30. continue Statements <ul><li>Used in while , for , and do ... while structures </li></ul><ul><li>When executed in a loop, the remaining statements in the loop are skipped; proceeds with the next iteration of the loop </li></ul><ul><li>When executed in a while / do … while structure, expression evaluated immediately after continue statement </li></ul><ul><li>In a for structure, the update expression is executed after the continue statement; then the loop condition executes </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  31. 31. Nested Control Structures <ul><li>Provides new power, subtlety, and complexity </li></ul><ul><li>if , if … else , and switch structures can be placed within while loops </li></ul><ul><li>for loops can be found within other for loops </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  32. 32. Nested Control Structures (Example) <ul><li>for ( int i = 1; i <= 5; i++) </li></ul><ul><li>{ </li></ul><ul><li>for ( int j = 1; j <= i; j++) </li></ul><ul><li> System.out.print( &quot; * &quot; ); </li></ul><ul><li>System.out.println(); </li></ul><ul><li>} </li></ul><ul><li>Output : </li></ul><ul><li>* </li></ul><ul><li>** </li></ul><ul><li>*** </li></ul><ul><li>**** </li></ul><ul><li>***** </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  33. 33. Chapter Summary <ul><li>Looping Mechanisms </li></ul><ul><ul><li>Counter-controlled while loop </li></ul></ul><ul><ul><li>Sentinel-controlled while loop </li></ul></ul><ul><ul><li>Flag-controlled while loop </li></ul></ul><ul><ul><li>EOF-controlled while loop </li></ul></ul><ul><ul><li>for loop </li></ul></ul><ul><ul><li>do … while loop </li></ul></ul><ul><li>break statements </li></ul><ul><li>continue statements </li></ul><ul><li>Nested control structures </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×