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
Now, let us see a diagramtic workflow of the Java Switch statement for a better understanding.
132
136
133
-
#### Points to Remember
134
-
- 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.
@@ -189,6 +186,13 @@ Let us see an example to understand it better.
189
186
190
187
Output:Vowel
191
188
```
189
+
#### The 'break' Keyword
190
+
- When Java 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:
192
196
193
197
Now let us see another example where there are no break statements present:
194
198
@@ -217,6 +221,14 @@ Now let us see another example where there are no break statements present:
217
221
```
218
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.
219
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.
220
232
221
233
#### Java Nested Switch Statements
222
234
We can use switch statement inside other switch statement in Java. It is known as nested switch statement.
@@ -282,4 +294,5 @@ We can use switch statement inside other switch statement in Java. It is known a
0 commit comments