Page 160 - CA_Blue( J )_Class10
P. 160

1  class infinite_for
                 2  {

                 3      public static void main()
                 4      {

                 5          for(int i = 1; i >= 1; i++)
                 6          {

                 7              System.out.println(i);
                 8          }

                 9      }
                10  }

              Another example of infinite for loop is:

              for( ; ; )
              { }
              Using infinite while loop:

                 1  class infinite_while
                 2  {
                 3          public static void main()

                 4          {
                 5              int a=-10;

                 6              while(true)
                 7              {

                 8                  System.out.println(a);
                 9                  a=a-2;

                10              }
                11          }

                12  }

              The test condition in while(true) is never false, so the loop executes for infinite number of times. Using do-while loop:
                 1  class infinite_dowhile

                 2  {
                 3      public static void main()

                 4      {
                 5          int a = 1;

                 6          do






                158158  Touchpad Computer Applications-X
   155   156   157   158   159   160   161   162   163   164   165