Language syntax

Java enhanced for statement

Visit each element in iteration order. An empty array yields no iterations; assigning a primitive iteration variable does not modify the source.

Formal Syntax & Structure

java syntaxjava
for (Type element : arrayOrIterable) { statements }

Working Example

Example Snippetjava
public class Main {    public static void main(String[] args) {        int[] weights = {3, 5};        for (int weight : weights) { System.out.println(weight); }    }}
Java Reference