switch..case in java

switch..case in java

exploreJava logo

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:
//statements
break;
case option_2:
//statements
break;
....
....
....
default:
//statements
break;
}

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;
}
}
}

Output
First Run
Enter a number: 5
The value entered is 5
Second Run
Enter a number: 45
The 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: 17
Displaying all numbers divisible by 17 between 101 and 200 inclusive.
102, 119, 136, 153, 170, 187,

Second Run:
Enter a number: 33
Displaying product of 33.
1 * 33 = 33
2 * 33 = 66
3 * 33 = 99
4 * 33 = 132
5 * 33 = 165
6 * 33 = 198
7 * 33 = 231
8 * 33 = 264
9 * 33 = 297
10 * 33 = 330

Third Run:
Enter a number: 77
Enter a word: Kanchanjunga
You 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: first
here work related to first case will be done

Second Run:
Enter a text: sixth
here work related to sixth case will be done

Third Run:
Enter a text: fourth
here work related to other cases will be done


if statement in java

String Handling in Java

Post a Comment

Previous Post Next Post