To write the numbers 0 through 9, you could do it like this:
System.out.println("0"); System.out.println("1"); System.out.println("2"); System.out.println("3"); System.out.println("4"); System.out.println("5"); System.out.println("6"); System.out.println("7"); System.out.println("8"); System.out.println("9");or
System.out.println("0\n1\n2\n3\n4\n5\n6\n7\n8\n9");�
or you could use a loop and do it like this
int i = 0 ; while( i < 10 ) { System.out.println( i ); i++ ; }This loop is called a "while loop". This is the simplest loop and the one that I use the most often. Its structure looks like this:
while( boolean-expression ) { stuff }As long as boolean-expression is true, stuff will be executed.
In the "0 through 9" example above, as long as "i" is less than 10, "i" will be printed and then "i" will be incremented.
The first time a "while loop" is encountered, boolean-expression is evaluated to be true or false. If it is false, the whole statement is skipped. If it is true, stuff is executed and then boolean-expression is evaluated again.
Let's walk through a short loop:
line 1: int i = 0 ; line 2: while( i < 2 ) line 3: { line 4: System.out.println( i ); line 5: i++ ; line 6: } line 7: System.out.println("the end!");
On line 2, "i" is compared to 2. Since "i" is currently zero and zero is less than 2, the statement is true. So we're gonna do the stuff in the braces!
On line 4, print "i": "0" appears on the screen.
On line 5, "i" is incremented. It now contains 1 (one).
On line 6, we're all done doing stuff in the braces, so we go back to the top of the "while loop."
On line 2, "i" is compared to 2. Since "i" is currently 1 and 1 is less than 2, the statement is true. So we're gonna do the stuff in the braces!
On line 4, print "i": "1" appears on the screen.
On line 5, "i" is incremented. It now contains 2.
On line 6, we're all done doing stuff in the braces, so we go back to the top of the "while loop."
On line 2, "i" is compared to 2. Since "i" is currently 2 and 2 is not less than 2, the statement is false. So we're gonna skip the entire "while loop."
On line 7, print "the end!": "the end!" appears on the screen.
boolean done = false ; while ( ! done ) { if ( userPressedEsc() ) { done = true ; } else { System.out.println("not done yet"); } }Note that "!" means "not" in Java. So the controlling statment is "while not done".
The "while loop" can be used for all of your looping needs. However, it is a standard that if you know in advance how many times you need to go through your loop, you use a "for loop". It is considered a shorthand version of a "while loop", but I think it is pretty messy. Nonetheless, you must know how to use the "for loop"!
Take a look at a simple "while loop":
int i = 0 ; while( i < 10 ) { System.out.println( i ); i++ ; }The "for loop" shorthand for this is:
for( int i = 0 ; i < 10 ; i++ ) { System.out.println( i ); }The "for loop" has three controlling parts:
for( initialize ; again? ; iterator ) { // stuff goes here }which the compiler converts to:
{ initialize ; while( again? ) { // stuff goes here iterator ; } }When I'm writing code and I know I need a loop, I ask myself which message I want to convey:
There is one other looping construct available in Java. The "do...while loop". I never use it. It does provide a tiny convenience. Some would argue that it provides a more elegant solution once in a great while. This is fine for people that never share their code. When you have to try to explain to somebody what it is and why you use it, didn't you just negate the benefit?
For more information on loops, read page 177 through 179 in Just Java 1.2., or page 73 through 77 in Just Java 2 (sixth edition). Browse the stuff on "continue" and "break". You should know about them, but never use them yourself (IMO).
And for a change of pace, you can visit my permaculture site.