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.
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.
Revisiting Logical Operators Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 rights reserved.
Revisiting Logical Operators Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 4 rights reserved.
Revisiting Logical Operators Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 rights reserved.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Lecture 2 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 22 rights reserved.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Recommend
More recommend