Page 168 - CA_Blue( J )_Class10
P. 168

Program 7    To input a number and print the factorial of the number using do while loop.


                 1  import java.util.*;
                 2  class dowhile_loop

                 3  {
                 4      public static void main()

                 5      {
                 6          Scanner sc= new Scanner(System.in);

                 7          int i=1,n,f=1;
                 8          System.out.print("Enter a number : ");

                 9          n=sc.nextInt();
                10          do

                11          {
                12              f=f*i;

                13      i++;
                14          } while(i<=n);
                15          System.out.println("Factorial of a number : "+f);

                16      }

                17  }
              You will get the following output:

















                   8.8 JUMP STATEMENTS
              Jump statements transfer the program control to another part of the program which depends on the given condition.
              In Java, we have three jump statements break, continue and return. Let us study them in detail.

              8.8.1 The break Statement
              The break statement is used to exit from the loop. As soon as the break statement in a loop executes, the execution
              will be transferred to the next statement immediately outside the loop. This means the statements after the break
              statement in the loop body will not be executed. The break statement can be applied to all the different types of loops.
              Syntax of the break statement is:
              for (initialization; condition for testing; increment or decrement)
              {


                166166  Touchpad Computer Applications-X
   163   164   165   166   167   168   169   170   171   172   173