☞Following is the syntax of switch.
switch(choice){ case constant1: block-1 break; case constant2: block-2 break; case constant3: block-3 break; …………. default: default-block }
☞When you have multiple options and every option has some value associated for identification of the case. The choice in switch parenthesis is then looking for the correct match in among the available case statements in the body of switch.
☞So control jumps from switch to appropriate matching case and then executes all the lines written in that case. In the absence of break statement, control keeps on executing other cases down the line till the end of switch body.
☞break keyword can be used to throw control out of the switch body.
☞case keyword is followed by a constant value, which must be unique integer only. It can never be a variable or expression. It can never be a real constant. It can be a character or string.
☞You can write any number of cases in switch body.
☞default keyword is used to handle the default case, that is when no case matches.
☞It is not mandatory to write default keyword at the end of all cases.
import java.util.*; class SwitchExample1{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter your choice"); int choice=sc.nextInt(); switch(choice){ case 1:System.out.println("Hello"); case 2:System.out.println("Bye"); case 3:System.out.println("Good"); default:System.out.println("How are you?"); } } }
Enter your choice 2 Bye Good bye How are you?
import java.util.*; class SwitchExample2{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); System.out.println("Enter your choice"); char choice=sc.next().charAt(0); switch(choice){ case 'A':System.out.println("Hello"); break; case '#':System.out.println("Bye"); break; case 'b':System.out.println("Good"); break; default:System.out.println("How are you?"); } } }
Enter your choice # Bye
//Menu-driven program
import java.util.*;
class SwitchExample3{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a,b,result,choice;
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("Enter your choice");
choice=sc.nextInt();
switch(choice){
case 1:
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a+b;
System.out.println("The addition is "+result);
break;
case 2:
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a-b;
System.out.println("The difference is "+result);
break;
case 3:
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a*b;
System.out.println("The product is "+result);
break;
case 4:
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a/b;
System.out.println("The division is "+result);
break;
default: System.out.println("Invalid entry");
}
}
}
1. Addition 2. Subtraction 3. Multiplication 4. Division Enter your choice 3 Enter two numbers 12 10 The product is 120
//Menu-driven program
import java.util.*;
class SwitchExample4{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int a,b,result;
System.out.println("Enter '+' for Addition");
System.out.println("Enter '-' for Subtraction");
System.out.println("Enter '*' for Multiplication");
System.out.println("Enter '/' for Division");
System.out.println("Enter your choice");
char choice=sc.next().charAt(0);
switch(choice){
case '+':
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a+b;
System.out.println("The addition is "+result);
break;
case '-':
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a-b;
System.out.println("The difference is "+result);
break;
case '*':
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a*b;
System.out.println("The product is "+result);
break;
case '/':
System.out.println("Enter two numbers");
a=sc.nextInt();
b=sc.nextInt();
result=a/b;
System.out.println("The division is "+result);
break;
default: System.out.println("Invalid entry");
}
}
}
Enter '+' for Addition Enter '-' for Subtraction Enter '*' for Multiplication Enter '/' for Division Enter your choice * Enter two numbers 12 10 The product is 120