Java/C2/Switch-Case/English-timed
From Script | Spoken-Tutorial
Time' | Narration |
00:02 | Welcome to the spoken tutorial on Switch case in Java. |
00:06 | In this tutorial, you will learn how to use the switch case construct in Java |
00:11 | For this tutorial we are using
|
00:21 | For this tutorial, you should have knowledge of if else statement in Java.
|
00:25 | If not, please go through the tutorials on these topics available at our website which is as shown http://spoken-tutorial.org |
00:32 | A switch case is used to perform actions based on the value of a variable.
|
00:39 | Here is the syntax for a switch case statement.
|
00:44 | Let us use it now. |
00:47 | I already have Eclipse opened.
|
00:49 | I have created a class named SwitchCaseDemo.
|
00:53 | Now Let us add some variables. |
00:57 | Inside the main method, we will create a variable day of type int.
|
01:02 | So type inside the main method int day and we can give it a value equal to 3 semi-colon.
|
01:12 | Now, let us create a variable dName of type String.
|
01:18 | String dName we can linitialize it to null.
|
01:25 | Here dName is a variable to hold the names of the days of a week.
|
01:34 | day stores the day number.
|
01:36 | Now, we will type the switch case statement.So next line type
|
01:43 | switch within brackets day , then open curly brackets... Press enter
|
01:52 | This statement defines which variable is under consideration for the cases. |
01:59 | Next line type
|
02:01 | case 0 colon
|
02:04 | Next line dName equal to within double quotes Sunday semicolon
|
02:14 | Then type Next linebreak
|
02:17 | This statement says that if the day is 0, then dName must be set to Sunday.
|
02:26 | Note that a break statement must be used at the end of each case.
|
02:31 | without the break statement, the switch-case functions in a complex fashion.
|
02:35 | It will be explained in subsequent part of the tutorial. |
02:40 | Similarly, let us type the remaining cases.
|
02:45 | Next line type case 1 colon |
02:50 | next line dName equal to within double quotes Monday semicolon |
02:56 | Next line type break |
02:58 | Then type case 2 colon |
03:01 | Next line dName equal to Tuesday then semicolon |
03:06 | Next line type break |
03:08 | Then next line case 3 colon |
03:12 | Next line type dName equal to within double quotes Wednesday then semicolon |
03:18 | Next line type break ' |
03:20 | Thencase 4 colon |
03:24 | Next line dName equal to within double quotes Thursday then semicolon |
03:32 | Thenbreak |
03:24 | Then next line typecase 5 colon |
03:37 | dName equal to within double quotes Friday then semicolon |
03:41 | Thenbreak |
03:43 | Then case 6 colon |
03:47 | Next line type dName equal to within double quotes Saturday then semicolon |
03:55 | Then type break semicolon |
03:59 | Then close the brackets. |
04:03 | now Let us add a print statement and see the code in action. |
04:07 | So next line Type System dot out dot println within brackets dName then semicolon.
|
04:16 | Now Save and run the file.
|
04:20 | Now press Ctrl S and Ctrl F11 keys
|
04:25 | we get the output as Wednesdaywhich is corresponding to the case 3.
|
04:31 | Now Let us change the value of the day and see the result |
04:35 | SoChange 3 to 0
|
04:38 | NowSave and Run tht file
|
04:40 | As we can see, the output is Sunday corresponding to the case 0
|
04:46 | Now what if there is no case corresponding to the value. So Let us see that |
04 :52 | change day equal to -1 Save and run the file |
04:58 | As we can see, there is no output.
|
05:01 | But it would be better if we could have a case for all other values.
|
05:06 | That is done by using the default keyword. |
05:09 | So After the last case, type
|
05:12 | default colon |
05:14 | Next line dName equal to within double quotes Wrong Choice then semicolon |
05:24 | Next line break semicolon |
05:27 | We do not say case default;
|
05:30 | Note that we simply use the keyword default .
|
05:34 | Now let us run the code. So save and run the file
|
05:38 | As we can see, the default case is executed and the required message Wrong choice is printed.
|
05:45 | Let us try with another random value. |
05:48 | Change -1 to 15
|
05:51 | As we can see, again the default case is executed. |
05:57 | Now let us see what happens if we remove the break statement. |
06:01 | So Let us change day = 15 to day = 4
|
06:07 | remove the corresponding break statement for day =4
|
06:12 | Now Save and run |
06:15 | Although the case is 4, we get the output as Friday and not Thursday. |
06:20 | because of the way switch case works.
|
06:24 | First the value of day is compared with 0.
|
06:29 | Then with 1 then with 2 and so on with all the possible cases. |
06:34 | When a match is found, it executes all the case from the match onwards.
|
06:42 | In our case, it executed case 5 after case 4 .
|
06:47 | Then it stops because of the break statement in case 5. |
06:53 | To avoid that, we need to add a break statement in each case. |
06:57 | Let us now add the break statement we have removed.
|
07:00 | So type break semicolon.
|
07:05 | Now let us run the code. |
07:08 | As we can see, now only case 4 is executed.
|
07:13 | As a rule, remember to use a break statement in every case to avoid errors. |
07:20 | We have come to the end of this tutorial.
|
07:22 | In this tutorial we have learnt how to use switch case construct and how to use break statement. |
07:30 | As an assignemet write 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. |
07:44 | To know more about the Spoken Tutorial project, watch the video available at the following link, it summarises the spoken-tutorial project. |
07:53 | If you do not have good bandwidth you can download and watch it. |
07:58 | The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test.
|
08:06 | For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
08:12 | Spoken Tutorial Project is a part of the Talk to a Teacher project and it is supported by the |
08:17 | National Mission on Education through ICT, MHRD, Government of India. |
08:22 | More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
08:31 | This tutorial has been contributed by TalentSprint. Thanks for joining.
|