Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

Write a Java program to demonstrate for each loop?

In Java, for-each loop traverses an array or collection of elements one by one. In place of declaring and initializing a loop counter variable, you declare a variable of the same type as the array's base type, followed by a colon and the array name.

Syntax:


for (type var : array) 
{ 
    //statements to be executed
} 

Let’s look at the following Java program using a for-each loop that prints the elements of an Array.


class Example {
  public static void main(String[] args) {
     
    int[] num = {1, 3, 5, 7};
   
    // for each loop
    for (int num1: num) {
      System.out.println(num1);
    }
  }
}
LambdaTest

Test your websites, web-apps, or mobile apps seamlessly with LambdaTest.

Start Free Testing
LambdaTest

Earn resume-worthy Selenium certifications that help you land a top job.

Learn More
LambdaTest

Test your web or mobile apps

Test orchestration and execution cloud of 3000+ browsers and OS

Support

24/7 support

Security

Enterprise grade security

Cloud

Fastest test execution cloud