·  JAVA – Syntax – Conditional Statements – do ... while

 

- Info:

  - This tutorial shows how to use do...while conditional statement.

 

- Procedure:

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

      

      public static void main (String arg[]) throws Exception {

   

        //Initialize variable that will be used as counter.

        int i=0;

 

        //SIMPLE. Output is "1 2 3 4 5".

        do{

          i++;

          System.out.println(i);     

        }while(i<=4);

 

        //BREAK.  Output is "1 2" since we break out of for loop before condition i<=4 is reached.

        do{

          i++;

          if(i==3) { break; }

          System.out.println(i);

        }while(i<=4);

 

        //CONTINUE. Output is "1 2 4 5" since display of number 3 is skipped.

        do{

          i++;

          if(i==3) { continue; }

          System.out.println(i);

        }while(i<=4);

 

      }

      

    }