Loop Constructs in Java
Java Basics – Part 9
Forward: In Java, you have the do-while loop, the while loop and the for-loop. You will see what all these mean in this Tutorial.
By: Chrysanthus Date Published: 2 Sep 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
The do-while Loop
Try the following code first:
class DoWhile
{
public static void main(String[] args)
{
int n = 0;
do
{
System.out.println(n);
++n;
} while (n<5);
}
}
Let us look at what is in the block of the main method. Zero is assigned to a variable, n. Then you have the do-while loop construct. The first thing in the construct is, do. This is an instruction to Java to execute what is inside the braces (curly brackets).
Java executes statements in the block of the main method from top to bottom. The first statement to execute is the initialization for the variable, n. The next statement is the do-while loop construct. So, as soon as Java sees, do, it executes all the statements in the braces (curly brackets) of the construct. There are two statements in the do-block (construct). The first one displays the value of n. The second one increments, n.
Now, after the closing brace of the do construct, you have the word, while. do and while are reserved words. They are instructions to Java. After the word, while, you have a condition. So, do instructs Java to execute the statements within the braces. Immediately after that Java sees while. while evaluates the condition to see if the condition results in true. If it results in true (returns true), then the statements in the curly braces of the do-while loop construct are executed again. The while condition is checked again; if it is true the block of the do-while construct is executed again. This cycle repeats until the condition is false.
The highest value of n displayed is 4, but n arrives at a value of 5, since in the block, it is displayed before being incremented.
The syntax for the do-while construct is:
do
{
statements
} while (condition);
Note the semicolon just after the closing parentheses of the condition.
The while Loop Construct
The syntax for the while loop construct is
while (condition)
{
statements
}
The while loop construct is almost the same as the do-while loop construct with the following difference: There is no do instruction for the while loop. With the while loop, if at the start, the condition evaluates to false, the block is never executed. For the do-while loop, the block is evaluated at least once (the first time). The following while loop will do the same thing that the do-while loop above does:
class WhileLoop
{
public static void main(String[] args)
{
int n = 0;
while (n<5)
{
System.out.println(n);
++n;
}
}
}
Try the above code.
In the above code of the while loop, there are two main statements. The int initialization statement and the while loop statement (construct). Inside the while loop you have another important statement, which is the increment statement. These three statements can be recombined, as another loop called the for-loop. This is the same code in a for-loop:
class ForLoop
{
public static void main(String[] args)
{
for (int n=0; n<5; ++n)
{
System.out.println(n);
}
}
}
The for-loop begins with the reserved word, for, followed by parentheses, then the block to be executed. In the parentheses there are three expressions (statements), separated by semicolons.
In the parentheses, the first expression is the initialization for the variable, n. The next expression in the parentheses is the while condition we had. What pushed the while loop to be repeating orderly, was the incrementing of n, that is, ++n. In the parentheses of the for-loop, this is the third expression. There were two statements in the block of the while loop. One of the statements is now in the parentheses of the for-loop. The other one remains in the block of the for-loop.
Try the above code.
A simplified syntax for the for-loop is given below. The explanation is given after.
for ([initialExpression]; [condition]; [incrementExpression])
{
statements
}
When a for loop executes, the following occurs:
- The initial-expression (initialization statement), if any, is executed. This expression usually initializes a value to a variable (loop counter).
- The condition expression is evaluated. If the value of condition is true, the loop statements will execute. If the value of the condition is false, the for-loop ends. If the expression for the condition is omitted, the condition is assumed to be true.
- The block statements execute, if the condition was true.
- The increment (or update) expression, if there is one, executes, and control returns to Step 2.
The “break;” statement can be used to terminate a loop before its determined end. Try the following code and note that the loop ends after n is 2.
class Breaking
{
public static void main(String[] args)
{
for (int n=0; n<5; ++n)
{
System.out.println(n);
if (n == 2)
{
break;
}
}
}
}
Each time in the loop, the if-condition is checked for the value of true. When n is 2, the if-condition will return true; making the if-block to execute. In the if-block, you have just one statement, the break statement. It is just one word, break. Always end the break statement and other statements with a semicolon. The break statement stops the loop from repeating. In this case it stopped the loop when the internal if-condition occurred (was true).
The continue Statement
You can cause an iteration to be skipped as the loop is repeating. You use the continue statement for this. It is just one word, continue. Always end it with a semicolon. The following code illustrates this, when n is 2. The iteration for n equal 2 is skipped.
class Continuing
{
public static void main(String[] args)
{
for (int n=0; n<5; ++n)
{
if (n == 2)
{
continue;
}
System.out.println(n);
}
}
}
In order to skip the iteration of the block, you put the continue statement and its condition at the beginning of the block. Try the above code.
This is how the continue statement behaves:
- In a while loop, it jumps back to the condition.
- In a for-loop, it jumps to the update (increment) expression.
Note: a word for the phrase, reserved word, is keyword.
We have come to the end of this part of the series, we continue in the next part.
Chrys