Quick Reach
The switch case statement
In the real World, where there are more than one options we have to decide. One of the methods that we use in the Java programming is the switch case statement – to take the decisions based on different situations.
The decision-making statements in Java
Two types of decision-making statements provided by Java are:
- The If statement
- The Switch statement
A switch-case example
This chapter will explain the switch case statement only. For the if statement go to java if statement chapter.
The switch case in java
The switch case statement is used when you have many options and you have to execute one of those. The Java switch statement is useful when you have long blocks of the if, else if code.
In the switch case statement, a variable is tested against a list of values. Where each value is called a case. Following is an example to explain how the switch case statement works, first look at its syntax.
Syntax of the Java switch statement
The general syntax of using the switch case statement is:
switch (condition) {
case value :
// Code to be executed if condition is true
brerak; //This is optional, The execution will stop here for switch statement
case value :
// Code to be executed if condition is true
brerak; //This is optional, The execution will stop here for switch statement
case value :
// Code to be executed if condition is true
break; //This is optional, The execution will stop here for switch statement
default: // This is optional and works like else in if statement
// Code to be executed if condition is true
}
Main points about Switch-Case statement
A few points to be noted while using the switch with case statement:
- The data type of the variables used in the switch statement can be the byte, char, short and int.
- The value used in the java case statement must be same data type as in the switch.
- The break statement is used to terminate the execution within the switch block or it will keep on executing the other cases.
- The default block is used, which is optional. In a situation where none of the Java case statement value is matched, the default part will be executed and after that execution will move to the next statement outside of Java switch block.
- You can use any number of cases in the switch java statement.
Example of using switch case statement
The following example uses the Java switch statement where a variable daynumber is used. If the daynumber value is 1 then it will display Mon, 2 for Tue and so on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
public class switch_ex {
public static void main(String []args) {
byte daynumber;
daynumber=3;
switch (daynumber){
case 1 :{
System.out.println(“Mon”);
break;
}
case 2 :{
System.out.println(“Tue”);
break;
}
case 3 :{
System.out.println(“Wed”);
break;
}
case 4 :{
System.out.println(“Thu”);
break;
}
case 5 :{
System.out.println(“Fri”);
break;
}
}
}
}
|
Experience this online
The output of the above code will be:
Wed
In the example, you can see the variable daynumber is used in the switch statement. A value is assigned to that variable that you can assign dynamically, depending on the scenario. After that, five cases are used with different values to check with the break statement.
Also see – The for loop | While loop of Java
Leave A Comment?