Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

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);
    }
  }
}

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free