Page 131 - CA_Blue( J )_Class10
P. 131
7.6 DIFFERENCE BETWEEN IF STATEMENT AND SWITCH STATEMENT
if Statement switch Statement
The flow of control is bidirectional. The flow of control is multidirectional i.e. depending on
the choice.
All kinds of relational operators are used for checking. Checking is satisfied if the choice variable is matching
the case value.
Any data type can be used for checking. Only int and char data types are used for checking.
Satisfied condition returns true else returns false. Satisfied condition neither returns true nor false.
7.7 MENU DRIVEN PROGRAM USING SWITCH STATEMENT
This type of program obtains values from the end-users through a list of options provided. This list of options mentioned
is called Menu and the users are allowed to input values according to their choice.
To create a calculator that will do the following task according to user's choice:
Program 10
1. Sum 2. Product 3. Difference 4. Quotient
1 import java.util.*;
2 class calculator
3 {
4 public static void main()
5 {
6 Scanner sc= new Scanner(System.in);
7 int a,b,add,sub,pro,ch;
8 double quo;
9
10
11 System.out.println(" Calculator Menu ");
12 System.out.println("1. Sum");
13 System.out.println("2. Product");
14 System.out.println("3. Difference");
15 System.out.println("4. Quotient");
16 System.out.print("Enter your choice :");
17
18 ch=sc.next Int();
19 System.out.print("Enter 1st number : ");
20 a=sc.nextInt();
21 System.out.print("Enter 2nd number : ");
22 b=sc.nextInt();
23 switch(ch)
24 {
129
Conditional Constructs in Java 129

