Skip to content

Commit c707c0a

Browse files
authored
Merge pull request #54 from ananya0504/io-conditionals-switch-stmt
Fixed issue: io-conditionals.md (Improved switch statement)
2 parents 18d66ce + d620002 commit c707c0a

File tree

2 files changed

+160
-16
lines changed

2 files changed

+160
-16
lines changed
9.95 KB
Loading

lessons/io-conditionals.md

Lines changed: 160 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,173 @@ 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. 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.
129136

130-
Above is the basic syntax of switch statements. Let's see some examples to understand it better.
137+
![java-switch-statement](./images/java-switch-statement.png)
138+
139+
Let us see an example to understand it better.
131140

132141
```java
133-
// print hello world in different languages using switch statement
142+
// Program to check Vowel or Consonant:
143+
// It is not case-sensitive.
134144

135145
public class <ClassName> {
136146
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");
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");
150182
break;
151183
}
152184
}
153185
}
186+
187+
Output : Vowel
154188
```
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+
public class SwitchExample2 {
203+
public static void main(String[] args) {
204+
int num = 20;
205+
switch(num){
206+
case 10:
207+
System.out.println("10");
208+
case 20:
209+
System.out.println("20");
210+
case 30:
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+
public class NestedSwitchExample {
240+
public static void main(String args[])
241+
{
242+
//C - CSE, E - ECE, M - Mechanical
243+
char branch = 'C';
244+
int collegeYear = 4;
245+
switch(collegeYear)
246+
{
247+
case 1:
248+
System.out.println("English, Maths, Science");
249+
break;
250+
case 2:
251+
switch( branch )
252+
{
253+
case 'C':
254+
System.out.println("Operating System, Java, Data Structure");
255+
break;
256+
case 'E':
257+
System.out.println("Micro processors, Logic switching theory");
258+
break;
259+
case 'M':
260+
System.out.println("Drawing, Manufacturing Machines");
261+
break;
262+
}
263+
break;
264+
case 3:
265+
switch(branch)
266+
{
267+
case 'C':
268+
System.out.println("Computer Organization, MultiMedia");
269+
break;
270+
case 'E':
271+
System.out.println("Fundamentals of Logic Design, Microelectronics");
272+
break;
273+
case 'M':
274+
System.out.println("Internal Combustion Engines, Mechanical Vibration");
275+
break;
276+
}
277+
break;
278+
case 4:
279+
switch(branch)
280+
{
281+
case 'C':
282+
System.out.println("Data Communication and Networks, MultiMedia");
283+
break;
284+
case 'E':
285+
System.out.println("Embedded System, Image Processing");
286+
break;
287+
case 'M':
288+
System.out.println("Production Technology, Thermal Engineering");
289+
break;
290+
}
291+
break;
292+
}
293+
}
294+
}
295+
296+
Output : Data Communication and Networks, MultiMedia
297+
```
298+

0 commit comments

Comments
 (0)