1// Starting on 0:
2for(int i = 0; i < 5; i++) {
3 System.out.println(i + 1);
4}
5
6// Starting on 1:
7for(int i = 1; i <= 5; i++) {
8 System.out.println(i);
9}
10
11
12// Both for loops will iterate 5 times,
13// printing the numbers 1 - 5.
1int values[] = {1,2,3,4};
2for(int i = 0; i < values.length; i++)
3{
4 System.out.println(values[i]);
5}
1package myForLoops
2
3public class forLoop {
4 public static void main(String[] args) {
5
6 for (int i = j; i<=k; i++)
7 //variable "i" is the value checked in loop
8 //variable j is the starting value for i
9 //variable k is where we want the loop to end
10 //i++ adds 1 to i each iteration of the loop until i == k
11 {
12 //displays "i"
13 System.out.println(i);
14 }
15
16 //this is a for loop in use
17 for (int i = 1; i<=10; i++) {
18 System.out.println(i); //i will be displayed 10 times being
19 //increased by 1 every time
20 }
21
22 for (char ch = 'k'; ch<= 'q'; ch++) {
23 //this is a for loop with characters, it will
24 //display every character from 'k' to 'q'
25 System.out.println(ch);
26 }
27 for (int i = 10; i>=1; i++) {
28 //this loop will be infinitely run since i
29 //will always be greater than 1, we do not want this
30 System.out.println(i);
31 }
32 for (int i = 10; i>=1; i--) {
33 //this is the correct way of doing the previous code
34 //(if you do not want an infinite for loop)
35 System.out.println(i);
36 }
37 }
38}