Loops in Java

Loops in java are used to execute a set of statements repeatedly until a condition is satisfied or to iterate a collection of objects.

There are 4 types of loop:-

  • The basic for loop
  • The enhanced for loop
  • The while loop
  • The do while loop
  • The forEach loop

The basic for loop

The basic for loop has the following syntax:-

for(initialization ; condition ;counter increment/decrement){

}

If we have only one statement in the for loop then the curly braces are not mandatory.

Step 1 :- The initialization part of the loop executes for the first time.

Step 2 :- The condition is checked .If the expression evaluates to true the statements inside the for block is executed else the control flow exits the loop that is step 4

Step 3 :- The counter is incremented or decremented.

We repeat 2 and 3 phases until the condition is not met and the for loop is terminated

Step 4 :- Exit the loop

The enhanced for loop

The enhanced for loop is used to iterate a collection of objects or an array in java

Syntax:-

for(data_type item:collection/array){

//statements ….

}

  • Item is the single item from the collection 
  • Collection /array is the variable which you have to iterate or loop through 

Here’s the flow of the enhanced for loop:-

  • Iterate through each item in the array /collection
  • Places each item in the variable 
  • Executes the body of the loop

The loop iterates through all the objects in the collection/array one by one.

The while loop

The while loop is used to execute a set of statements until a condition is satisfied.

Here’s the flow:-

  1. Loop starts with the condition.If the condition is true step 2 is executed.If false the loop exists that is step 3.
  2. The statements inside the loop is executed .Usually the statements also update the variable being used in the conditional statement(at the start of the loop)
  3. Exit the loop

The do while Loop

The do while loop make sure the statements in the block is executed at least once irrespective of the conditional expression in while .

Syntax :-

Do {

..//statements 

}while(expression);
It ends with a colon.

Here’s the flow:-

Step1 :- The statements is executed in the do while block 

Step2 :- The expression is evaluated.If it is true the statement in the block is executed else the control flow exists the do while block that is step3 

Step3 :- Exit the do while block.

The forEach method

In Java 1.8 a new forEach method is introduced to iterate the elements.It is defined in Iterable and Stream interface.It’s a default method in iterable interface.

We can loop a collection of objects using the forEach method as the collection interface extends the Iterable interface.

The forEach method take functional interface (Consumer ) as the arguments so we can use lambda expressions along with the forEach method.

That’s all about loops in Java.I hope you enjoyed reading this article.

Happy Coding 🙂



Published by Anna

Just a simple girl who loves to code. .I am primarily a Java developer by profession and I am here to share my love for coding with you all I hope you enjoy reading my blog. Happy Coding Guys :-)

Leave a comment

Your email address will not be published. Required fields are marked *