The while loop repeatedly executes a block of statements until a particular condition is true. It first check the condition and executes a block of statements if condition is true.
Syntax:
while(condition){
//Block of statements
} |
while(condition){
//Block of statements
}
Program to use while loop example in java.
/**
* Program to use while loop example in java.
* @author W3spoint
*/
public class WhileLoopExample {
static void whileLoopTest(int num){
int i = 1;
//while loop test
while(i<=num){
System.out.println(i);
i++;
}
}
public static void main(String args[]){
//method call
whileLoopTest(10);
}
} |
/**
* Program to use while loop example in java.
* @author W3spoint
*/
public class WhileLoopExample {
static void whileLoopTest(int num){
int i = 1;
//while loop test
while(i<=num){
System.out.println(i);
i++;
}
}
public static void main(String args[]){
//method call
whileLoopTest(10);
}
}
Output: