One of the basic element of a programming language is its loop control. It’s ability to iterate over a collection of elements and then get the desired result. In java, this is no different than any other programming languages such as C and C++. Like for instance we want to print all elements of an array or other java collections, we will be using loops to iterate through the array contents and then print one by one.

On this java tutorial we will be showing different ways and scenarios on every single available loop statements in java. We will be showing as well on the usage of break and continue in combination with different loop statements. To add more spice, we will be using as well on some examples the combination of loops and conditional statements.

There are 4 loop statements in java. These are the following:

  • while
  • do while
  • for
  • for each

The while loop

The while statement continues to execute a block of statements while the condition specified is still true.

while loop Syntax:

while (boolean_expression) {
statement(s)
}

the boolean_expression must be evaluated to boolean data type which means it should be equal to true or false. In order for the loop to iterate over the statement(s) specified, the expression must be be evaluated to true. The loop will continue to evaluate the statement block/s until the boolean_expression is still true or we have not yet invoked the break statement. More on the break statement later on this document.

while Loop Example:

Let’s take an example scenario wherein we want to sum up numbers from 1 t0 20. Instead of doing 1+2+3…+n which requires a lot of effort and patience to do so. And what if the range changes dynamically? The while loop will come in handy to handle situation like this.

Running the program above will give you the following output:

The example above will give the sum of numbers 1 to 5. Initially we assign a variable i to 0. and we put the boolean expression on the while as i < 0. And inside the while loop our intention is to increase the number of i by 1 and then at the same time we would add the value to the sum variable. The increasing of variable value to can easily be done using the increment operation i++. And then we could gather the sum value by adding the previous value of sum to the current value of i. This example become tricky given that we have initially initialized the value of i to be 0 and the boolean_expression on the while loop is i < 5. If we follow the looping, the value of i would be 0,1,2,3,4 and the loops terminates. Then we will be missing the 5 and then we have added 0 on getting the sum. That’s why on the solution presented above, we have done the following:

the i++ is cleverly placed before the operation in getting the sum because of the initial value of i which starts at 0 and also considering the boolean_expression that would only be true if i is less than 5. So what if we have initially initialized the value of i to 1. We should be having the following code block instead of the above implementation:

Even though we have different implementation of the while loop, it would achieve the desired result. You might be asking which one is better? Depending on the situation, the business requirements and your coding style and preference, you might opt to select any of the two approach. It’s just a matter of preference.

while loop and break statement

What if we want to continuously do the loop until a certain condition has been meet outside the boolean expression declared on the while loop? On this kind of scenario, the break statement becomes handy. The following code block is one way to do an infinite loop and then at some point we terminate it.

Make sure to properly watch for loops which will not terminate at all (infinite loop). It’s a common mistake for beginners in java programming and will result problems like out of memory error.

The do while Loop

This looping mechanism in java as the statement suggest that it will do first a statement block while the boolean_expression evaluates to true. The syntax is conveniently structured, for us not to get confused.

do while Loop Syntax

do {
statement(s)
} while (boolean_expression);

the boolean expression on the while parameter must be evaluated to boolean true or false. Basing on the structure of the do while loop, even the boolean_expression is evaluated to false, the code block inside the do statement will get evaluated at least once. Let’s take the same example as that on the while loop for you to differentiate the two.