You are here: Home / Topics / For each loop example in Java

For each loop example in Java

Filed under: Java on 2023-08-18 06:53:12

// foreach example

public class ForEachLoop {

   public static void main(String[] args) {
       int arrayname[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};

//learn
       for (int val : arrayname) {
           System.out.print(" "+val);
       }

       System.out.println();

       //explore
       for(float val : arrayname){
           System.out.print(" "+val);
       }

       System.out.println();
       
       for(double val : arrayname){
           System.out.print(" "+val);
       } 
  }

}


Output:

1 3 5 7 9 11 13 15 17 19 21
1.0 3.0 5.0 7.0 9.0 11.0 13.0 15.0 17.0 19.0 21.0
1.0 3.0 5.0 7.0 9.0 11.0 13.0 15.0 17.0 19.0 21.0

 

Note: For each loop is used to iterate the items of an array.


About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.