While loop in java
previous NextThe while loop is a control flow statement that first checks the condition in the while loop statement. If the given condition is true then it executes the statement written in the while loop and if the condition is false, it will terminate the loop. the only type of condition will be boolean, In this loop, the user doesn't know how many times it will execute the loop statement. The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to use the while loop.
whenever we don't know in advance that how many times the loop will execute when we should go with the while loop. The argument in the while statement should be Boolean type. If we use any other type then we will get a compile-time error.
Syntex
while(expression){ //code //update_expression; }
Example of While loop in java
class WhileLoopExample {
public static void main(String args[]){
int i = 1;
while (i <= 6) {
System.out.println("Hello World : "+i);
i++;
}
}
}
Output :
Hello World : 1 Hello World : 2 Hello World : 3 Hello World : 4 Hello World : 5 Hello World : 6
Infinitive while loop in Java
class WhileLoopExample {
public static void main(String args[]){
int i = 1;
while (i <= 6) {
System.out.println("Hello World");
}
}
}
Output :
Hello World Hello World Hello World Hello World Hello World . . nth time Ctrl+c // for stop the infinite loop
