Java-Loops


  • Loops are very useful mechanism to execute a code several times in java programming.
  • Java has 3 looping mechanisms. They are
    • For loop
    • While loop
    • do....while loop
                          For Loop
      For loop is very good choice if you know how many times a task wants to execute.
      Basic syntax:
                 for(initialization;expression;update)
                 {
                      //statements
                  }
      Example:
                   public class ForLoop
                  {
                  public static void main(String args [])
                 {
                       for(int x=1;x<10;x++)
                       {
                         System.out.println(x);
                       }
                  }
                   }

       You can get the answer like this....


                                 While loop
   Basic syntax: 
              while(expression)
              {
                  //statements
               }
    Example:
              public class WhileLoop
              {
                   public static void main(String args [])
                  {
                         int x=1;
 while(x<10)
 {
                        System.out.println(x);
 x++;
 }
                   }
                }

       You can get the answer like this..        



                    Do....While  loop
       Do while loop is similar to the while loop. The different thing is in while loop before                    executing the code check  the condition. But in do while loop after executing the code                check  the condition.

       Basic Syntax:
        do
         {
               //statements
          }
         while(expression);

         Example:
          
                public class DoWhileLoop
                {
                   public static void main(String args [])
                  {
                     int x=1;
                 do
                 {
                    System.out.println(x);
                    x++;
                  }
while(x<10);
                   }
                   }

             You can get the answer like this..   
         

Comments

Popular posts from this blog

GL_QUAD_STRIP Example In OpenGl

GL_TRIANGLE_FAN Example In OpenGl