-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwitch_Case.java
80 lines (68 loc) · 2.41 KB
/
Switch_Case.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class Switch_Case
{
/*
The switch statement allows us to execute a block of code among many alternatives.
The syntax of the switch statement in Java is:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
...
...
default:
// default statements
}
How does the switch-case statement work?
The expression is evaluated once and compared with the values of each case.
If expression matches with value1, the code of case value1 are executed.
Similarly, the code of case value2 is executed if expression matches with value2.
If there is no match, the code of the default case is executed.
Note: The working of the switch-case statement is similar to the Java if...else...if ladder.
However, the syntax of the switch statement is cleaner and much easier to read and write.
*/
public static void main(String[] args)
{
int number = 5; // number
String language;
//switch statement for choosing language
switch(number)
{
case 1:
language = "Java";
break;
case 2:
language = "C";
break;
case 3:
language = "C++";
break;
case 4:
language = "C#";
break;
case 5:
language = "Python";
break;
case 6:
language = "Scala";
break;
//The break statement is used to terminate the switch-case statement.
// If break is not used, all the cases after the matching case are also executed
default: //default case
language = "You have enter Wrong input";
break;
//Here, the value of expression doesn't match with any of the cases.
//Hence, the code inside the default case is executed
}
System.out.println("Chose the language : " + language);
}
}
/*
* Rules for Switch Case
* - we cannot write boolean, long, float, double as input for switch cases there should be no duplicate labels
* - break statements are not mandatory
* - default statement can be written anywhere inside the switch-case statement block
* - break statement will be executed only when the action is performed.
*/