The while statement continually executes a block of statements while a particular condition is true.
When condition is false, the control comes out of loop and jumps to the next statement after while loop.
Syntax:
while(condition){
// code to be executed
}
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.
Note: The most important point to note when we using the while loop we need to use increment or decrement statement inside the while loop so that the loop variable gets changed on each iteration, and at some point condition returns false.
// Java program to print number form 1 to 10
class Simple{
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 program to print number form 1 to 10
class Simple{
public static void main(String args[]){
int i = 1;
while(i <= 10){
System.out.println(i);
}
}
}
Output:
1
1
1
1
1
1
1
1
1
.
.
.
ctrl+c
Note: ctrl+c is used for stop the execution of this program and exit form the program.