for loop

for(Initialization; condition; increment/decrement){
  statement 1;
  statement 2;
  statement 3;
  ............
  statement m;
}
 statement n;
  1. Execute initialization.
  2. Test the condition,if it is true,execute statements in the for loop.
  3. Execute increment/decrement.
  4. Test the condition again,If it is true,repeats steps 2 and 3.if condition is false control goes out of loop and execute statements outside the loop.
  5. Statements in the for loop are executed repeatedly as long as condition is true.the moment condition is false control goes out of loop and statements outside the loop are executed.

Example:

class Test {

	public static void main(String[] args) {
		for(int i=1;i<=5;i++){
		 System.out.println(i);
		}
	}
}
/*
Output:
1
2
3
4
5
*/

Initialization Section

  1. Here,we can not declare different data types variables.
    int i=10,j=20//valid
    int i=10,byte j=20//Invalid
    int i=10,int j=20//Invalid
    
  2. In the initialization part, we can take any valid java statement including System.out.println() also.
    class Test {
    
    	public static void main(String[] args) {
    		int i = 0;
    		for (System.out.println("hello"); i < 3; i++) {
    			System.out.println("hai");
    		}
    	}
    }
    /*
    Output:
    
    hello
    hai
    hai
    hai
    
    */
    

Conditional Expression

  1. It can be any valid java expression which should return boolean value.
  2. This condition expression is optional and default value is true.
    class Test {
    
    	public static void main(String[] args) {
    		for (int i = 0;; i++) {
    			System.out.println("hi");
    		}
    	}
    }
    /*
    Output:
    hi
    hi
    .
    .
    infinity loop
    */
    

Increment/Decrement section

In this section we can place any void java statement including System.out.println().

class Test {

	public static void main(String[] args) {
		for (int i = 0; i < 3; System.out.println("hello")) {
			System.out.println("hi");
			i++;
		}
	}
}
/*
Output:
hi
hello
hi
hello
hi
hello

*/

All three parts of for loop are independent of each other and are optional
Example:

 for(;;)

Important Points

Example:1

class Test {

	public static void main(String[] args) {
		for (; true;) {
			System.out.println("hello");
		}
		System.out.println("hi");
	}
}
/*
Compile time error:Unreachable statement.
*/

Example:2

class Test {

	public static void main(String[] args) {
		for (;;) {
			System.out.println("hello");
		}
		System.out.println("hi");
	}
}
/*
Compile time error:Unreachable statement.
*/

Example:3

class Test {

	public static void main(String[] args) {
		int a = 10, b = 20;
		for (; a < b;) {
			System.out.println("hello");
		}
		System.out.println("hi");
	}
}
/*
Output:
hello
hello
hello
hello
....
....
Infinity loop
*/

Example:4

class Test {

	public static void main(String[] args) {
		for (; false;) {
			System.out.println("hello");
		}
		System.out.println("hi");
	}
}
/*
Compile time error:Unreachable statement.
*/

Nested for loop

loop inside another loop called Nested loop.

class Test {

	public static void main(String[] args) {
		for (int i = 1; i <= 2; i++) {
			System.out.println(i);
			for (int j = 1; j <= 3; j++) {

				System.out.println(j);
			}

		}
	}
}
/*
Output:
1
1
2
3
2
1
2
3
*/