Page 158 - CA_Blue( J )_Class10
P. 158

// job performed by the body of the loop
                   increment or decrement;
               }
                                                                 Start



                                                              Conditional   false
                                                              Expression


                                                                     true


                                                            while Loop Body






                                                                  End
                                                         Flowchart of while loop

              Example: Extract the digits from a number, say 2546.

               int n = 2546, r;
                                                                                      Dry Run & Output
               while(n > 0)
                                                                         n      r=n%10     System.out.println(r)   n=n/10
               {
                                                                         2546>0       6        6             254
                   r = n % 10;
                                                                         254>0       4         4             25
                   System.out.println(r);
                   n = n / 10;                                           25>0        5         5             2
               }                                                         2>0         2         2             0
              As the while loop checks the condition before executing the   0>0
              body of loop, so it is entry controlled loop. Example of the for
              loop and while loop working as the entry controlled loops:


                                                                       The while Loop
                    The for Loop                                       int i=1;
                    int i;                                             while(i<=0)
                    for(i=1;i<=0;i++)                                  {
                    {                                                      System.out.println("Will not print");
                         System.out.println("Will not print");              i++;
                    }                                                  }
                    System.out.println("Entry Controlled loop");
                                                                       System.out.println("Entry Controlled Loop");

              The above programs will only print the line "Entry controlled loop".


                   8.5 THE DO-WHILE LOOP
              The do-while loop is used to execute a set of statements repeatedly if the number of iterations is not known. However,
              the do-while loop will execute at least once even if the test condition is false. This is because the condition lies at the
              end of the loop. The syntax of the do-while loop is:
               initialization;
               do
               {



                156156  Touchpad Computer Applications-X
   153   154   155   156   157   158   159   160   161   162   163