Java & J2EE Page 4 - Java Statements |
The For Loop allows you to condense the code used in While loops. If we wanted to rewrite our NASA launch code with a For loop, it would look something like this (hit it!): (Note: the writer of this article has listened to the Beastie Boys one too many times) class LaunchCode { public static void main(String args[]) { int timer; for(timer = 10; timer > 0; timer—) System.out.println(“T minus “ + timer); } } This would result in the same countdown as our previous NASA program. As a side note, you could technically initialize the timer variable inside the For loop. If you had done so, you would not be able to call it outside of the loop. Placing the variable outside the loop allows the variable to be called again later on. Jump Statements There are three types of Jump statements in Java: the break, the continue, and the return. We have already used the break statement in some of our previous code. It's function is pretty simple: it terminates a statement, exits a loop, or can be used as a goto statement. Since we have already used it several times, I won't discuss it any further.
blog comments powered by Disqus |
|
|
|
|
|
|
|