WhileLoop
syntax:

while(condition){
 Stmt1;
 Stmt2;
 Stmt3;
 .....
 .....
 Stmtm;
}
Stmtn;
  1. Statements in while loop are repeatedly executed as long as the condition is true.
  2. Control goes out of the loop, the moment condition is false.
  3. Statements outside while loop are executed when condition is false.

Example:

class Test {

	public static void main(String[] args) {
		int x=1;
		while (x <= 5) {
			System.out.println(x);
			x++;
		}
	}
}
/*
Output:
1
2
3
4
5
*/
  1. The valid argument to the while loop is always boolean.
    class Test {
    
    	public static void main(String[] args) {
    		while (1) {
    			System.out.println("hi");
    		}
    	}
    }
    /*
    Compile time error:
    Incompatable types
    found:int
    required:boolean
    */
    
  2. Curly braces are optional.without curly braces we can take only one statement and that statement never be declaration statement.

Important Notes:

Example:

class Test {

	public static void main(String[] args) {
		while (true) {
			System.out.println("hi");
		}
		System.out.println("hello");

	}
}
/*
Compile time error:
Unreachable statement
*/

Example:

class Test {

	public static void main(String[] args) {
		while (false) {
			System.out.println("hi");
		}
		System.out.println("hello");

	}
}
/*
Compile time error:
Unreachable statement
*/

Example:

class Test {

	public static void main(String[] args) {
		int a = 10;
		int b = 20;
		while (a < b) {
			System.out.println("hi");
		}
		System.out.println("hello");

	}
}
/*
Output:
hi
hi
.
.
Infinity Loop
*/

Example:

class Test {

	public static void main(String[] args) {
		final int a = 10;
		final int b = 20;
		while (a < b) {
			System.out.println("hi");
		}
		System.out.println("hello");

	}
}
/*
Compile time error:
Unreachable statement
*/