Page 133 - CA_Blue( J )_Class10
P. 133
5 {
6 Scanner sc = new Scanner(System.in);
7 int quan;
8 double rate,amt=0.0;
9 System.out.print("Enter the rate of the item : ");
10 rate= sc.nextDouble();
11 System.out.print("Enter the quantity required : ");
12 quan=sc.nextInt();
13 if(rate<=0 || quan<=0)
14 {
15 System.out.println("Wrong Input. The program stops.");
16 System.exit(0);
17 }
18 else
19 {
20 amt = rate * quan;
21 }
22 System.out.println("Rate : "+ rate + " Quantity : "+quan);
23 System.out.println("Total Amount required to buy the item : "+ amt);
24 }
25 }
Output 1
Output 2
• For Output 1: The rate is not a positive number, so the program terminates as soon as the if statement satisfies and
the statements inside the else block do not execute.
• For Output 2: The if statement does not satisfy, so after calculation, the statements inside the else block execute.
131
Conditional Constructs in Java 131

