You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -109,7 +109,7 @@ Let's go through the code line by line.
109
109
110
110
Switch statements are used to execute different code depending on the value of a variable. We use Switch statements for the condition based problem solving. Switch statements are used when we have multiple conditions in a single if statement. It is just a substitute for multiple if-else statements. In this a variable is compared to multiple cases one by one and when the case is matched then the code block under that case will be executed.
111
111
112
-
#### Semantics
112
+
#### Syntax
113
113
114
114
```java
115
115
// syntax
@@ -126,29 +126,160 @@ Switch statements are used to execute different code depending on the value of a
126
126
break;
127
127
}
128
128
```
129
+
Above is the basic syntax of switch statements. Now, let us see a diagramtic workflow of the Java Switch statement for a better understanding.
- There can be 1 or N number of `case values` for a switch expression.
135
+
- The `case values` must be `literal` or `constant`. It doesn't allow variables.
136
+
- The `case values` must be `unique`. In case of duplicate value, it renders compile-time error.
137
+
- The Java `switch expression` must be of `byte, short, int, long` (with its Wrapper type), `enums and string`.
138
+
- Each case statement can have a `break` statement which is `optional`. When control reaches to the break statement, it jumps the control after the switch expression. If a break statement is `not found`, it executes the next case.
139
+
- The case value can have a `default` label which is `optional`.
140
+
- The Java switch statement is `fall-through`. It means it executes all statements after the first match if a break statement is NOT present.
129
141
130
-
Above is the basic syntax of switch statements. Let's see some examples to understand it better.
142
+
Let us see an example to understand it better.
131
143
132
144
```java
133
-
// print hello world in different languages using switch statement
145
+
// Program to check Vowel or Consonant:
146
+
// It is not case-sensitive.
134
147
135
148
public class <ClassName> {
136
149
publicstaticvoid main(String[] args){
137
-
int a =10;
138
-
switch(a){
139
-
case1:
140
-
System.out.println("Hello");
141
-
break;
142
-
case2:
143
-
System.out.println("Namastey");
144
-
break;
145
-
case3:
146
-
System.out.println("Hola");
147
-
break;
148
-
default:
149
-
System.out.println("learning more");
150
+
char ch='O';
151
+
switch(ch)
152
+
{
153
+
case'a':
154
+
System.out.println("Vowel");
155
+
break;
156
+
case'e':
157
+
System.out.println("Vowel");
158
+
break;
159
+
case'i':
160
+
System.out.println("Vowel");
161
+
break;
162
+
case'o':
163
+
System.out.println("Vowel");
164
+
break;
165
+
case'u':
166
+
System.out.println("Vowel");
167
+
break;
168
+
case'A':
169
+
System.out.println("Vowel");
170
+
break;
171
+
case'E':
172
+
System.out.println("Vowel");
173
+
break;
174
+
case'I':
175
+
System.out.println("Vowel");
176
+
break;
177
+
case'O':
178
+
System.out.println("Vowel");
179
+
break;
180
+
case'U':
181
+
System.out.println("Vowel");
182
+
break;
183
+
default:
184
+
System.out.println("Consonant");
150
185
break;
151
186
}
152
187
}
153
188
}
189
+
190
+
Output:Vowel
191
+
```
192
+
193
+
Now let us see another example where there are no break statements present:
194
+
195
+
```java
196
+
//switch cases without break statements
197
+
198
+
publicclassSwitchExample2 {
199
+
publicstaticvoidmain(String[] args) {
200
+
int num =20;
201
+
switch(num){
202
+
case10:
203
+
System.out.println("10");
204
+
case20:
205
+
System.out.println("20");
206
+
case30:
207
+
System.out.println("30");
208
+
default:
209
+
System.out.println("The number is not 10, 20 or 30");
210
+
}
211
+
}
212
+
}
213
+
214
+
Output:20
215
+
30
216
+
The number is not 10, 20 or 30
154
217
```
218
+
Remember that the switch statement is `fall-through`. That's why all the statements got executed after the first match because the break statement is NOT present.
219
+
220
+
221
+
#### Java Nested Switch Statements
222
+
We can use switch statement inside other switch statement in Java. It is known as nested switch statement.
223
+
224
+
```java
225
+
//Java Program to demonstrate the use of Java Nested Switch
226
+
227
+
publicclassNestedSwitchExample {
228
+
publicstaticvoidmain(Stringargs[])
229
+
{
230
+
//C - CSE, E - ECE, M - Mechanical
231
+
char branch ='C';
232
+
int collegeYear =4;
233
+
switch(collegeYear)
234
+
{
235
+
case1:
236
+
System.out.println("English, Maths, Science");
237
+
break;
238
+
case2:
239
+
switch( branch )
240
+
{
241
+
case'C':
242
+
System.out.println("Operating System, Java, Data Structure");
0 commit comments