Difference between revisions of "Java/C2/Switch-Case/English"
Line 93: | Line 93: | ||
Now, let us create a '''variable dName''' of type '''String.''' | Now, let us create a '''variable dName''' of type '''String.''' | ||
− | String | + | String ane we shall linitialize it to null. |
− | |||
Line 101: | Line 100: | ||
− | '''day''' stores the '''day number'''. | + | '''day''' stores the '''day number'''. |
− | Now, we will type the '''switch case statement.''' | + | Now, we will type the '''switch case statement.'''So next line type |
− | + | '''switch '''within brackets''' day , '''then open curly brackets... Press enter | |
Line 138: | Line 137: | ||
− | This statement says that if the value of day is 0, | + | This statement says that if the value of day is 0, then '''dName''' must be set to '''Sunday.''' |
Line 202: | Line 201: | ||
'''dName '''''equal to within double quotes''''' Monday '''''semicolon'' | '''dName '''''equal to within double quotes''''' Monday '''''semicolon'' | ||
− | '''break ' | + | '''break ''' |
'''case 2 '''''colon'' | '''case 2 '''''colon'' | ||
Line 208: | Line 207: | ||
'''dName '''''equal to within double quotes''''' Tuesday '''''semicolon'' | '''dName '''''equal to within double quotes''''' Tuesday '''''semicolon'' | ||
− | '''break ' | + | '''break ''' |
'''case 3 '''''colon'' | '''case 3 '''''colon'' | ||
Line 214: | Line 213: | ||
'''dName '''''equal to within double quotes''''' Wednesday '''''semicolon'' | '''dName '''''equal to within double quotes''''' Wednesday '''''semicolon'' | ||
− | '''break '' | + | '''break '''' |
'''case 4 '''''colon'' | '''case 4 '''''colon'' | ||
Line 220: | Line 219: | ||
'''dName '''''equal to within double quotes''''' Thursday '''''semicolon'' | '''dName '''''equal to within double quotes''''' Thursday '''''semicolon'' | ||
− | '''break ' | + | '''break ''' |
'''case 5 '''''colon'' | '''case 5 '''''colon'' | ||
Line 226: | Line 225: | ||
'''dName '''''equal to within double quotes''''' Friday '''''semicolon'' | '''dName '''''equal to within double quotes''''' Friday '''''semicolon'' | ||
− | '''break ''' | + | '''break ''' |
− | + | ||
'''case 5 '''''colon'' | '''case 5 '''''colon'' | ||
'''dName '''''equal to within double quotes''''' Saturday '''''semicolon'' | '''dName '''''equal to within double quotes''''' Saturday '''''semicolon'' | ||
− | '''break ''''' | + | '''break ''' |
− | + | Then '''case 6 '''Colon''' | |
+ | '''dname'''wihin double quotes'''Saturaday''' | ||
+ | '''break'''Semicolon | ||
Then close the brackets. | Then close the brackets. | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| | | style="border:0.035cm solid #000000;padding:0.097cm;"| | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| now Let us add a print statement and see the code in action. |
|- | |- | ||
Line 248: | Line 248: | ||
Point to output | Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Type '''System '''''dot '''''out '''''dot '''''println '''''within brackets '''''dName '''''semicolon.'' | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|So next line Type '''System '''''dot '''''out '''''dot '''''println '''''within brackets '''''dName '''''semicolon.'' |
− | '''Save''' and | + | Now '''Save''' and runfile. |
+ | Now press Ctrl S and Ctrl F11 keys | ||
− | |||
+ | we get the output as '''Wednesday'''which is corresponding to the case '''3.''' | ||
− | Let us change the value of day and see the result | + | |
+ | Now Let us change the value of day and see the result | ||
|- | |- | ||
Line 267: | Line 269: | ||
Point to output | Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| SoChange '''3''' to '''0''' |
− | + | NowSave and Run | |
Line 276: | Line 278: | ||
− | Now what if there is no case corresponding to the value. Let us see | + | Now what if there is no case corresponding to the value. Let us see that |
|- | |- | ||
Line 282: | Line 284: | ||
save and run. Point to output | save and run. Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| As we can see, there is no output. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| So change day equal to -1 Save and run the file |
+ | As we can see, there is no output. | ||
Line 304: | Line 307: | ||
Point to output | Point to output | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| After the last case, type | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| So After the last case, type |
'''default '''''colon'' | '''default '''''colon'' | ||
− | '''dName '''''equal to within double quotes '''''Wrong Choice''' | + | '''dName '''''equal to within double quotes '''''Wrong Choice'''''semicolon'' |
'''break '''''semicolon'' | '''break '''''semicolon'' | ||
− | + | We do not say '''case default'''; | |
Note that we simply use the keyword '''default''' . | Note that we simply use the keyword '''default''' . | ||
+ | Now let us run the code. So save and run the file | ||
− | + | ||
− | + | As we can see, the default case is executed and the required message '''Wrong choice''' is printed. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | As we can see, the default case is executed and the required message is printed. | + | |
Line 344: | Line 343: | ||
Remove the '''break in case 4''' statement there | Remove the '''break in case 4''' statement there | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us change '''day = 15''' to '''day = 4''' | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|So Let us change '''day = 15''' to '''day = 4''' |
− | + | remove the corresponding break statement for '''day<nowiki>=4</nowiki>''' | |
− | Save and run | + | Now Save and run |
|- | |- | ||
Line 383: | Line 382: | ||
'''break<nowiki>;</nowiki>''' | '''break<nowiki>;</nowiki>''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us now add the break statement we removed. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us now add the break statement we have removed. |
− | '''Type break '''''semicolon.'' | + | So type '''Type break '''''semicolon.'' |
Line 411: | Line 410: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 7'''Assignment''' | | style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 7'''Assignment''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| 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. |
|- | |- |
Latest revision as of 10:16, 9 May 2013
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.
|