Page 240 - CA_Blue( J )_Class9
P. 240

Dry Run of the above program:

                                  i                      j                    f=f*j                Output
                                  1
                                  2                     1                      1
                                                        2                      2                     2
                                  3
                                  4                     1                      1
                                                        2                      2

                                                        3                      6
                                                        4                     24                     24
                                  5




                       10.2 NESTED WHILE LOOP
                  When a “while loop” is used within a “while loop”, is called a nested while loop. Let us take a look at the syntax of
                  a nested while loop.
                      initialization;                               //initialization of outer loop
                      while(condition)                              //condition of outer loop
                      {
                         initialization;                            //initialization of inner loop
                          while(condition)                          //condition of inner loop
                         {
                         // statement of inner loop
                             increment/decrement;                   //increment or decrement of inner loop
                          }
                         // statement of outer loop
                             increment/decrement;                   //increment or decrement of outer loop
                      }
                  Let us take an example:

                   Program 2      Write a program to print all the prime numbers from 3 and 6.


                     1     class nested_while
                     2     {   public static void main()

                     3         {   int i,j,c;
                     4             System.out.println("Printing all the prime numbers from 3 to 6");

                     5             i=3;
                     6             while(i<=6)
                     7             {

                     8                 j=1; c=0;

                     9                 while(j<=i)
                    10                 {   if(i%j==0)



                   238    Touchpad Computer Applications-IX
   235   236   237   238   239   240   241   242   243   244   245