Java Reference
In-Depth Information
continue;
}
System.out.println(j);
}while(j-- > 0);
}
}
The for loop in the ContinueDemo program has a continue statement that
executes each time the loop counter is divisible by 2. For example, the first time
through the loop, the value of i is 10. Because 10 percent of 2 is 0, the continue
occurs and flow of control jumps to the update statement, i- -. Therefore, the 10
is not displayed because the println() statement is skipped. Notice in the out-
put in Figure 3.7 that only the odd values less than 10 are displayed.
The while loop in the ContinueDemo program has a continue that occurs
each time the loop counter j is not divisible by 3. The first time through the
loop, j is 20 and the continue occurs. Flow of control jumps to the Boolean
expression, which decrements j. This process repeats, with the println() state-
ment getting skipped over each time the value of j is not divisible by 3. The
output of this while loop is the numbers less than 20 that are divisible by 3, as
you can see in Figure 3.7.
Excessive use of the break and continue statements is not going to
impress any of your programming colleagues. The main reason these
keywords appear in the Java language is that they are used in C and C++.
They do serve a purpose, especially the break keyword (as discussed
earlier); however, in many situations you can avoid using a break or
continue simply by redesigning your code.
Figure 3.7
Output of the ContinueDemo program.
Search WWH ::




Custom Search