Page 203 - CA_Blue( J )_Class10
P. 203

You will get the following output:











                 Dry Run of the above program:

                      i          n         t>0       r=t%10        s=s+(r*r*r)        t=t/10              Output
                      1         153       153>0         3          0+3*3*3=27        153/10=15
                                           15>0         5         27+5*5*5=152        15/10=1
                                                                                                           153 is
                                           1>0          1        152+1*1*1=153        1/10=0
                                                                                                     Armstrong Number
                      2         125       125>0         5         0+5*5*5=125        125/10=12
                                           12>0         2        125+2*2*2=133        12/10=1
                                                                                                         125 is not
                                           1>0          1        133+1*1*1=134        1/10=0
                                                                                                     Armstrong Number

                     9.4 USING THE BREAK STATEMENT IN NESTED LOOP
                 Since the break statement is used for exiting the loop, placing the break statement in the correct position will allow the
                 control to come out of the inner loop or outer loop.

                 9.4.1 Using the break Statement in Outer Loop
                 To use the break statement in the outer loop, the break statement should be written in the body of the outer loop as
                 shown in the following code:

                   1  class outerexit
                   2  {

                   3      public static void main()
                   4      {   int i ,j;

                   5          for(i=1;i<=5;i++)

                   6          {   for(j=1;j<=i;j++)
                   7              {         System.out.print(j+":");  }
                   8              if(i%2==0)

                   9                  break;

                  10          }
                  11      }
                  12  }


                 Output:  1: 1: 2:
                 Explanation: When the value of “i” is an even number, the condition becomes true and the break statement executes
                 and the control comes out of the outer loop.





                                                                                                                       201
                                                                                                         Nested Loop   201
   198   199   200   201   202   203   204   205   206   207   208