Java

For and for-each loops in Java | Explained

Loops are one of the most essential concepts of any programming language. A loop is a block of statements/conditions that can be used to repeat a block of code until it met the specified condition. Java provides multiple looping structures such as for loop, for each loop, etc. and all these loops have the same goal of repeating a specific code block in a sequence.

This write-up will provide a detailed understanding of for loop and for-each loop and in this regard, we will follow the below-given concepts:

  • for and for-each Loop in Java
  • Basic Syntax of for and for-eachLoop
  • How to Use for and for-eachLoop

So, without a further delay let’s start!

for Loop in Java

The for loop is one of the basic and easy-to-understand loops that is most commonly used when the number of iterations is fixed or the exact number of iterations is already known.

Syntax

The below-given snippet shows the basic syntax of for loop:

for (initialization; condition; increment/decrement) {

  // Statement(s)

}

The syntax of for loop starts with the keyword “for” followed by round brackets () and within the brackets, we have to provide three expressions:

  • Initialization: determines the starting point of the loop and it will execute only one time when the loop starts.
  • Condition: determine whether the loop iteration would execute or not.
  • Increment/decrement: determine whether the counter will be incremented or decremented after each iteration.

How to Use for Loop

For a better understanding of how for loop works, consider the below-given example.

Example

In this example we will print “Welcome to LinuxHint” ten times using the for loop:

for (int i = 0; i <10; i++)

{

System.out.println("Welcome to LinuxHint");

}

In the above code snippet, the first we initialized a variable “i” with the value “0”.

The second statement/condition tests whether the variable “i” is less than 10. If the condition is true then it will enter the body of the for loop and prints “Welcome to LinuxHint”. This condition executes until “i” is less than 10 and the loop will terminate when “i” becomes greater than or equal to 10.

Finally, we incremented the value of variable “i”, i.e. in the first iteration, its value is 0, after each successful iteration its value will be increased by 1 and the loop negates the condition(i<10).

The complete code along with the output is shown in the below-given figure:

Output authenticates the working of for loop.

What is for-each Loop

The for-each loop is mostly used to traverse the array or list, as it traverses the whole array therefore there is no need to specify increment or decrement condition.

Syntax

The basic syntax of the for-each loop is:

for (data-type variable-name : array-name) {

  // statement(s)

}

The syntax starts with for keyword same as the classic for loop followed by parentheses () which takes three statements:

  • data type: determine what type of data the for each loop will take.
  • variable name: it can be any user-defined legal identifier.
  • array name: the name of the array to be traversed.

How to Use for-each Loop

Let’s consider the below-given example for a profound understanding of for-each loop.

Example

This example specifies a string type array that holds the employee names. We will use the for each loop to traverse all the array values:

String[] employeeName= {"John", "Joe", "Micheal", "Seth", "Dany"};

for (String name : employeeName) {

  System.out.println(name);

}

The below snippet will provide the complete code and output of for-each loop:

The “employeeName” array contains names of five employees, and the output verifies that for-each loop iterates through each entry of the array and printed them out.

Conclusion

The for loop is highly recommended when the number of iterations is fixed and you have clarity for how many time you are going to execute some particular piece of code. On the other hand, the for-each loop is preferred for the scenarios where we have to traverse the whole array/list. This article provides a detailed understanding of for loop and for-each loop in Java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.