Difference between revisions of "C-and-C++/C2/Nested-If-And-Switch-Statement/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 || '''Time''' || '''Narration''' |- | 00:01 | Welcome to the spoken tutorial on Nested if & Switch statements in C and C++. |- | 00:08 | In this tutorial we wil…')
 
Line 7: Line 7:
  
 
|-
 
|-
| 00:08
+
| 00:07
 
|  In this tutorial we will learn ,  
 
|  In this tutorial we will learn ,  
  
 
|-
 
|-
|  00:11
+
|  00:09
 
| How to use nested if statement. And
 
| How to use nested if statement. And
  
 
|-
 
|-
|  00:14
+
|  00:12
 
| switch statement.
 
| switch statement.
  
 
|-
 
|-
|  00:16
+
|  00:13
| We will do this with the help of an examples.
+
| We will do this with the help of some examples.
  
 
|-
 
|-
|  00:20
+
|  00:17
 
|  To record this tutorial, I am using,
 
|  To record this tutorial, I am using,
  
 
|-
 
|-
|  00:23
+
|  00:20
 
| Ubuntu operating system version 11.10
 
| Ubuntu operating system version 11.10
  
 
|-
 
|-
|  00:27
+
|  00:24
 
| gcc and g++ Compiler version 4.6.1 on Ubuntu.
 
| gcc and g++ Compiler version 4.6.1 on Ubuntu.
  
 
|-
 
|-
| 00:34
+
| 00:30
 
|  First we will learn, how to write '''nested if and switch statement''' with an example.
 
|  First we will learn, how to write '''nested if and switch statement''' with an example.
  
 
|-
 
|-
|  00:42
+
|  00:36
 
| I have already written the program  
 
| I have already written the program  
  
 
|-
 
|-
| 00:44
+
| 00:39
 
|  Let’s have a look  
 
|  Let’s have a look  
 
|-
 
|-
|  00:49
+
|  00:40
 
|  In this program ,we will learn to check the range of integers.
 
|  In this program ,we will learn to check the range of integers.
 
|-
 
|-
|  00:53
+
|  00:45
 
|  Note that our file name is '''nested-if.c'''
 
|  Note that our file name is '''nested-if.c'''
  
 
|-
 
|-
01:00
+
|  00:50
 
|  Let me explain the code now.
 
|  Let me explain the code now.
  
 
|-
 
|-
01:03
+
00:52
 
|  This is our Header file.
 
|  This is our Header file.
  
  
 
|-
 
|-
01:05
+
00:54
 
|  This is our  main function.
 
|  This is our  main function.
 
   
 
   
  
 
|-
 
|-
01:07
+
00:56
| Inside the main function we have declared   two integer variable ''''x and y''''
+
| Inside the main function we have declared two integer variables ''''x and y''''
  
 
|-
 
|-
|  01:16
+
|  01:02
 
|Here we prompt the users to enter a number between the range of''' 0 to 39'''.
 
|Here we prompt the users to enter a number between the range of''' 0 to 39'''.
 
|-
 
|-
| 01:23
+
| 01:08
 
|  we take the value of '''y''' as input from the user.
 
|  we take the value of '''y''' as input from the user.
  
 
|-
 
|-
|  01:30
+
|  01:12
 
| This is our if condition  
 
| This is our if condition  
  
 
|-
 
|-
|  01:32
+
|  01:14
 
| Here, we will check whether y/10=0
 
| Here, we will check whether y/10=0
  
 
|-
 
|-
|  01:36
+
|  01:19
 
| If the condition is true
 
| If the condition is true
  
 
|-
 
|-
|  01:38
+
|  01:20
 
| We print '''"you have entered a number  in the range of 0-9.'''
 
| We print '''"you have entered a number  in the range of 0-9.'''
  
 
|-
 
|-
| 01:47
+
| 01:25
 
| This is our else-if condition.
 
| This is our else-if condition.
  
 
|-
 
|-
|  01:50
+
|  01:28
 
|  Here we check that y/10=1.
 
|  Here we check that y/10=1.
 
|-
 
|-
|  01:54
+
|  01:32
 
|If the condition is true.
 
|If the condition is true.
  
 
|-
 
|-
|  01:56
+
|  01:34
 
| We print '''you have entered a number in the range of 10-19.'''
 
| We print '''you have entered a number in the range of 10-19.'''
  
 
|-
 
|-
02:03
+
01:39
 
|In this else if condition we check whether the number is in the range of 20-29.
 
|In this else if condition we check whether the number is in the range of 20-29.
 
|-
 
|-
02:11
+
01:45
 
