← Java tutorial

Java

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.

Example
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);
    }
}
Output
Total: 15
Ready to go further?

These free tutorials are a preview, our Backend Development cohort covers it in depth, with a real project and a cohort alongside you. Closes Nov 30

Enroll in Backend Development

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account