Java/C2/Switch-Case/English-timed

From Script | Spoken-Tutorial
Revision as of 15:07, 28 March 2017 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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 Ubuntu v 11.10, JDK 1.6 and Eclipse 3.7.0
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 [1]
00:32 switch case: The 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 semicolon.
01:12 Now, let us create a variable dName of type String.
01:18 String dName we can initialize it to null.
01:25 Here, dName is a variable to hold the name 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 line break;
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 Then case 4 colon.
03:24 Next line, dName equal to within double quotes Thursday then semicolon.
03:32 Then break .
03:34 Then next line, type: case 5 colon.
03:37 dName equal to within double quotes Friday then semicolon.
03:41 Then break.
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: Wednesday which is corresponding to the case 3.
04:31 Now, let us change the value of the day and see the result.
04:35 So change 3 to 0.
04:38 Now Save and Run that 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 the file.
06:15 Although the case is 4, we get the output as Friday and not Thursday.
06:20 This is 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 assignment, 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 summarizes 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 to 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.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14, Sneha