Loops
Java's for loop has three parts: a starting statement, a
continue condition, and an update step, all separated by semicolons.
Example: summing with a for loop
public class Main {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
System.out.println("Total: " + total);
}
}
Total: 15
This exact three-part for (start; condition; update)
shape appears, essentially unchanged, in JavaScript, C, C++, and C# —
learning it once here transfers almost directly to every other
C-family language you might pick up later, which is a big part of why
starting with a language like Java or C++ makes the syntax of many other
languages feel immediately familiar.
public class Main {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
System.out.println("Total: " + total);
}
}
Total: 15