Java While Loop
While loop is executing statement under certain condition expression. When expression result are true and non zero then this loop execute statements. Otherwise this are terminate the loop execution process. This loop are very useful when we are no know about how many times are loop statement are work. We can define while-loop using of this syntax.
while(expression){
//Statement's
}
For example
//Example in While loop
class Test{
public static void main(String[] args) {
int counter=0;
while(counter<=10){
System.out.println(counter);
//Updating terminating condition
counter+=2;
}
}
}
0
2
4
6
8
10
Do-while Loop
do-while loops is first executing given loop statement after that this loop are check terminating condition at the end. The main advantage of this loop that, it will be execute loop statement at least one time. When inside loop are not provide other terminate condition.
do{
//statements
//Update Expression Condition
}while(expression);
//Example in While loop
class Test{
public static void main(String[] args) {
int counter=0;
do{
System.out.println(counter);
//Updating terminating condition
counter+=2;
}while(counter<=10);
}
}
0
2
4
6
8
10
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment