A for loop in java consists of 3 parts, 1) starting value, 2) the limit, 3)Iteration; doesnt have to be necessarily iteration, can also decrement, multiply, add or any other condition you need. More advanced examples will be given on the next tutorials.
public class SimpleForLoop { public static void main(String[] args) { //Simple Sample for loops in java // WwW.Code-Blocks.OrG for (int i=0; i<10; i++) System.out.println("This is the value of i " + i); System.out.println("This line doesn't belong to for loop "); System.out.println("Lets leave some space between the for loops with backslash n \n \n \n "); // "\n" is an Escape Sequence for new line for (int z=0; z<5; z++){ System.out.println("This is the value of z " + z); System.out.println("This line belongs to for loop with z, because it is inside the loop brackets"); } } }
Below is the output of above code:
This is the value of i 0
This is the value of i 1
This is the value of i 2
This is the value of i 3
This is the value of i 4
This is the value of i 5
This is the value of i 6
This is the value of i 7
This is the value of i 8
This is the value of i 9
This line doesn’t belong to for loop
Lets leave some space between the for loops with backslash n
This is the value of z 0
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 1
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 2
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 3
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 4
This line belongs to for loop with z, because it is inside the loop brackets