| And here we will see that the number is in the range of 30 to 39.
 
| And here we will see that the number is in the range of 30 to 39.
 
|-
 
|-
02:19
+
01:51
 
|  This is our else condition.
 
|  This is our else condition.
  
  
 
|-
 
|-
02:21
+
01:53
 
|  If all of the above conditions are false
 
|  If all of the above conditions are false
  
Line 144: Line 144:
 
|-
 
|-
 
| 02:57
 
| 02:57
| type dot slash “./nested”. Press '''Enter'''
+
| type dot slash “nested”. Press '''Enter'''
  
 
|-
 
|-
Line 235: Line 235:
 
|-
 
|-
 
|  04:50
 
|  04:50
| Then we print '''you have entered  the number in the range of0-9.'''
+
| Then we print '''you have entered  the number in the range of 0-9.'''
  
 
|-
 
|-
Line 269: Line 269:
 
| 05:33
 
| 05:33
 
| And this is case 3. Here we check whether the number is in the range of 30-39.
 
| And this is case 3. Here we check whether the number is in the range of 30-39.
 +
 
|-
 
|-
 
| 05:43
 
| 05:43
Line 294: Line 295:
 
|  06:06
 
|  06:06
 
|Type:gcc switch.c -o switch. Press Enter
 
|Type:gcc switch.c -o switch. Press Enter
 +
 
|-
 
|-
 
| 06:16  
 
| 06:16  
Line 302: Line 304:
 
|  06:21
 
|  06:21
 
| Enter a number between  of 0 to  39. I willl enter 35
 
| Enter a number between  of 0 to  39. I willl enter 35
 +
 
|-
 
|-
 
|  06:28
 
|  06:28
Line 308: Line 311:
 
|-
 
|-
 
|  06:35
 
|  06:35
| Now we will see how to execute the program in C++.
+
| Now we will see how to execute the programs in C++.
 
|-
 
|-
 
| 06:44
 
| 06:44
Line 378: Line 381:
 
|-
 
|-
 
|  08:33
 
|  08:33
| Rest of the code is similar to our switch.c C program
+
| Rest of the code is similar to our switch.c program
 
|-
 
|-
 
|  08:38
 
|  08:38
Line 417: Line 420:
 
|-
 
|-
 
|  09:28
 
|  09:28
|Netsed-if statement is run only if the result of the expression is true.
+
|Netsed-if statement is run, only if the result of the expression is true.
 
|-
 
|-
 
|  09:35
 
|  09:35

Revision as of 10:50, 31 October 2013

Time Narration
00:01 Welcome to the spoken tutorial on Nested if & Switch statements in C and C++.
00:07 In this tutorial we will learn ,
00:09 How to use nested if statement. And
00:12 switch statement.
00:13 We will do this with the help of some examples.
00:17 To record this tutorial, I am using,
00:20 Ubuntu operating system version 11.10
00:24 gcc and g++ Compiler version 4.6.1 on Ubuntu.
00:30 First we will learn, how to write nested if and switch statement with an example.
00:36 I have already written the program
00:39 Let’s have a look
00:40 In this program ,we will learn to check the range of integers.
00:45 Note that our file name is nested-if.c
00:50 Let me explain the code now.
00:52 This is our Header file.


00:54 This is our main function.


00:56 Inside the main function we have declared two integer variables 'x and y'
01:02 Here we prompt the users to enter a number between the range of 0 to 39.
01:08 we take the value of y as input from the user.
01:12 This is our if condition
01:14 Here, we will check whether y/10=0
01:19 If the condition is true
01:20 We print "you have entered a number in the range of 0-9.
01:25 This is our else-if condition.
01:28 Here we check that y/10=1.
01:32 If the condition is true.
01:34 We print you have entered a number in the range of 10-19.
01:39 In this else if condition we check whether the number is in the range of 20-29.
01:45 And here we will see that the number is in the range of 30 to 39.
01:51 This is our else condition.


01:53 If all of the above conditions are false
02:24 We print number not in range.
02:28 And this is our return statement
02:31 Now let us execute the program.
02:35 Please open the terminal window , by pressing Ctrl+Alt+T keys simultaneously on your keyboard.
02:45 To execute , Type “gcc” space “nested-if.c” space hyphen “-o” space “nested”. Press Enter
02:57 type dot slash “nested”. Press Enter
03:01 We see,Enter a number between 0 to 39.
03:06 I will Enter 12
03:09 The output is displayed as:
03:11 you have entered the number in the range of 10-19.
03:17 Let us enter another number.
03:21 Let's execute again. Press the up arrow key, press enter.
03:28 I will give 5 this time.
03:34 We see the output as :
03:35 you have entered the number in the range of 0-9.
03:42 The conditional execution can also be done in another way.
03:46 By using switch statement.
03:49 Let’s see how it is done.
03:51 We will see the same program using switch.
03:57 I have already opened the program.


