Page 117 - CA_Blue( J )_Class10
P. 117

4      public static void main ()
                   5      {

                   6          Scanner sc= new Scanner (System.in);
                   7          double si,p,r,t;

                   8          System.out.print("Enter the principal amount: ");
                   9          p=sc.nextDouble();

                  10          System.out.print("Enter the rate: ");
                  11          r=sc.nextDouble();

                  12          System.out.print("Enter the number of years: ");
                  13          t=sc.nextDouble();

                  14          si=(p*r*t)/100.0;
                  15          System.out.println("Simple Interest: "+si);
                  16      }

                  17  }

                 You will get the following output:














                 The preceding program executes in a top-down approach which means the program execution starts from the very
                 first line and ends with the last line of the program in a sequential manner.


                 7.1.2 Conditional Flow of Control
                 Sometimes, there may be some situations wherein you need to change the flow of control based on some conditions.
                 For example:
                 •  Suppose, we need to solve a calculation that will find the income tax from the taxable income of a person. If the
                   person gets more than 25000/-, then the tax he has to pay is 10% else he has to pay 8%.
                 •  If a number is greater than zero then the number is a positive number, if the number is less than zero then the
                   number is negative else it is zero.
                 In the above cases, the program control executes the required statements depending on the condition stated. This
                 type of control flow is known as the conditional flow of control. To implement the conditional flow of control, Java
                 provides conditional statements.

                 7.1.3 Multi-Branching Flow of Control
                 Sometimes, a situation arises where we need to choose a single action based on a condition and execute the related
                 block of statements. This type of flow of control is known as multi-branching flow of control. Java provides a switch
                 statement to use multi-branching flow of control.





                                                                                                                       115
                                                                                           Conditional Constructs in Java  115
   112   113   114   115   116   117   118   119   120   121   122