YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Java class 2010.10.22. Outline for loop while loop do while loop How to choose? Nested loop.

Java class2010.10.22

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

Outline

• for loop

• while loop

• do while loop

• How to choose?

• Nested loop

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

for loop

EX:

for ( i=0 ; i<=100 ; i++ ) // 迴圈條件運算式{

// 迴圈內的動作 ;

}

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

迴圈內的動作迴圈內的動作

進入迴圈

true

false

離開迴圈

迴圈條件運算式

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

while loop EX:

int count=0;

while ( count<100 )

{

System.out.println(”Hello!”);

count++;

}

p.s count 從 0 累加到 99

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

loop-

continuation condition?

Statement(s)(loop body)

false

true

count=0

(count < 100) ?

System.out.println(“Hello!”);count++;

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

練習 :

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

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

do while loop

EX:

int count=0;

do {

System.out.println(”Hello!”);

count++;

} while ( count<100 );

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

loop-continuation condition?

Statement(s)(loop body)

false

true

count=0

(count < 100) ?

System.out.println(“Hello!”);count++;

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

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.

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

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

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

Nested( 巢狀 ) loop

for

{

…….

for

{

…….

}

}

課本 p.153

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

回家看 :

p.152 ~ p.153


Related Documents