Download presentation
Presentation is loading. Please wait.
Published byBob Ewell Modified over 6 years ago
1
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13
2
written in Java Programs (general solution) transform the initial input values (specific problem) to the final output values (specific solution) Solutions are expressed as – Sequence of statements – Branching – Repetition Design before code – Flowcharts – Activity Diagrams – Pseudo Code thus far in CS140 … 2 − Variables − Classes − Objects and references
3
Chapter 4 Java Loop Statements (4.1) The while statement The do-while statement Nested loops The for statement Programming with loops (4.2) Graphics Supplement (4.3) 3
4
Conditional Loops: while and do-while SYNTAX: while (condition) loop body SYNTAX: do loop body while (condition) ; 4 SEMANTICS: repeat the loop body while the condition is true Loop Body can be a single statement, or a {statement block} Condition can be a logical expression or a boolean value (variable or constant)
5
Repetition: while and do-while SYNTAX: while (condition) loop body SYNTAX: do loop body while (condition) ; 5 loop body true false condition loop body true false condition
6
What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ System.out.print(count + “, ”); count++; } 1.(none) 2.0, 1, 2, 3, 3.1, 2, 3, 4.0, 1, 2, 5.(don’t know) 6
7
public static final int MAX = 3; int count = 0; while (count > MAX){ count++; System.out.print(count + “, ”); } What is the output? 1.(none) 2.0, 1, 2, 3, 3.1, 2, 3, 4.0, 1, 2, 5.idk 7
8
What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ count++; System.out.print(count + “, ”); } 1.0, 1, 2, 3, 2.1, 2, 3, 3.0, 1, 2, 4.1, 2, 3, 4 5.idk 8
9
What is the output? public static final int MAX = 3; int count = 0; while (count < MAX) count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.idk 9
10
What is the output? public static final int MAX = 3; int count = 0; while (count > MAX) count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.idk 10
11
What is the output? public static final int MAX = 3; int count = 0; while (count < MAX); count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.(none) 6.idk 11
12
What is the output? public static final int MAX = 3; int count = 0; while (count > MAX); count++; System.out.print(count + “, ”); 1.0, 2.1, 2, 3, 3.1, 4.3, 5.(none) 6.idk 12
13
What is the output? public static final int MAX = 3; int count = 0; while (count < MAX) System.out.print(count + “, ”); 1.(none) 2.0, 1, 2, 3, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 13
14
What is the output? public static final int MAX = 3; int count = 0; do { count++; System.out.print(count + “, ”); } while (count < MAX); 1.0, 1, 2, 3, 2.1, 2, 3, 3.0, 1, 2, 4.1, 2, 3, 4 5.idk 14
15
What is the output? public static final int MAX = 3; int count = 0; do{ System.out.print(count + “, ”); count += 3; } while (count < MAX); 1.(none) 2.0, 3.0, 3, 4.0, 0, 0, 0, 0, … 5.idk 15
16
What is the output? public static final int MAX = 3; int count = 0; do System.out.print(count + “, ”); while (count > MAX); 1.(none) 2.0, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 16
17
What is the output? public static final int MAX = 3; int count = 0; do System.out.print(count + “, ”); while (count < MAX); 1.(none) 2.0, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 17
18
SYNTAX: while (condition) loop body SYNTAX: do loop body while (condition) ; 18 Loop body true false condition Loop body true false condition 1.for loop 2.while loop 3.do-while loop 4.idk
19
Repetition: for loop SYNTAX: for (initialization; condition; update) loop body 19 SEMANTICS: Execute the initialization (once) repeat the loop body while the condition is true Execute the update statement after each loop
20
Repetition Conditionally –while –do-while “pre-determined number of times” can know in advance how many times something should be repeated –for loop 20 Q: So why all the examples of while and do-while where there is counting? A: Easiest way to illustrate how those loops work. Q: So why all the examples of while and do-while where there is counting? A: Easiest way to illustrate how those loops work.
21
Repetition: for loop SYNTAX: for (initialization; condition; update) loop body 21 Loop body true false condition initialization update
22
Repetition: for loop int max = 6; int i; for ( i = 0; i < max; i++ ) { System.out.print( i ); } 22 int max = 6; int i = 0; while ( i < max ) { System.out.print( i ); i++; }
23
Repetition: for loop int max = 6; int i; for ( i = 0; i < max; i++ ) { System.out.print( i ); } System.out.print( “last i ” + i ); 23 int max = 6; for ( int i = 0; i < max; i++ ) { System.out.print( i ); } System.out.print( “last i ” + i );
24
What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count++) System.out.print(count + “, ”); 1.(none) 2.0, 1, 2, 3.1, 2, 3, 4.0, 0, 0, 0, 0, … 5.idk 24 Print count true false count < MAX count = 0 count++
25
What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count++); System.out.print(count + “, ”); 1.(none) 2.0, 1, 2, 3.1, 2, 3, 4.3 5.idk 25
26
What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count++) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 1, 1 2, 2 3, 3 3. 0, 1, 2, 34. 1, 2, 3, 4 5. 0, 0, 0, 0, 0, … 6. idk 26
27
What is the output? public static final int MAX = 3; int count; for(count = 0; count < MAX; count+=2) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 2, 4 3. 0, 1, 2, 34. 0, 2, 4 5. 0, 2 6. idk 27
28
What is the output? public static final int MAX = 3; int count; for(count = MAX; count > 0; count--) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 3, 2, 1, 3. 3, 2, 1, 0,4. 2, 1, 0, 5. 3, 3, 3, 3, 3, … 6. idk 28
29
What is the output? public static final int MAX = 3; int count; for(count = MAX; count < 0; count--) System.out.print(count + “, ”); System.out.print(count); 1. (none)2. 3, 2, 1, 3. 3, 2, 1, 0,4. 3 5. 0 6. idk 29
30
Nested loops Loops can be nested just like branching expressions int a = 3; int b = 2; for (int i = 0; i < b; i++) { for (int j = 0; j < a; j++) { System.out.print( j + i ); } System.out.println( i ); } 30
31
What is the output? public static final int MAX = 3; for (int f1 = 0; f1 < MAX; f1++){ for (int f2 = 0; f2 < MAX; f2++){ System.out.print(f1 + “ + ” + f2); System.out.println(“ = ” + (f1+f2)); } 31 not multi-choice
32
What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ for (int f2 = 0; f2 < MAX; f2++){ System.out.print(count + “ ”); } count++; } 32 not multi-choice
33
What is the output? public static final int MAX = 3; int count = 0; while (count < MAX){ for (int f2 = 0; f2 < MAX; f2++){ System.out.print(f2 + “ ”); } count++; } 33 not multi-choice
34
Which Loop? Do you know (for any input) how many times the loop body will be executed? Should the loop body execute one or more times? 34
35
Which Loop? Write a program to read a list of integers and to display the largest, smallest, and average of all integers. The user indicates the end of the input list by entering 0 (zero). 35 1.while() 2.do-while() 3.for() 4.idk
36
Which Loop? Write a program to read a list of 20 integers and to display the largest, smallest, and average of all integers. 36 1.while() 2.do-while() 3.for() 4.idk
37
Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account in ten years. 37 1.while() 2.do-while() 3.for() 4.idk
38
Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account after a number of years entered by the user. 38 1.while() 2.do-while() 3.for() 4.idk
39
Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account in ten years. The interest is added each month of each of the ten years. 39 1.while() 2.do-while() 3.for() 4.idk
40
Which Loop? Write a program that reads a bank account balance and an interest rate and displays the balance of the account in ten years. The interest is added each day of each month of each of the ten years. 40 1.while() 2.do-while() 3.for() 4.idk
41
Which Loop? Write a program that simulates the falling and bouncing of a ball by computing its height at each 1/10 of a second (of a simulated clock). The user enters an initial height (above the ground) and the program continues until the fifth bounce. 41 1.while() 2.do-while() 3.for() 4.idk
42
break; and continue; break; - ends only the innermost loop or switch continue; - ends current iteration and proceeds to the next one Only use these if you have a good reason Try to rewrite your code to avoid these 42
43
Review Three loop statements – Two are top-tested: execute loop body 0+ – One is bottom tested: execute loop body 1+ Loop body can be – a single statement or {statement block} – Branching – Loops Condition can be boolean or logical expression Choose for when number of loops is ‘known’ 43
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.