Java Statements - To Be Continued... (
Page 5 of 5 )
No, it's not the end of the article just yet (grumble grumble). The Continue statement allows you to force an early iteration of the loop. Basically it runs a portion of the loop without executing the rest of the code in the body. Here is a sample:
class Counter {
public static void main(String args[]) {
for(int timer = 0; timer < 10; timer ++) {
System.out.print(timer + “ “);
if (i%5 == 0) continue;
System.out.println.(“”);
}
}
}
The above code will print five numbers on a line up to the number 10 like so:
0 1 2 3 4 5
6 7 8 9
The Return of the Jedi
I just wanted to throw that in there. The Return statement is used when you wish to return from a method (we will discuss methods later on). Here is how that looks in code:
class ChickenEgg {
public static void main(String args[]) {
boolean t = true;
System.out.println(“The chicken!”);
if(t) return;
System.out.println(“The egg!”);
}
}
The above code will print out: The Chicken! as it forces a return from the method prior to it, executing the second System.out.println command, thus solving the age old question: what came first, the chicken or the egg?
Incidentally, neither of those is right. The correct answer is a pre-evolved version of the chicken.
Well that's it for this tutorial. Be sure to join me for the next article, when I will help teach you to become a true Java ninja.
Till then...