http://www.technicalpage.net/search/label/SQL

>> "Do While" loop in Java


"Do While"  loop in Java
This is similar to While loop , except this loop executes at least once even though the loop condition is not satisfied.

For example,
In below loop, the value of  "n" is 10 initially. And the loop will add 2 each time to the value of "n" as long as  the value is less than 10. But value of "n" is already 10 and it will never be less than 10. So the loop ideally should not run in normal loop condition. However, in "Do While"  , the code inside loop will execute at least once.

public class DoWhile {

       public static void main(String[] args) {

              int n = 10;
              do {
                     System.out.println("The value of n is : " + n);
                     n = n + 2;
              } while (n < 10);
       }
}

Output:
The value of n is : 10

No comments:

Post a Comment