Java/C2/Switch-Case/English
Title of script: Switch Case in Java
Author: TalentSprint
Keywords: case, switch, conditions, video tutorial
Visual Cue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on Switch case in Java. |
Slide 2
Learning Outcomes |
In this tutorial, you will learn how to use the switch case construct in Java |
Slide 3
Tools Used |
For this tutorial we are using
|
Slide 4
Prerequisites |
For this tutorial, you should have knowledge of if else statement in Java.
|
Slide 5
switch case |
A switch case is used to perform actions based on the value of a variable.
|
Minimize Slides and open Eclipse
Eclipse should contain the following code public class SwitchCaseDemo{ public static void main(String[] args){ } } |
I already have Eclipse opened.
|
Inside the main, type
int day = 3; String dName = “”;
|
Inside the main method, we will create a variable day of type int.
String dane we shall initialize Then initialize it to null.
Now, we will type the switch case statement.
|
Highlight switch(day) | This statement defines which variable is under consideration for the cases. |
Type
case 0: dName = “Sunday”; break; } |
Next line type
dName equal to within double quotes Sunday semicolon Then type break semicolon.
|
switch(day) {
case 0: dName = “Sunday”; break; case 1: dName = “Monday”; break; case 2: dName = “Tuesday”; break; case 3: dName = “Wednesday”; break; case 4: dName = “Thursday”; break; case 5: dName = “Friday”; break; case 6: dName = “Saturday”; break; } |
Similarly, let us type the remaining cases.
dName equal to within double quotes Monday semicolon break semicolon case 2 colon dName equal to within double quotes Tuesday semicolon break semicolon case 3 colon dName equal to within double quotes Wednesday semicolon break semicolon case 4 colon dName equal to within double quotes Thursday semicolon break semicolon case 5 colon dName equal to within double quotes Friday semicolon break semicolon case 5 colon dName equal to within double quotes Saturday semicolon break semicolon Then close the brackets. |
Let us now add a print statement and see the code in action. | |
Type System.out.println(dName);
|
Type System dot out dot println within brackets dName semicolon.
|
Change day = 3 to day = 0
|
Change 3 to 0
|
Change day = 0 to day = -1
save and run. Point to output |
As we can see, there is no output.
|
After the last case, type this
default: dName = “Wrong Choice” break;
|
After the last case, type
dName equal to within double quotes Wrong Choice break semicolon
|
Change day = -1 to day = 15
save and run. Point to output |
Change -1 to 15
As we can see, again the default case is executed. |
Now let us see what happens if we remove the break statement. | |
Change day = 15 to day = 4
Remove the break in case 4 statement there |
Let us change day = 15 to day = 4
|
Point to output | Although the case is 4, we get the output as Friday and not Thursday. |
Highlight case 4 and case 5 | This is because of the way switch case works.
|
When a match is found, it executes all the case from the match onwards.
Then it stops because of the break statement in case 5. | |
To avoid that, we need to add a break statement in each case. | |
Type in case 4
break; |
Let us now add the break statement we removed.
|
Save and run. Point to output | As we can see, now only case 4 is executed.
|
Minimize the Eclipse window and switch to slides.
Summary |
We have come to the end of this tutorial.
In this tutorial we have learnt how to use switch case construct and how to use break statement. |
Slide 7Assignment | Write a program that has a name and gender and a switch case statement that prints “Hello Mr....” for males and “Hello Ms...” for females. |
Slide 8About the Spoken Tutorial Project
|
To know more about the Spoken Tutorial project, watch the video available at the following link, that summarises the project.Alternatively, you can download and watch it. |
Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
Slide 10Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
Slide 11About the contributor
|
This tutorial has been contributed by TalentSprint. Thanks for joining.
|