Fallowing icon

Wednesday, September 26, 2012

do-while loops



do-while statement

   The do-while statement is used for execute a block of statements while the test condition (Boolean expression) is true. It's like a wheel,It's execute again and again when the particular condition is true.


   The structure of the do-while statement below-


           do{

              Statement1;
              Statement1;
              Statement1;
              ..........;
              ..........;
           }while(Boolean Expression);

   
   There are two statements in this structure, do and other one is while. The do-while loop is started on do statement, then executed the statements are in do block. After execute while statement.

   while statements have a round brackets. Insert the test value(Boolean expression) between this brackets. The expression must return the boolean value. You can use any primitive data types for while boolean expression. If the expression return true, The do statement is execute again till the expression return false. 

Important-

    The while expression is establish after the do statement in this structure. So program first execute do statement, then check boolean expression. If the boolean expression is false when the do statement is start, one time execute the statements are in do block then catch false value and throw the do-while statement.


Example 1-

    
         class Test{
       
                    public static void main(String args[]){
    
                            int age=1, year=1989;                     //declare the variables age and year, then assign the values 1 and 1989   
          
                            do{
                                  year++;                                         //add one for year variable
           
                                  System.out.println("I am a "+age+" years old on "+year);
           
                                  age++;                                          //add one for age variable
           
                              }while(year<=2012);                       //Check the year less than or equal to 2012
       
                      }
              }





   //Output is-  I am a 1 years old on 1990
                        I am a 2 years old on 1991
                        I am a 3 years old on 1992
                         .............................................
                         .............................................
                        I am a 24 years old on 2013


    First declare the variables age and year then assign the 1 and 1989. then add one for year and print age and year, after add one for age.  The while statement continues testing the expression and executing do block until the expression evaluates to false.  



      
Example 2-


        public class Test { 
    
                     public static void main(String args[]){
    
                               char x='A';                                         //declare the variable x and assign A  
          
                               do{                                                     //declare the do while statement
                                  System.out.print(x);
                                  x++;
                               }while(x<='Z');                                   
       
                      }
               }





      //Output is- ABCDEFGHIJKLMNOPQRSTUVWXYZ     


   We deal with the ascii values in this example. First we declare the variable x and assign the value A(ascii-65). Then declare the do statement and print x value. After add on(add one for ascii value) for the x variable then check x value is less then or equal Z(ascii-90). So program give output for  ABCDEFGHIJKLMNOPQRSTUVWXYZ. 


  
while statement 


   while statement is similar to the do-while statement.

   
   The structure of the do-while statement is- 


                    while(Boolean Expression){

                                                                 statement1;
                                                                 statement2;
                                                                  ..................;
                                                                  ..................;
         
                           }


     The while statement start on the word while, then while statement have round brackets. The boolean expression is used between the brackets. The while statement is first check boolean expression and then execute statements in while block. 


Example -
   

     public class Test { 
    
                     public static void main(String args[]){
     
                                int roundNo=1,value=0;
                                while(roundNo<10){
                                       System.out.println("Round no ="+roundNo+" Value is "+value);
                                        value+=7;
                                        roundNo++;
                                }  
                       }
                }





     //Output is-  Round no =1 Value is 0
                          Round no =2 Value is 7
                          Round no =3 Value is 14
                          ..........................................
                          Round no =9 Value is  56


        First we declare the variables roundNo and value then assign the values 1 and 0. After roundNo<10 is used for the while expression. Print roundNo and value variable using print statement, then add 7 for value variable and add 1 for ronunNo variable. While loop expression is return true for till the value is 9.  


No comments:

:a   :b   :c   :d   :e   :f   :g   :h   :i   :j   :k   :l   :m   :n   :o   :p   :q   :r   :s   :t

Post a Comment