Where we need a looping statements:
When we have repeated actions on that time we need to go to looping statements.
Java While Loop
The Javawhile loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.
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.
while (condition){   Â
//code to be executed  Â
Increment / decrement statement Â
}Â
Flowchart of While loop:
Here, the important thing about while loop is that, sometimes it may not even execute. If the condition to be tested results into false, the loop body is skipped and first statement after the while loop will be executed.
Example:
In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and increment the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.
Whileloop example in java:
public class WhileExample { Â
public static void main(String[] args) { Â
    int i=1; Â
    while(i<=10){ Â
        System.out.println(i); Â
    i++; Â
    } Â
}Â Â
}Â Â
Output:
1
2
3
4
5
6
7
8
9
10
Java Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.
- while(true){
- //code to be executed
- }
Example:
public class WhileExample2 {   Â
public static void main(String[] args) {  Â
 // setting the infinite while loop by passing true to the condition Â
    while(true){   Â
        System.out.println("infinitive while loop");   Â
    }   Â
}Â Â Â Â
}Â Â Â Â
Output:
infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop ctrl+c
In the above code, we need to enter Ctrl + C command to terminate the infinite loop.
Source: javapoint.com