For Loops

In this example, we count by 5's. Notice the n+=5 as the last part of the for loop

1
2
3
4
5
6
7
8
9
10
11
//for loop prints 5 10 15 20 25 30 35 40 45
package numbers;
public class Numbers {
  public static void main(String[] args) {
    int n;
    for(n=5; n<50; n+=5) { //count by 5
      System.out.print(n+" ");
    }//loop
    System.out.println();
  }//main
}//class
CODE

Output:

5 10 15 20 25 30 35 40 45 

You should be able to use a for loop to display any sequence of values.

NEXT: