Language syntax

Java basic for statement

Use a bounded counter for regular work. Initialize once; check the condition before each iteration.

Formal Syntax & Structure

java syntaxjava
for (initialization; condition; update) { statements }

Working Example

Example Snippetjava
public class Main {    public static void main(String[] args) {        for (int n = 2; n <= 6; n += 2) { System.out.println(n); }    }}
Java Reference