Page 169 - Cs_withBlue_J_C11_Flipbook
P. 169
import java.util.*;
class sequential_flow
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int n, pens=35;
double cost1, cost2;
System.out.print("Enter cost of 35 pens: ");
cost1=sc.nextDouble();
System.out.print("Enter number of pens: ");
n=sc.nextInt();
cost2=cost1/pens*n;
System.out.println("Cost of " +n+ " pens: " +Math.round(cost2));
}
}
The output of the preceding program is as follows:
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
Yes No
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.
To handle such flow of control, “if” and “switch case” statements are used. The “if” statement can be used in various
ways as illustrated below.
Conditional Statement
if statement Switch Case
if-else statement
if and only if statement
if else if statement
nested if statement
167
Statements and Scope 167

