DO- WHILE LOOP :-
The Java do-while loop is used to iterate a part of the program repeatedly, until the specified condition is true. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop.
SYNTAX:-
do{
// code to be executed / loop body
// update statement
} while(condition);
The different parts of do-while loop:
1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to update expression. As soon as the condition becomes false, loop breaks automatically.
Example:
i <=100
2. Update expression: Every time the loop body is executed, the this expression increments or decrements loop variable.
EXAMPLE:-
public class example{
public static void main(String args[]){
int i=1;
do{
System.out.println(i);
i++;
} while(i<=10);
}
}
0 Comments