Figure 5.28 Program Segment NoNo #1 Program Segment YesYes #1 int x = 0; int x = 0; < 10) while(x < 10) System.out.println(x); { x++; System.out.println(x); } The loop condition variable, x, The loop condition variable, x, changes. changes. The loop will not exit. The loop exits when x reaches 10. Figure 5.29 Program Segment NoNo #2 Program Segment YesYes #2 int x; int x = 0; while(x < 10) < 10) { { x++; x++; System.out.println(x); System.out.println(x); } } The loop condition variable, x, The loop condition variable, x, is never initialized. is initialized. The program will This program will not compile. compile and execute correctly. Fixed Repetition vs. Conditional Repetition Fixed Repetition describes a situation where you know ahead – of time how many times you want the loop to repeat. – An example would be drawing exactly 100 circles on the screen. The command for fixed repetition is for.
Figure 5.28 Program Segment NoNo #1 Program Segment YesYes #1 int x = 0; < 10) System.out.println(x); The loop condition variable, x, changes. The loop will not exit. int x = 0; while(x < 10) { x++; System.out.println(x); } The loop condition variable, x, changes. The loop exits when x reaches 10. Figure 5.29 Program Segment NoNo #2 Program Segment YesYes #2 int x; while(x < 10) { x++; System.out.println(x); } The loop condition variable, x, is never initialized. This program will not compile. int x = 0; < 10) { x++; System.out.println(x); } The loop condition variable, x, is initialized. The program will compile and execute correctly. Fixed Repetition vs. Conditional Repetition Fixed Repetition describes a situation where you know – ahead of time – how many times you want the loop to repeat. An example would be drawing exactly 100 circles on the screen. The command for fixed repetition is for.
Text05-ControlStructures.docx