03:59 Let's switch back to our text editor
04:07 I have explained this in the previous program.
04:11 So i will move on to the Switch statements


04:16 Here, we divide the inputs i.e y by 10 and the result is stored in the variable x.
04:24 That means the quotient will be stored in x.
04:28 With the help of the quotient we can identify the range of the number.
04:37 Here, we tell the switch command that the variable to be checked is x.
04:47 This is case 0 . If case 0 is satisfied.
04:50 Then we print you have entered the number in the range of 0-9.
04:58 We add break to come out of the loop if the case is satisfied.
05:03 We need to break the loop each time.
05:05 It is because only one condition can be true at a time.
05:11 This is “case 1” . “case 1” means “if the value of x is 1”


05:17 We print you have entered a number in the range of 10-19.
05:24 This is “case 2” .
05:26 Here we print you have entered a number in the range of 20-29.
05:33 And this is case 3. Here we check whether the number is in the range of 30-39.
05:43 This is the default case. Default case specifies what needs to be done if none of the above cases are satisfied.
05:52 Here we print number not in range.


05:57 And This is our return statement.
05:59 Let us execute the program.


06:02 Switch back to the terminal.
06:06 Type:gcc switch.c -o switch. Press Enter
06:16 Type:./switch. Press Enter


06:21 Enter a number between of 0 to 39. I willl enter 35
06:28 The output is displayed as,“you have entered the number in the range of 30 to 39”.
06:35 Now we will see how to execute the programs in C++.
06:44 Switch back to the text editor.
06:47 Note that our filename is nested-if.cpp
06:55 Here the logic and implementation are same
06:59 There are a few changes like:
07:03 The header file as iostream in place of stdio.h
07:08 Here we have included the using statement.
07:11 Using namespace std
07:14 And the cout and cin function in place of printf and scanf.
07:23 You can see that the rest of the code is similar to our C program.
07:29 Let’s execute the code.
07:31 Come back to the terminal.
07:34 Type: g++ nested-if.cpp -o nested1. Press Enter.
07:45 Type: ./nested1. Press Enter
07:50 enter a number between 0 and 39. I will enter 40.
07:53 The output is displayed as: “number not in range”
08:06 Now let’s see the switch program in C++
08:10 Come back to the text editor.
08:14 Here also the logic and implementation are same.
08:19 You can see that the header file is iostream
08:23 Here is the using statement.
08:25 And we have changed the cout and cin function.
08:33 Rest of the code is similar to our switch.c program
08:38 Let us execute.
08:40 Come back to the terminal.
08:42 Type:g++ switch.cpp -o switch1 Press Enter
08:52 Type ./switch1. Press Enter
08:57 Enter a number between 0 and 39.
09:00 I will enter 25.
09:04 The output is displayed as:
09:06 “you have entered the number in the range of 20-29”
09:11 Now let us switch back to our slides.
09:16 We will see the comparison between switch and nested-if statement.
09:21 Switch statement is evaluated according to the result of the expression.
09:28 Netsed-if statement is run, only if the result of the expression is true.
09:35 In switch we treat various values of the variable as cases.
09:41 In nested-if we have to write the conditional statement for each value of the variable.
09:49 Switch statement can only check the integer values
09:54 Nested if can check for both integer and fractional values.
10:00 This brings us to the end of this tutorial.
10:03 Let us summarize.
10:05 In this tutorial we learnt, nested if statement.
Ex: else if( y/10==0) 
10:13 switch statement.

e.g. Switch(x)

10:16 And Difference between nested-if and switch statements.


10:22 As an assignment,
10:23 Write a program to check whether the age of the employee is between 20 to 60.
10:30 Watch the video available at the link shown http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
10:33 It summarizes the Spoken Tutorial project
10:36 If you do not have good bandwidth, you can download and watch it.
10:40 The Spoken Tutorial Project Team,
10:42 Conducts workshops using spoken tutorials.
10:45 Gives certificates to those who pass an online test.
10:49 For more details please write to contact @spoken-tutorial.org
10:56 Spoken Tutorial Project is a part of the Talk to a Teacher project
11:00 It is supported by the National Mission on Education through ICT, MHRD, Government of India
11:08 More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
11:13 The script is contributed by Chaitanya Mokashi. This is Ashwini patil from IIT Bombay signing off. Thank you for joining.