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,173 @@ 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. This is how it works:
130
+
- The switch expression is only evaluated `once`.
131
+
- The value of the expression is `compared` with the values of each case value.
132
+
- If there is a `match`, ie. the case value and the switch expression are same, the associated block of code with the case value is executed.
133
+
- The `break` and `default` keywords are optional and will be explained ahead.
134
+
135
+
Now, let us see a diagramtic workflow of the Java Switch statement for a better understanding.
129
136
130
-
Above is the basic syntax of switch statements. Let's see some examples to understand it better.
// print hello world in different languages using switch statement
142
+
// Program to check Vowel or Consonant:
143
+
// It is not case-sensitive.
134
144
135
145
public class <ClassName> {
136
146
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");
147
+
char ch='O';
148
+
switch(ch)
149
+
{
150
+
case'a':
151
+
System.out.println("Vowel");
152
+
break;
153
+
case'e':
154
+
System.out.println("Vowel");
155
+
break;
156
+
case'i':
157
+
System.out.println("Vowel");
158
+
break;
159
+
case'o':
160
+
System.out.println("Vowel");
161
+
break;
162
+
case'u':
163
+
System.out.println("Vowel");
164
+
break;
165
+
case'A':
166
+
System.out.println("Vowel");
167
+
break;
168
+
case'E':
169
+
System.out.println("Vowel");
170
+
break;
171
+
case'I':
172
+
System.out.println("Vowel");
173
+
break;
174
+
case'O':
175
+
System.out.println("Vowel");
176
+
break;
177
+
case'U':
178
+
System.out.println("Vowel");
179
+
break;
180
+
default:
181
+
System.out.println("Consonant");
150
182
break;
151
183
}
152
184
}
153
185
}
186
+
187
+
Output:Vowel
154
188
```
189
+
#### The 'break' Keyword
190
+
- When the Java compiler reaches a `break` keyword, it breaks out of the switch block.
191
+
- This will stop the execution of more code and case testing inside the block.
192
+
- Basically it means that When a match is found, and the job is done, there is no need for more testing. Hence we break out of the switch statement.
193
+
194
+
#### The 'default' Keyword
195
+
- The `default` keyword specifies some code to run if there is no case match:
196
+
197
+
Now let us see another example where there are no break statements present:
198
+
199
+
```java
200
+
//switch cases without break statements
201
+
202
+
publicclassSwitchExample2 {
203
+
publicstaticvoidmain(String[] args) {
204
+
int num =20;
205
+
switch(num){
206
+
case10:
207
+
System.out.println("10");
208
+
case20:
209
+
System.out.println("20");
210
+
case30:
211
+
System.out.println("30");
212
+
default:
213
+
System.out.println("The number is not 10, 20 or 30");
214
+
}
215
+
}
216
+
}
217
+
218
+
Output:20
219
+
30
220
+
The number is not 10, 20 or 30
221
+
```
222
+
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.
223
+
224
+
#### Points to Remember
225
+
- There can be 1 or N number of `case values` for a switch expression.
226
+
- The `case values` must be `literal` or `constant`. It doesn't allow variables.
227
+
- The `case values` must be `unique`. In case of duplicate value, it renders compile-time error.
228
+
- The Java `switch expression` must be of `byte, short, int, long` (with its Wrapper type), `enums and string`.
229
+
- 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.
230
+
- The case value can have a `default` label which is `optional`.
231
+
- The Java switch statement is `fall-through`. It means it executes all statements after the first match if a break statement is NOT present.
232
+
233
+
#### Java Nested Switch Statements
234
+
We can use switch statement inside other switch statement in Java. It is known as nested switch statement.
235
+
236
+
```java
237
+
//Java Program to demonstrate the use of Java Nested Switch
238
+
239
+
publicclassNestedSwitchExample {
240
+
publicstaticvoidmain(Stringargs[])
241
+
{
242
+
//C - CSE, E - ECE, M - Mechanical
243
+
char branch ='C';
244
+
int collegeYear =4;
245
+
switch(collegeYear)
246
+
{
247
+
case1:
248
+
System.out.println("English, Maths, Science");
249
+
break;
250
+
case2:
251
+
switch( branch )
252
+
{
253
+
case'C':
254
+
System.out.println("Operating System, Java, Data Structure");
0 commit comments