Signup/Sign In

Java For Each Loop Programs

In this tutorial, we will learn how to implement a for-each loop in different scenarios. But before moving further, if you are not familiar with the concept of the for-each loop, then do check the article on Loops in Java.

Syntax:

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

Program 1: Java For-each loop Program

In this program, we will see how to print the array elements using a for each loop with pre-defined values.

Algorithm:

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Traverse the array by using a for each loop.
  5. Print the array elements.
  6. Stop

Below is the code for the same

//Java Program to see the implementation of the for-each loop in Arrays
public class Main
{
     public static void main(String []args)
     {
        int arr[]={1,3,4,7,8,5,4};  
        System.out.println("The Array elements are ");
        //traversing the array with for-each loop  
        for(int i:arr)
        {  
           System.out.println(i);  
        }  
     }
}


The Array elements are
1
3
4
7
8
5
4

Program 2: Java For-each loop Program

In this program, we will see how to print the collection elements using a for each loop with pre-defined values.

Algorithm:

  1. Start
  2. Declare an ArrayList of string type.
  3. Add elements to the ArrayList.
  4. Traverse the ArrayList by using a for each loop.
  5. Print the ArrayList elements.
  6. Stop

Below is the code for the same.

//Java Program to see the implementation of the for-each loop in Collections
import java. util.*;  
public class Main
{  
  public static void main(String args[])
  {  
   //Creating a list of elements  
   ArrayList<String> list=new ArrayList<String>();  
   list.add("Cat");  
   list.add("Dog");  
   list.add("Cow"); 
   list.add("Tiger"); 
   list.add("Lion"); 
   //traversing the list of elements using for-each loop 
   System.out.println("The elements in the list are: ");  
   for(String str:list)
   {  
     System.out.println(str);  
   }  
  
 }   
}  


The elements in the list are:
Cat
Dog
Cow
Tiger
Lion

Difference between Java For-Loop and For-each Loop

Here, we will see how a for-each loop differs from a for loop. The major difference between the for loop and for each loop is that for loop provides us the opportunity to control the looping process.

Program 3: Java For-each loop Program

In this program, we will see the implementation of a for-each loop in a user-defined program.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare an array.
  4. Ask the user to initialize the array using a for each loop.
  5. Print the array elements using the same for each loop.
  6. Stop.

Below is the code for the same.

//Java Program to see the implementation of for-each loop
import java.util.*;  
public class Main
{  
  public static void main(String args[])
  {  
     //Take input from the user
     //Creates an instance of the Scanner Class
    Scanner sc=new Scanner(System.in);
    int arr[]=new int[5];     //Declare an array
    System.out.println("The elements in the array: ");
    for (int i : arr) 
    {
        i=sc.nextInt();      //Initialize the array elements
        System.out.println(i);   //Print the array elements
    }
 }   
}  


The elements in the array:
4
5
3
2
1

Program 4: Java For Loop Program

In this program, we will see the implementation of a for loop in a user-defined program.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare an array.
  4. Ask the user to initialize the array using a for loop.
  5. Print the array elements using the same for loop.
  6. Stop.

Below is the code for the same.

//Java Program to see the implementation of for loop
import java.util.*;  
public class Main
{  
  public static void main(String args[])
  {  
     //Take input from the user
     //Creates an instance of the Scanner Class
    Scanner sc=new Scanner(System.in);
    int arr[]=new int[5];     //Declare an array 
    //Initialize the array elements
    System.out.println("Initializing the elements in the array: ");
    for (int i=0; i< arr.length;i++) 
    {
        arr[i]=sc.nextInt();
    }
    //Print the array elements
    System.out.println("The elements in the array: ");
    for (int i=0; i<arr.length;i++) 
    {
        System.out.println(arr[i]);
    }
 }   
}  


Initializing the elements in the array: 4 5 3 2 1
The elements in the array:
4
5
3
2
1



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.