Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 99 - 2617_JSSPS_C-7
P. 99

    Program 4:  Write a program to find the amount to be paid.

                     1   public class Amount {
                     2       public static void main(String args[]) {
                     3          int unitPrice, units, amount;

                     4          unitPrice = 20;
                     5          units = 100;
                     6          amount = unitPrice * units;

                     7          System.out.println("Total amount to be paid: " + amount);
                     8       }
                     9   }

                     You will get the following output:

                     CONDITIONAL STATEMENTS

                     Conditional statements allows us to change the default flow of a program. They transfer control of
                     a program from one statement to another, depending  on the condition  provided. If the provided
                     condition returns true, a block of specified statements is executed. Conditional statements are also
                     known as decision-making statements.

                     There are four decision-making statements in Java as follows:

                        if statement
                        if … else statement

                        if … else… if statement
                        switch statement

                     Let‘s learn about these decision-making statements in Java.

                     The if Statement

                     The if statement is the most basic conditional statement in Java that allow us to test a condition before
                     executing a block of statements. If the provided conditional expression returns true, the statements
                     specified in the body of the if statement are executed.

                                                                                                   Java Programming     97

| 94 95 96 97 98 99 100 101 102 103 104 |