switch..case in java
switch..case is a control statement, it is used to choose and execute statements based on different values of a single variable. Syntax of switch..case:
switch(option){case option_1://statementsbreak;case option_2://statementsbreak;............default://statementsbreak;}
In switch..case option to be evaluated can be String, int etc.. default case is not a compulsory part of the switch case. User can put default or not, it depends on the requirements of the program.
Examples
In the given program below, user is asked to enter an integer; that integer value is stored in int variable named value; that value is passed to switch; three cases are written in the switch: case 1, case 9 and case 5 and a default case. The entered value is compared with the given cases; if the match is found, statements corresponding to the case is executed otherwise statement corresponding to default case is executed.
import java.util.*;class switch_case_1{public static void main(String[] args){int value;Scanner sc = new Scanner(System.in);System.out.print("Enter a number: ");value = sc.nextInt();switch(value){case 1: System.out.println("The value entered is 1");break;case 9: System.out.println("The value entered is 9");break;case 5: System.out.println("The value entered is 5");break;default: System.out.println("The default case and value entered is "+value);break;}}}OutputFirst RunEnter a number: 5The value entered is 5Second RunEnter a number: 45The default case and value entered is 45
In the given program user is asked to enter a integer value; that integer value is stored in variable named option; variable option is passed to switch; three different cases are written with values 17, 33 and 77 and a default case is also written. The user provided value is compared with given cases, if it matches with any one of the given cases, then statements within that case will be executed; otherwise, statements within the default case will be executed; each case is paired with break statement; break statement stops the current case and also stops statement in other case from executing.
import java.util.*;class switch_case_2{public static void main(String[] opp){int option;Scanner sc = new Scanner(System.in);System.out.print("Enter a number: ");option = sc.nextInt();switch(option){case 17:System.out.println("Displaying all numbers divisible by 17 between 101 and 200 inclusive.");int i = 101;do{if(i%17 == 0)System.out.print(i+", ");i++;}while(i<=200);break;case 33:System.out.println("Displaying product of 33.");for(i=1;i<=10;i++){System.out.println(i+ " * 33 = "+ (i*33));}break;case 77:System.out.print("Enter a word: ");String x = sc.next();if(x.equals("Kanchanjunga")){System.out.println("You have entered name of Mountain.");}else {System.out.println("You have entered :"+x);}break;default: System.out.println("No task to be done.");break;}}}Output:First Run:Enter a number: 17Displaying all numbers divisible by 17 between 101 and 200 inclusive.102, 119, 136, 153, 170, 187,Second Run:Enter a number: 33Displaying product of 33.1 * 33 = 332 * 33 = 663 * 33 = 994 * 33 = 1325 * 33 = 1656 * 33 = 1987 * 33 = 2318 * 33 = 2649 * 33 = 29710 * 33 = 330Third Run:Enter a number: 77Enter a word: KanchanjungaYou have entered name of Mountain.*/
In the given example, user is asked to provide string value; that string value is passed to switch with different cases, if the passed string matches with any of the case, then statements corresponding to that case is executed otherwise default case is executed.
class switch_case_3{public static void main(String[] opp){String option;Scanner sc = new Scanner(System.in);System.out.print("Enter a text: ");option = sc.next();switch(option){case "first": System.out.println("here work related to first case will be done");break;case "second": System.out.println("here work related to second case will be done");break;case "sixth": System.out.println("here work related to sixth case will be done");break;default: System.out.println("here work related to other cases will be done");break;}}}Output:First Run:Enter a text: firsthere work related to first case will be doneSecond Run:Enter a text: sixthhere work related to sixth case will be doneThird Run:Enter a text: fourthhere work related to other cases will be done
if statement in java
String Handling in Java