Java offers three types of basic loops: for, while, and do..while.
(1) For loops:
for ( initialization; exit_limit_test;
increment/decrement )
{
statement;
statement;
}
For loops are used when you want to repeat one or more statements a specific number of times. The statements to be repeated are placed inside braces { }.
Inside parentheses, after the keyword for you place three Java statements, separated by semicolons:
First: a
statement that declares an initializes an integer variable. This variable
will control the loop.
int i = 1; or
int x = 0; or int counter =
10;
Second: a
conditional statement that tests the value in the counting variable to see if
it has reached a limit. When the value in the variable reaches the set
limit, the loop ends. That is, the loop continues as long as the
conditional statement is true, and ends when it is false
i <= 1; or
x < 7; or counter > 0;
Third: a
statement that changes the value in the counting variable by incrementing or
decrementing it. This statement will be executed for each iteration –
each pass through the loop.
i = i +
1; or i = i - 3; or i++;
For example, the code below prints out the numbers from 1 to 10
for ( int i = 1; i <= 10; i++ )
{
System.out.println ( i );
}
Exercise 11:
Write an application in Java named Exercise11 that prints out the numbers from 10 to 1.
For the solution, click here.
(2) While loops
while ( condition ) {
statement;
statement;
}
While loops begin with the keyword while, followed by parentheses that contain a condition.
The statements inside the braces { } will be executed as long as the condition inside the loop remains true. Therefore, one of the statements inside the braces must be able to change the condition, or less the loop will continue forever.
Notice that there is no semicolon after the while line in the regular while loop.
An example of a while loop that prints the numbers from 1 to 10:
int i=1;
while ( i < 10 ) {
System.out.println (i);
i++;
}
Exercise 12:
Write an application in Java named Exercise12 that prints out the numbers from 10 to 1 using a while loop.
For the solution, click here.
(3) Do ... while loops
do {
statement;
statement;
} while ( condition ) ;
Do while loops are just like while loops except that the while condition is placed after the loop instead of in front of it, and followed by a semicolon. The keyword do must be placed in front of the { } that enclose the loop.
int i=1;
do
{
System.out.println(i);
i++;
} while ( i < 10 ) ;
Because the while condition is at the end of the body of the loop, it will
not be checked when the program runs until the loop has executed the first
time. That means that if we had
initialized i as 100
instead of 1 in the previous example, the program would have printed the 100,
and then incremented it before it checked and saw that i was greater than
10. However, in the previous while
loop example, if we had initialized i to 100, the loop would not have
executed even once, and nothing would have printed, because the while condition
would have been checked before the loop was entered.
Exercise13
Write an application in Java named Exercise13 that prints out the numbers from 10 to 1 using a do .. while loop..
For the solution, click here.
Done!
You have reached the end of this brief introduction to Java syntax. The course "Introduction to Java for MainFramers" will review all of this syntax, then continue with additional statements and an introduction to writing and using classes and objects, and to the techniques of object-oriented programming.
More Practice
More practice exercises over operations, conditionals, and loops are available here.
You should do some of these extra exercises to maintain your Java syntax knowledge if it will be more than a day or two before you take the class, or just if you’d like a little more practice to solidify your Java syntax skills.
See you in class!