Page 202 - CA_Blue( J )_Class9
P. 202

9.3 DIFFERENT FORMS OF LOOPS

                                                            FORMS OF LOOP




                                                      Null Loop/       Extra Expression/
                                 Infinite Loop                                               Finite Loop
                                                     Delay Loop          Statements
                  Let us study in detail:

                  9.3.1 Infinite loop
                  These types of loops repeats infinitely as the test condition is always true. It can be created by any of the loops.
                     Using for loop:                                  Using while loop:

                         class infinite_for                               class infinite_while
                          {    public static void main()                   {    public static void main()
                               {  for(int i=1; i>=1; i++)                     {  int i=-10;
                                 {                                                      while(true)
                            System.out.println(i); }                             {     System.out.println(i);
                                }                                                 i=i-2;
                         }                                                   }
                     Another example of for loop:                                 }
                                                                          }
                         for( ; ; )
                         { }


                                              Using do-while loop:
                                                 class infinite_dowhile
                                                  {    public static void main()
                                                     {         int i=1;
                                                         do
                                                         {     System.out.println(i);
                                                               i=i+2;
                                                  } while(i>=1);
                                                         }
                                                 }


                  9.3.2 Null Loop/Delay Loop
                  These types of loops have no code in the body of the loop. They are also known as delay loops as they delay the
                  execution of the program.


                     1. Using for loop:                               2. Using while loop:
                         class null_for                                   class null_while
                         { public static void main()                       {    public static void main()
                             {   int i;                                       {       int i=-10;
                                 for(i=1; i<=50; i=i+1);                              while(i<=-1)
                                 {   }                                            {   }
                             }                                                    }
                         }                                                }





                   200    Touchpad Computer Applications-IX
   197   198   199   200   201   202   203   204   205   206   207