Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java class 2010.10.22. Outline for loop while loop do while loop How to choose? Nested loop.

Similar presentations


Presentation on theme: "Java class 2010.10.22. Outline for loop while loop do while loop How to choose? Nested loop."— Presentation transcript:

1 Java class 2010.10.22

2 Outline for loop while loop do while loop How to choose? Nested loop

3 for loop EX: for ( i=0 ; i<=100 ; i++ ) // 迴圈條件運算式 { // 迴圈內的動作 ; }

4 迴圈內的動作 進入迴圈 true false 離開迴圈 迴圈條件運算式

5 while loop EX: int count=0; while ( count<100 ) { System.out.println(”Hello!”); count++; } p.s count 從 0 累加到 99

6 loop- continuation condition? Statement(s) (loop body) false true count=0 (count < 100) ? System.out.println(“Hello!”); count++;

7 練習 : 用 for loop 以及 while loop, 寫九九乘法表 格式 :

8 do while loop EX: int count=0; do { System.out.println(”Hello!”); count++; } while ( count<100 );

9 loop- continuation condition? Statement(s) (loop body) false true count=0 (count < 100) ? System.out.println(“Hello!”); count++;

10 Pretest & Posttest loop 先測迴圈 : while 、 for The condition is checked before the loop body is excuted. 後測迴圈 : do while The condition is checked after the loop body is excuted.

11 How to choose? A for loop may be used if the number of repetitions is known in advance. A while loop may be used if the number of repetitions is not fixed. A do while loop can be used to replace a while loop if the loop body has to be executed before the condition is tested. 課本 p.152

12 Nested( 巢狀 ) loop for { ……. for { ……. } 課本 p.153

13 回家看 : p.152 ~ p.153


Download ppt "Java class 2010.10.22. Outline for loop while loop do while loop How to choose? Nested loop."

Similar presentations


Ads by Google