Page 243 - CA_Blue( J )_Class9
P. 243

16              }while(i<=5);
                  17          }

                  18      }
                 You will get the following output:
















                 Dry Run of the above program:

                             i                        j                     if(i%j==0)          System.out.println(i)
                             2                        1                     2%1==0                      1
                                                      2                     2%2==0                      2
                             3                        1                     3%1==0                      1
                                                      2                     3%2==0
                                                      3                     3%3==0                      3
                             4                        1                     4%1==0                      1

                                                      2                     4%2==0                      2
                                                      3                     4%3==0
                                                      4                     4%4==0                      4
                             5                        1                     5%1==0                      1
                                                      2                     5%2==0
                                                      3                     5%3==0

                                                      4                     5%4==0
                                                      5                     5%5==0                      5


                     10.4 USING BREAK STATEMENT IN NESTED LOOP
                 You have already learnt that a break statement is used to exit a loop. This is done by placing the break statement in
                 a position that will allow the control to come out of the loop. The position of the break statement is very important
                 in getting the desired output, as it immediately terminates the loop when executed. The break statement will stop
                 the execution of the inner loop and transfer control to the next iteration of the outer loop. Let us take a look at an
                 example and learn how to place the break statement.

                  Program 4      Using break statement in the Outer Loop.

                   1      class outerloop_break
                   2      {

                   3          public static void main()



                                                                                                   Nested Loop   241
   238   239   240   241   242   243   244   245   246   247   248