Introduction to Computing Using Java Repetition for loop









![Use of for Loop counter 1 <= 3? [true] int counter; for (counter = Use of for Loop counter 1 <= 3? [true] int counter; for (counter =](https://slidetodoc.com/presentation_image_h2/16c0a0870dbf51053ed2eed79bd1e93a/image-10.jpg)


![Use of for Loop counter 2 <= 3? [true] int counter; for (counter = Use of for Loop counter 2 <= 3? [true] int counter; for (counter =](https://slidetodoc.com/presentation_image_h2/16c0a0870dbf51053ed2eed79bd1e93a/image-13.jpg)


![Use of for Loop counter 3 <= 3? [true] int counter; for (counter = Use of for Loop counter 3 <= 3? [true] int counter; for (counter =](https://slidetodoc.com/presentation_image_h2/16c0a0870dbf51053ed2eed79bd1e93a/image-16.jpg)


![Use of for Loop counter 4 <= 3? [false] int counter; for (counter = Use of for Loop counter 4 <= 3? [false] int counter; for (counter =](https://slidetodoc.com/presentation_image_h2/16c0a0870dbf51053ed2eed79bd1e93a/image-19.jpg)














- Slides: 33
Introduction to Computing Using Java Repetition: for loop 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 1
Let’s Sing a Song 一齊唱歌仔 有隻雀仔跌落水 London Bridge is Falling Down 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 2
Learn from the Song ||: … : || (n) Repetition Repeat n times (男/女/left/right) Selection 先中文,後英文 Sequential 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 3
Flow of Control ¬Sequential – Instructions are executed one-by-one. ¬Branching/Selection – There are multiple paths going to the same destination. ¬Looping/Repetition – The instructions are repeated. ¬In real use, they can be inter-mixed!!! 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 4
Looping/Counting ¬ 「捉伊人」數 50下 – 1, 2, 3, 4, 5, 16, 27, 38, 49, 50! ¬ 「晚上/上堂數羊仔」 ¬ When a computer counts, we need a loop. ¬ In terms of executing hundreds of millions of instructions per second, a computer could potentially finish its tasks (instructions) in no time… if our programs are written only sequentially. 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 5
Deterministic Loop ¬Loop in computer programs means repetition. ¬If we know how much times a loop repeats, it is called a deterministic loop. ¬Like counting, we may give a loop a starting value and an ending condition. ¬There is also a loop counter. 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 6
Use of for Loop counter 0 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); A variable as loop counter, its value varies in the loop. 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 7
Use of for Loop counter 0 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); for (start; check; update) { body } 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 8
Use of for Loop counter 1 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); Starting the loop by setting the loop counter to a starting value. 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 9
Use of for Loop counter 1 <= 3? [true] int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); The ending condition is checked during each repetition. 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 10
Use of for Loop counter 1 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2008 -2009 3 b Execute the body during each repetition. Michael Fung, CS&E, The Chinese University of HK 11
Use of for Loop counter 2 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2008 -2009 3 b Update the loop counter according to our update rule. Michael Fung, CS&E, The Chinese University of HK 12
Use of for Loop counter 2 <= 3? [true] int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2008 -2009 3 b Check the ending condition again. Michael Fung, CS&E, The Chinese University of HK 13
Use of for Loop counter 2 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2008 -2009 3 b Execute the body again. Michael Fung, CS&E, The Chinese University of HK 14
Use of for Loop counter 3 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2008 -2009 3 b Update the loop counter according to our update rule again. Michael Fung, CS&E, The Chinese University of HK 15
Use of for Loop counter 3 <= 3? [true] int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2008 -2009 3 b Check the ending condition again. Michael Fung, CS&E, The Chinese University of HK 16
Use of for Loop counter 3 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2 ***** 3 ***** 2008 -2009 3 b Execute the body again. Michael Fung, CS&E, The Chinese University of HK 17
Use of for Loop counter 4 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2 ***** 3 ***** 2008 -2009 3 b Update the loop counter according to our update rule again. Michael Fung, CS&E, The Chinese University of HK 18
Use of for Loop counter 4 <= 3? [false] int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); 1 ***** 2 ***** 3 ***** 2008 -2009 3 b Check the ending condition again. Condition failed this time at last. Michael Fung, CS&E, The Chinese University of HK 19
Use of for Loop counter 4 int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter + “ *****”); . . . 1 ***** 2 ***** 3 ***** 2008 -2009 3 b Continue execute the instructions following the loop. Michael Fung, CS&E, The Chinese University of HK 20
Dissecting the for Loop for (i = 0; i < n; i++) { do. Something(may. Be. Something. With_i); } 1. 2. 3. 4. 5. Execute the starting part. Check the ending condition. Execute the loop body. Perform update. Goto 2. for (start; check; update) { body_statement(s); } following_statement; Rhythm: 2008 -2009 3 b s c b u c following… Michael Fung, CS&E, The Chinese University of HK 21
How to Write IT Correctly? ¬ What’s the difference between: Ø for (counter = 1; counter <= 3; counter++). . . Ø for (counter = 0; counter < 3; counter++). . . Ø for (counter = 3; counter >= 0; counter--). . . ¬ How many times to repeat? ¬ Starting and Ending values of counter? ¬ Tree-planting question! 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 22
7 -Minute Soft Break 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 23
More Examples for (number = 0; number < 10; number++) System. out. print((number * 2) + “ ”); Ø 0 2 4 6 8 10 12 14 16 18 for (number = 1; number < 20; number += 2) System. out. print(number + “ ”); Ø 1 3 5 7 9 11 13 15 17 19 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 24
Properties of for Loop /* degenerate case ? ! */ for (; ; ) ; ¬ The start portion is executed one-and-only-once. ¬ If there are more than one statements in the loopbody, a pair of curly braces is needed. ¬ The start-check-update complex usually concerns one single loop counter variable. ¬ Either or both of the elements start, check, update and even the body could be absent. – Nevertheless, the punctuations must be there. 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 25
Properties of for Loop ¬ The counter value can be used in the loop. ¬ The counter value is retained after the loop. int counter; for (counter = 1; counter <= 3; counter++) System. out. println(counter * 10 + “ *****”); System. out. println(“counter = ” + counter); --- 8< --------------------10 ***** Rhythm: s c b u c print… 20 ***** 30 ***** counter = 4 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 26
Properties of for Loop ¬Ending condition checking is performed before executing the loop body. int counter; for (counter = -5; counter > 7; counter++) System. out. println(counter + “ *****”); System. out. println(“counter = ” + counter); --- 8< --------------------counter = -5 Rhythm: 2008 -2009 3 b s c b next… u c b u c next… Michael Fung, CS&E, The Chinese University of HK 27
Nested for Loops int counter 1, counter 2; for (counter 1 = 1; counter 1 <= 2; counter 1++) for (counter 2 = 1; counter 2 <= 3; counter 2++) System. out. println(counter 1 + “, ” + counter 2); following_statement. . . --- 8< --------------------1, 1 1, 2 1, 3 2, 1 2, 2 2, 3 Rhythm: s 1 c 1 b 1[s 2 c 2 b 2 u 2 c 2 b 2 u 2 c 2] u 1 c 1 following_statement… 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 28
Nested for Loops int counter 1, counter 2; for (counter 1 = 1; counter 1 <= 2; counter 1++) for (counter 2 = 1; counter 2 <= 3; counter 2++) System. out. println(“*”); following_statement. . . --- 8< --------------------* * * Rhythm: s 1 c 1 b 1[s 2 c 2 b 2 u 2 c 2 b 2 u 2 c 2] u 1 c 1 following_statement… 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 29
Nested for Loops int counter 1, counter 2; for (counter 1 = 1; counter 1 <= 2; counter 1++) for (counter 2 = 1; counter 2 <= 3; counter 2++) System. out. print(“*”); following_statement. . . --- 8< --------------------****** Rhythm: s 1 c 1 b 1[s 2 c 2 b 2 u 2 c 2 b 2 u 2 c 2] u 1 c 1 following_statement… 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 30
Nested for Loops int counter 1, counter 2; for (counter 1 = 1; counter 1 <= 2; counter 1++) { for (counter 2 = 1; counter 2 <= 3; counter 2++) System. out. print(“*”); System. out. println(); } --- 8< --------------------- Rhythm: *** s 1 *** c 1 b 1[s 2 c 2 b 2 u 2 c 2 b 2 u 2 c 2 PL] u 1 c 1 following_statement… 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 31
Nested for Loops ¬ The counter value may be used in the loop body. int counter, star_count; for (counter = 1; counter <= 3; counter++) { System. out. print(counter + “ ”); for (star_count = 1; star_count <= counter; star_count++) System. out. print(‘*’); System. out. println(); } System. out. println(“counter = ” + counter); --- 8< --------------------1 * 2 ** 3 *** counter = 4 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 32
End Note ¬Readings and References – Section 5. 3, 5. 4, 5. 8, 5. 9 ¬Exercise – 5. 9, 5. 10, 5. 17, 5. 18 ¬Programming Projects – 5. 7, 5. 10 2008 -2009 3 b Michael Fung, CS&E, The Chinese University of HK 33