The Future Is Behind You

For Loop

A for loop loops through a set of numbers starting at zero until its criteria is met.

import java.util.Scanner;

public class ForLoop {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("What message do you want to repeat? ");
    String line = in.nextLine();

    for (int i = 0; i < 10; i++) {
      System.out.format("%d. %s%n", i, line);
    }
  }

}

The output is as follows:

What message do you want to repeat? Hi
0. Hi
1. Hi
2. Hi
3. Hi
4. Hi
5. Hi
6. Hi
7. Hi
8. Hi
9. Hi

Imagine what would happen if you put i = i + 2 instead of i++. Give it a try and see what happens.

Source Code

a = b = c