Question #56743

How do you create a do while loop within a static method?

Expert's answer

The Java programming language provides a do-while statement, which can be expressed as follows:


do{
    statement(s)
} while (expression);


Notice that the Boolean expression appears at the end of the loop, so the statements in the loop execute once before the Boolean is tested.

If the Boolean expression is true, the control jumps back up to do statement, and the statements in the loop execute again. This process repeats until the Boolean expression is false. Fig. 1 shows the main idea of this loop.



Fig. 1

Example,


public static void main(String[] args) {
    int x = 1;
    do{
        System.out.print("value of x: " + x);
        x++;
        System.out.print("\n");
    } while(x < 11);
}


Result:

value of x : 1

value of x : 2

value of x : 3

value of x : 4

value of x : 5

value of x : 6

value of x : 7

value of x : 8

value of x : 9

value of x : 10

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS