Java Academy · Lesson

For Loop Basics

Understand for loop syntax, count up and down, accumulate totals, and use break/continue.

Lesson 1 of 38 steps

For Loop Basics is a free Java Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

For Concept

For loop repeats code with three parts in one line: init; condition; update.

for (int i = 0; i < 3; i = i + 1) {
System.out.println("Loop " + i);
}

Count Up

Count up 1..5.

for (int i = 1; i <= 5; i = i + 1) {
System.out.println(i);
}

Count Down

Count down 5..1.

for (int i = 5; i > 0; i = i - 1) {
System.out.println(i);
}

Accumulate

Sum 1..n

public class Main {
  public static void main(String[] args) {
    int sum = 0; // start with 0

    // Loop from 1 to 5 (inclusive)
    for (int i = 1; i <= 5; i = i + 1) {
      // Add current i to sum
      sum = sum + i;
    }

    // Print the result
    System.out.println("Sum = " + sum);
  }
}

Break/Continue

break exits the loop. continue skips to the next iteration.

public class Main {
  public static void main(String[] args) {
    // Loop from 1 to 5
    for (int i = 1; i <= 5; i = i + 1) {
      if (i == 3) continue; // skip printing when i == 3
      if (i == 5) break;    // stop the loop completely when i == 5

      System.out.println(i); // print i if not skipped or stopped
    }
  }
}

For Demo

Run it: See for loops for totals, factorial, and a small text triangle.

public class Main {
  public static void main(String[] args) {
    // Sum of even numbers 1..10
    int sumEven = 0;
    for (int i = 1; i <= 10; i = i + 1) {
      if (i % 2 == 0) sumEven = sumEven + i;
    }
    System.out.println("Sum of even 1..10 = " + sumEven);

    // Factorial of 5
    int fact = 1;
    for (int i = 1; i <= 5; i = i + 1) {
      fact = fact * i;
    }
    System.out.println("5! = " + fact);

    // Simple pattern (triangle)
    for (int row = 1; row <= 3; row = row + 1) {
      String line = "";
      for (int col = 1; col <= row; col = col + 1) {
        line = line + "*";
      }
      System.out.println(line);
    }
  }
}

5..1 Loop

Quick check: Which for loop prints 5 4 3 2 1 and then stops?

Recap & Next

Recap: You learned for loop syntax, counting up/down, accumulation, and break/continue.

Next: Practice patterns with nested for loops.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “For Loop Basics” lesson free?

Yes — the full text of “For Loop Basics” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “For Loop Basics”?

Understand for loop syntax, count up and down, accumulate totals, and use break/continue. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “For Loop Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. For Loop Basics
  2. Nested For & Text Patterns
  3. Pattern Practice & Mini-Challenges
← Back to Java Academy