Skip to content

Commit 0906eb2

Browse files
committed
Theoritical explanation of break and default keywords added
1 parent 1cc498e commit 0906eb2

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

lessons/io-conditionals.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,15 @@ Switch statements are used to execute different code depending on the value of a
126126
break;
127127
}
128128
```
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.
129+
Above is the basic syntax of switch statements. This is how it works:
130+
- The switch expression is evaluated `once`.
131+
- The value of the expression is `compared` with the values of each case.
132+
- If there is a `match`, the associated block of code is executed.
133+
- The `break` and `default` keywords are optional.
130134

131-
![java-switch-statement](./images/java-switch-statement.png)
135+
Now, let us see a diagramtic workflow of the Java Switch statement for a better understanding.
132136

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.
137+
![java-switch-statement](./images/java-switch-statement.png)
141138

142139
Let us see an example to understand it better.
143140

@@ -189,6 +186,13 @@ Let us see an example to understand it better.
189186

190187
Output : Vowel
191188
```
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:
192196

193197
Now let us see another example where there are no break statements present:
194198

@@ -217,6 +221,14 @@ Now let us see another example where there are no break statements present:
217221
```
218222
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.
219223

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.
220232

221233
#### Java Nested Switch Statements
222234
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
282294
}
283295

284296
Output : Data Communication and Networks, MultiMedia
285-
```
297+
```
298+

0 commit comments

Comments
 (0)