Introduction

Loops apply the same steps to many values. Start with a small collection so you can follow each iteration. A loop must eventually finish; make sure its condition or sequence moves toward an end.

Syntax & example

public class Main {
  public static void main(String[] args) {
    for (int number = 1; number <= 5; number++) {
      System.out.println(number);
    }
  }
}

Expected output

1
2
3
4
5

Common mistakes

  • Using a variable before assigning a value to it.
  • Mixing text and numbers without converting deliberately.
  • Skipping an empty input or a boundary value when testing.

Practice

Print the numbers from 1 to 5.

13px
Main.javaYour ideas, in code.
Loading your editor…
Run your code to see the output here.
Ready to buildUTF-87 lines