Page 187 - ComputerScience_Class_11
P. 187
Write a Java program to calculate the cost of a given number of pens based on the price of
Program 1
35 pens.
1 import java.util.*;
2 class sequential_flow
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7 int n, pens=35;
8 double cost1, cost2;
9 System.out.print("Enter cost of 35 pens: ");
10 cost1=sc.nextDouble();
11 System.out.print("Enter number of pens: ");
12 n=sc.nextInt();
13 cost2=cost1/pens*n;
14 System.out.println("Cost of " +n+ " pens: " +Math.round(cost2));
15 }
16 }
The output of the preceding program is as follows:
BlueJ: Terminal Window - Java
Options
Enter the cost of 35 pens: 500
Enter the number of pens: 100
Cost of 100 pens: 1429
8.4.2 Conditional Statements
These statements follow the conditional flow of control. In a conditional flow of control, the code is directed in a
specific direction according to the need of the program. The execution of statements depends on the result of the test
condition.
Depending on the true or false output, the course of action is decided. So, it is also a decision construct as it helps in
making a decision about which set of statements is to be executed.
Statement 1
True False
Condition
?
Statement 2 Statement 3
In the above flow chart, if the condition is satisfied, then Statement 2 will be executed else, Statement 3 will be executed.
Statements and Scope 185

