Page 177 - CA_Blue( J )_Class10
P. 177

r=n%10;
                           System.out.println(r);
                           n=n/10;
                       }
                   Ans.   int n, r;
                       for (n=456; n>0; n=n/10)
                       {
                       r=n%10;
                       System.out.println(r);
                       }
                    15.  Analysis the program and find how many times the loop will execute. Also write the output.
                       class test
                       {    public static void main ()
                           {   int i=100;
                               do
                               { if(i<0)
                                   {  break;            }
                                   System.out.println(i);
                                   i-=25;
                               }while(true);
                           }
                       }
                   Ans.  The loop will execute for 5 times
                        Output:
                        100
                        75
                        50
                        25
                       0
                    16.  What is the output of the loop?
                       int a=10,b=20;
                       do
                       {
                       a++;
                       b+=a++;
                       System.out.println(a+ " , " +b);
                       }while(a<=20);
                   Ans.  12 , 31
                        14 , 44
                        16 , 59
                        18 , 76
                        20 , 95
                        22 , 116
                    17.  Write the difference between for loop and while loop?
                   Ans.                   for Loop                                       while Loop
                       In for loop, initialization, test condition and increment or  In  a  while  loop,  initialization,  test  condition  and  increment  or
                       decrement are in the same line.              decrement are written in separate lines.
                       This loop executes a set of statements repeatedly for a  This loop is used to execute a set of statements repeatedly if the
                       fixed number of times.                       number of iterations is not known.
                    18.  Write one similarity and one difference between the while loop and do-while loop.
                   Ans.  The similarity between the while and do-while loop is that both the loops are used when the number of iterations is not known.
                         The difference between the while and do-in the while loop, is that while loop if the test condition is false, then the loop will not
                       execute.
                       In the do-while loop, the loop will execute at least once, even if the test condition returns false.



                                                                                                                       175
                                                                                              Iterative constructs in Java  175
   172   173   174   175   176   177   178   179   180   181   182