Page 200 - CA_Blue( J )_Class9
P. 200

In a while loop, the condition is checked before the execution of the loop. Hence, we can call it an "Entry Controlled
                  Loop”.
                  Example of for loop and while loop working as an Entry Control loop:

                     For Loop                                         While Loop

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

                  The above programs will only print the line "Entry control loop”. This is because while entering the condition is
                  checked and it is false.

                  9.2.4 The do-while Loop
                  Java do-while loop is used to execute a set of statements repeatedly if the            Start
                  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.                                                                    Body of do...while loop
                  The syntax of the do-while loop is:
                      Initialization;
                                                                                            true     Conditional
                      do
                                                                                                      Expression
                      {
                                                                                                            false
                         // job performed by the body of the loop
                         increment or decrement;                                                         Stop
                      } while (condition for testing);


                   Program 4      Print sum of first 5 natural numbers.

                     1     class dowhile_sum
                     2     {

                     3         public static void main()
                     4         {

                     5             int i=1,s=0;
                     6             do

                     7             {
                     8                 s=s+i;

                     9                 i++;
                    10             }

                    11             while(i<=5);



                   198    Touchpad Computer Applications-IX
   195   196   197   198   199   200   201   202   203   204   205