Page 242 - CA_Blue( J )_Class9
P. 242

4            6%4==0           c=3

                                        5            6%5==0           c=3

                                        6            6%6==0           c=4            false


                       10.3 NESTED DO-WHILE LOOP
                  When a “do-while loop” is used within a “do-while loop”, is called a nested do-while loop. Let us take a look at the
                  syntax of a nested do-while loop:


                      initialization;                               //initialization of outer do loop
                       do {                                         //outer do loop
                         initialization;                            //initialization of inner do loop
                          do {                                      //outer do loop
                              // statement of inner loop
                              increment/decrement;                  //increment or decrement of outer do loop
                          } while(condition);                       //condition of inner do loop
                          // statement of outer loop

                         increment/decrement;                       //increment or decrement of outer do loop
                      } while(condition);                           //condition of outer do loop
                  Let us take an example:


                   Program 3      Write a program to print the factors of all the numbers from 2 to 5.

                     1     class nested_dowhile
                     2     {   public static void main()

                     3         {   int i,j,c;
                     4             System.out.print("Printing all the factors numbers from 2 and 5");

                     5             i=2;
                     6             do

                     7             {
                     8                 System.out.print("\nFactors of "+i +" : ");

                     9                 j=1; c=0;
                    10                 do

                    11                 {   if(i%j==0)
                    12                     { System.out.print(j+" "); }

                    13                     j++;
                    14                 }while(j<=i);

                    15                 i++;





                   240    Touchpad Computer Applications-IX
   237   238   239   240   241   242   243   244   245   246   247