Skip to content

Commit 1cc498e

Browse files
committed
Improved switch statement explanation
1 parent 88bcb06 commit 1cc498e

File tree

2 files changed

+147
-16
lines changed

2 files changed

+147
-16
lines changed
9.95 KB
Loading

lessons/io-conditionals.md

Lines changed: 147 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Let's go through the code line by line.
109109

110110
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.
111111

112-
#### Semantics
112+
#### Syntax
113113

114114
```java
115115
// syntax
@@ -126,29 +126,160 @@ 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.
130+
131+
![java-switch-statement](./images/java-switch-statement.png)
132+
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.
129141

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

132144
```java
133-
// print hello world in different languages using switch statement
145+
// Program to check Vowel or Consonant:
146+
// It is not case-sensitive.
134147

135148
public class <ClassName> {
136149
public static void main(String[] args){
137-
int a = 10;
138-
switch(a){
139-
case 1:
140-
System.out.println("Hello");
141-
break;
142-
case 2:
143-
System.out.println("Namastey");
144-
break;
145-
case 3:
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");
150185
break;
151186
}
152187
}
153188
}
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+
public class SwitchExample2 {
199+
public static void main(String[] args) {
200+
int num = 20;
201+
switch(num){
202+
case 10:
203+
System.out.println("10");
204+
case 20:
205+
System.out.println("20");
206+
case 30:
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
154217
```
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+
public class NestedSwitchExample {
228+
public static void main(String args[])
229+
{
230+
//C - CSE, E - ECE, M - Mechanical
231+
char branch = 'C';
232+
int collegeYear = 4;
233+
switch(collegeYear)
234+
{
235+
case 1:
236+
System.out.println("English, Maths, Science");
237+
break;
238+
case 2:
239+
switch( branch )
240+
{
241+
case 'C':
242+
System.out.println("Operating System, Java, Data Structure");
243+
break;
244+
case 'E':
245+
System.out.println("Micro processors, Logic switching theory");
246+
break;
247+
case 'M':
248+
System.out.println("Drawing, Manufacturing Machines");
249+
break;
250+
}
251+
break;
252+
case 3:
253+
switch(branch)
254+
{
255+
case 'C':
256+
System.out.println("Computer Organization, MultiMedia");
257+
break;
258+
case 'E':
259+
System.out.println("Fundamentals of Logic Design, Microelectronics");
260+
break;
261+
case 'M':
262+
System.out.println("Internal Combustion Engines, Mechanical Vibration");
263+
break;
264+
}
265+
break;
266+
case 4:
267+
switch(branch)
268+
{
269+
case 'C':
270+
System.out.println("Data Communication and Networks, MultiMedia");
271+
break;
272+
case 'E':
273+
System.out.println("Embedded System, Image Processing");
274+
break;
275+
case 'M':
276+
System.out.println("Production Technology, Thermal Engineering");
277+
break;
278+
}
279+
break;
280+
}
281+
}
282+
}
283+
284+
Output : Data Communication and Networks, MultiMedia
285+
```

0 commit comments

Comments
 (0)