Difference between revisions of "Java/C2/Switch-Case/English-timed"

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

Revision as of 15:10, 25 March 2015

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 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 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.
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 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 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.

Contributors and Content Editors

Gaurav, PoojaMoolya, Sandhya.np14, Sneha