View
239
Download
3
Embed Size (px)
Citation preview
while loop EX:
int count=0;
while ( count<100 )
{
System.out.println(”Hello!”);
count++;
}
p.s count 從 0 累加到 99
loop-
continuation condition?
Statement(s)(loop body)
false
true
count=0
(count < 100) ?
System.out.println(“Hello!”);count++;
loop-continuation condition?
Statement(s)(loop body)
false
true
count=0
(count < 100) ?
System.out.println(“Hello!”);count++;
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.
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