chapter 4 loops and iteration
play

Chapter 4: Loops and Iteration CS1: Java Programming Colorado State - PowerPoint PPT Presentation

Chapter 4: Loops and Iteration CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Kris Brown Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights


  1. Chapter 4: Loops and Iteration CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Kris Brown Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights 1 reserved.

  2. Revisiting Logical Operators When searching you could say: - Looking for results for Monet and his use of color - Looking for movie results for Sci Fi or Crime - Looking for a van or SUV - not both Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 2 rights reserved.

  3. Revisiting Logical Operators Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 rights reserved.

  4. Revisiting Logical Operators Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 4 rights reserved.

  5. Revisiting Logical Operators Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 rights reserved.

  6. Motivations Suppose that you need to print a string (e.g., "Welcome to Java!") a hundred times. It would be tedious to have to write the following statement a hundred times: System.out.println("Welcome to Java!"); So, how do you solve this problem? Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 6 rights reserved.

  7. Opening Problem Problem: System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); 100 times … … … System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); System.out.println("Welcome to Java!"); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 7 rights reserved.

  8. Introducing while Loops int count = 0; while (count < 100) { System.out.println("Welcome to Java"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 8 rights reserved.

  9. while Loop Flow Chart int count = 0; while (loop-continuation-condition) { while (count < 100) { // loop-body; System.out.println("Welcome to Java!"); Statement(s); count++; } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 9 rights reserved.

  10. animation Trace while Loop Initialize count int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 10 rights reserved.

  11. animation Trace while Loop, cont. (count < 2) is true int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 11 rights reserved.

  12. animation Trace while Loop, cont. Print Welcome to Java int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 12 rights reserved.

  13. animation Trace while Loop, cont. Increase count by 1 int count = 0; count is 1 now while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 13 rights reserved.

  14. animation Trace while Loop, cont. (count < 2) is still true since count int count = 0; is 1 while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 14 rights reserved.

  15. animation Trace while Loop, cont. Print Welcome to Java int count = 0; while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 15 rights reserved.

  16. animation Trace while Loop, cont. Increase count by 1 int count = 0; count is 2 now while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 16 rights reserved.

  17. animation Trace while Loop, cont. (count < 2) is false since count is 2 int count = 0; now while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 17 rights reserved.

  18. animation Trace while Loop The loop exits. Execute the next int count = 0; statement after the loop. while (count < 2) { System.out.println("Welcome to Java!"); count++; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 18 rights reserved.

  19. Ending a Loop with a Sentinel Value Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a value is known as a sentinel value . Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 19 rights reserved.

  20. Caution Don’t use floating-point values for equality checking in a loop control. Since floating-point values are approximations for some values, using them could result in imprecise counter values and inaccurate results. Consider the following code for computing 1 + 0.9 + 0.8 + ... + 0.1: double item = 1; double sum = 0; while (item != 0) { // No guarantee item will be 0 sum += item; item -= 0.1; } System.out.println(sum); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 20 rights reserved.

  21. do-while Loop do { // Loop body; Statement(s); } while (loop-continuation-condition); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 21 rights reserved.

  22. Lecture 2 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 22 rights reserved.

  23. for Loops for (initial-action; int i; loop-continuation-condition; for (i = 0; i < 100; i++) { action-after-each-iteration) { System.out.println( // loop body; "Welcome to Java!"); Statement(s); } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 23 rights reserved.

  24. animation Trace for Loop Declare i int i; for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 24 rights reserved.

  25. animation Trace for Loop, cont. Execute initializer int i; i is now 0 for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 25 rights reserved.

  26. animation Trace for Loop, cont. (i < 2) is true int i; since i is 0 for (i = 0; i < 2; i++) { System.out.println( "Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 26 rights reserved.

  27. animation Trace for Loop, cont. Print Welcome to Java int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 27 rights reserved.

  28. animation Trace for Loop, cont. Execute adjustment statement int i; i now is 1 for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 28 rights reserved.

  29. animation Trace for Loop, cont. (i < 2) is still true int i; since i is 1 for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 29 rights reserved.

  30. animation Trace for Loop, cont. Print Welcome to Java int i; for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 30 rights reserved.

  31. animation Trace for Loop, cont. Execute adjustment statement int i; i now is 2 for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 31 rights reserved.

  32. animation Trace for Loop, cont. (i < 2) is false int i; since i is 2 for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 32 rights reserved.

  33. animation Trace for Loop, cont. Exit the loop. Execute the next int i; statement after the loop for (i = 0; i < 2; i++) { System.out.println("Welcome to Java!"); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 33 rights reserved.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend