Sunday 28 August 2016

Java do while

1. do while :

Syntax:

do
{
    statements;
    Increment / decrement
}  while(expression) ; 

Note : here semicolon is mandatory after while in syntax.
  • According to the syntax, the checking of expression will be done after the code got executed.
  • do while loop holds an expression.
  • expression gives the result either true or false

  • if the result is true then do while statements will executes.
  • if the result is false then do while loop terminates the execution.
  • So, do_while loop will execute at least one time even though the expression returns false.
--------------------------------------------------------------------------------
Program         :      A program to print Happy Bday 5 times
Program name  :      DoWhileDemo1.java
Output            : 
                             Happy Birthday
                             Happy Birthday
                             Happy Birthday
                             Happy Birthday
                             Happy Birthday

class DoWhileDemo1
{
        public static void main(String args[])
        {
               int i = 1;
               do
               {
                    System.out.println("Happy Birthday");
                    i++;
               }while(i<=5);
        }
}

Compile     :            javac DoWhileDemo1.java
Run           :            java DoWhileDemo1
Output       : 
                             Happy Birthday
                             Happy Birthday
                             Happy Birthday
                             Happy Birthday
                             Happy Birthday            
--------------------------------------------------------------------------------
Program         :      A program to print Happy Bday 1 time
Program name  :      DoWhileDemo2.java
Output            : 
                             Happy Birthday

class DoWhileDemo2
{
        public static void main(String args[])
        {
                        int i = 1;
                do
                {
                    System.out.println("Happy Birthday");
                    i++;
                }while(i>=5);
        }
}

Compile     :            javac DoWhileDemo2.java
Run           :            java DoWhileDemo2
Output       : 
                             Happy Birthday         
  --------------------------------------------------------------------------------
Program         :      A program to print 1 to 5 values
Program name  :      DoWhileDemo3.java
Output            : 
                             i value is : 1
                             i value is : 2
                             i value is : 3
                             i value is : 4
                             i value is : 5

class DoWhileDemo3
{
        public static void main(String args[])
        {
                        int i = 1;
                do
                {
                    System.out.println("i value is : ”+i);
                    i++;
                }while(i<=5);
        }
}

Compile     :            javac DoWhileDemo3.java
Run           :            java DoWhileDemo3
Output       : 
                             i value is : 1
                             i value is : 2
                             i value is : 3
                             i value is : 4
                             i value is : 5      
  --------------------------------------------------------------------------------

Thanks for your time.
- Nireekshan