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 ane we shall linitialize it to null.
Here dName is a variable to hold the names of the days of a week.
day stores the day number. Now, we will type the switch case statement.So next line type
switch within brackets day , then open curly brackets... Press enter
|
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 case 2 colon dName equal to within double quotes Tuesday semicolon break case 3 colon dName equal to within double quotes Wednesday semicolon break ' case 4 colon dName equal to within double quotes Thursday semicolon break case 5 colon dName equal to within double quotes Friday semicolon break case 5 colon dName equal to within double quotes Saturday semicolon break Then case 6 Colon dnamewihin double quotesSaturaday breakSemicolon Then close the brackets. |
now Let us add a print statement and see the code in action. | |
Type System.out.println(dName);
|
So next line Type System dot out dot println within brackets dName semicolon.
Now press Ctrl S and Ctrl F11 keys
we get the output as Wednesdaywhich is corresponding to the case 3.
|
Change day = 3 to day = 0
|
SoChange 3 to 0
|
Change day = 0 to day = -1
save and run. Point to output |
So change day equal to -1 Save and run the file
As we can see, there is no output.
|
After the last case, type this
default: dName = “Wrong Choice” break;
|
So After the last case, type
dName equal to within double quotes Wrong Choicesemicolon break semicolon We do not say case default; Note that we simply use the keyword default . Now let us run the code. So save and run the file
|
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 |
So Let us change day = 15 to day = 4
remove the corresponding break statement for day=4
Now Save and run |
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 have 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 | As an assignemetWrite a program that has a name and gender as a variable Use 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.
|