Foreach loop in java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn foreach loop in java.

Foreach loop or new version of for loop is used with arrays and loops through all the elements without using index or counter.

Foreach loop in java optimizes loop and reduces the code. Hence saving time in typing code. The data type declared in foreach loop must match datatype of the array or list that we are iterating.

Syntax:

for(<DataType of array><temporary variable name> : <Array to be iterated>)
{
   System.out.println(variable name);
}

Example:
public class ForEachDemo
{
   public static void main(String[] args) 
   {
      String numbers[] = {"Dhoni","Sachin","Raina","Rohit"};
      System.out.println("---------------------------------------------");
      System.out.println("Iterating over array using classical for loop");
      System.out.println("---------------------------------------------");
      for(int a = 0; a < numbers.length; a++)
      {
         System.out.println(numbers[a]);
      }
	
      System.out.println("---------------------------------------");
      System.out.println("Iterating over array using foreach loop");
      System.out.println("---------------------------------------");
      for(String num : numbers)
      {
         System.out.println(num);
      }
   }
}

Now let us see how to use foreach loop or new version of for loop. First we have to create an array of type string “numbers”.

First number is “Eleven”, second “Twelve”, third “Thirteen” and “Fourteen”.

First we need to write the keyword “for” and then a pair of parentheses and then the curly braces for body of foreach loop.

Also read – 2d array java

Foreach loop loops through each element of array that we pass. In the above program “numbers” array is passed. It loops through each element of “numbers” array.

First thing we need to do is we need to create a variable which is going to contain elements of “numbers” array.

Here array elements are of type string so I have created a string variable and let’s call it as “num”.

Then add colon ( : ) and then array name, that is, “numbers” array. Inside foreach loop print value that is present in “num” variable.

Also read – array java

Now what happens in foreach loop is, for first iteration “num” variable contain first element of “numbers” array.

In this case “Eleven”. In the second iteration “Twelve”. Similarly foreach loop loops through all the elements of the array. Here’s the output,

Output:

foreach loop in java

As you can see in the above output I have also used the classical for loop to loop(counter, condition, and increment) through “numbers” array.

NOTE

  • Iterating through characters in a string is forward only and in single step.
  • Foreach loop is not suitable when you want to modify an array.
  • In enhanced for loop we cannot process another decision making statements (if else statement).
  • As we learnt earlier enhanced for loop cannot obtain index. Hence can’t keep track of index.

Related Posts