Java- Loop control statements

Yugandharkumar
4 min readMar 9, 2023

--

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

  • for
  • while
  • do while
  • for each

for Loop:

Java for loop consists of 3 primary factors which define the loop itself. These are the initialization statement, a testing condition, an increment or decrement part for incrementing/decrementing the control variable.

The basic syntax of java for loop goes like this:

for(initializing statement;testing condition;increment/decrement)
{
//code to be iterated
}

The initializing statement marks the beginning of the loop structure. It contains a variable with some initial value that is defined by the programmer. This is the value of the control variable when the control shifts into the loop. However this statement is executed only once.
If a particular variable is declared within this part, then the scope of the variable remains limited within the loop.

The flow diagram for for loop is as shown below

For loop

While loop:

While loops are very important as we cannot know the extent of a loop everytime we define one. For example if we are asked to take a dynamic collection and asked to iterate through every element, for loops would be impossible to use because we do not know the size of the collection. Then we would have to use an enhanced for loop or a while loop.

A while loop iterates through a set of statements till its boolean condition returns false. As long as the condition given evaluates to true, the loop iterates.

The condition of the loop structure is checked at first and then the control proceeds into the loop structure only if the condition evaluates to true. Hence it is called an entry-controlled loop. The body of the loop generally contains a variable which controls the boolean condition mentioned.

The basic syntax of Java while loop is:

while(condition)
{
statement(s); //block of code
}

As soon as the condition hits false, the loop terminates.

Flow chart for while loop is as shown below

while loop

do-while Loop:

Java do while loop executes the statement first and then checks for the condition.Other than that it is similar to the while loop. The difference lies in the fact that if the condition is true at the starting of the loop the statements would still be executed, however in case of while loop it would not be executed at all.

This is an exit-controlled loop because of the fact that it checks the condition after the statements inside it are executed.

The syntax for do-while loop is:

do{    
//code to be executed / loop body
//update statement
}while (condition);

The flow chat for do-while loop is as shown below.

do-while loop

For-each Loop:

The Java for-each loop or enhanced for loop is introduced since J2SE 5.0. It provides an alternative approach to traverse the array or collection in Java. It is mainly used to traverse the array or collection elements. The advantage of the for-each loop is that it eliminates the possibility of bugs and makes the code more readable. It is known as the for-each loop because it traverses each element one by one.

The drawback of the enhanced for loop is that it cannot traverse the elements in reverse order. Here, you do not have the option to skip any element because it does not work on an index basis. Moreover, you cannot traverse the odd or even elements only.

But, it is recommended to use the Java for-each loop for traversing the elements of array and collection because it makes the code readable.

The syntax of Java for-each loop consists of data_type with the variable followed by a colon (:), then array or collection.

for(data_type variable : array | collection){  
//body of for-each loop
}

The flow chart for for-each loop is as shown below.

For-each loop

--

--

Yugandharkumar
Yugandharkumar

Written by Yugandharkumar

Android DEV skilled in Kotlin, Jetpack Compose, and MVVM. Passionate about building intuitive apps and exploring new tech. Let’s create something amazing! 🚀

No responses yet