C-and-C++/C2/Nested-If-And-Switch-Statement/English-timed

From Script | Spoken-Tutorial
Revision as of 13:04, 24 March 2017 by Pratik kamble (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Nested if and 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.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 statements with an example.
00:36 I have already written the program.
00:39 Let’s have a look. 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, we print "you have entered the number in the range of 0-9".
01:25 This is our else-if condition.
01:28 Here we check that y/10 equals to 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,
01:55 we print "number not in range".
01:58 And this is our return statement.
02:01 Now let us execute the program.
02:03 Please open the terminal window by pressing Ctrl+Alt and T keys simultaneously on your keyboard.
02:12 To execute, type gcc space nested-if.c space hyphen o space nested. Press Enter.
02:23 Type dot slash “nested” (./nested). Press Enter.
02:28 We see Enter a number between 0 to 39.
02:32 I will enter 12.
02:34 The output is displayed as:
02:35 you have entered the number in the range of 10-19.
02:40 Let us enter another number.
02:42 Let's execute again. Press the up arrow key, press Enter.
02:48 I will give 5 this time.
02:50 We see the output as :
02:52 you have entered the number in the range of 0-9.
02:56 The conditional execution can also be done in another way.
03:00 By using switch statement.
03:02 Let’s see how it is done.
03:05 We will see the same program using switch.
03:08 I have already opened the program.
03:10 Let's switch back to our text editor.
03:13 I have explained this in the previous program.
03:16 So I will move on to the switch statements.
03:20 Here, we divide the inputs i.e y by 10 and the result is stored in the variable x.
03:28 That means the quotient will be stored in x.
03:32 With the help of the quotient we can identify the range of the number.
03:36 Here, we tell the switch command that the variable to be checked is x.
03:41 This is case 0 . If case 0 is satisfied
03:45 then we print you have entered the number in the range of 0-9.
03:51 We add break to come out of the loop if the case is satisfied.
03:55 We need to break the loop each time.
03:58 It is because only one condition can be true at a time.
04:03 This is “case 1”. “case 1” means “if the value of x is 1”.
04:08 We print you have entered a number in the range of 10-19.
04:12 This is “case 2” .
04:14 Here we print you have entered a number in the range of 20-29.
04:20 And this is case 3. Here we check whether the number is in the range of 30-39.
04:26 This is the default case. Default case specifies what needs to be done if none of the above cases are satisfied.
04:36 Here we print "number not in range".
04:39 And This is our return statement.
04:41 Let us execute the program.
04:43 Switch back to the terminal.
04:46 Type gcc space switch.c space -o space switch. Press Enter.
04:55 Type ./switch(dot slash switch). Press Enter.
05:00 Enter a number between 0 to 39. I will enter 35.
05:06 The output is displayed as “you have entered the number in the range of 30 to 39”.
05:10 Now we will see how to execute the programs in C++.
05:16 Switch back to the text editor.
05:18 Note that our file name is nested-if.cpp.
05:23 Here the logic and implementation are same.
05:27 There are a few changes like:
05:30 The header file as "iostream" in place of "stdio.h".
05:35 Here we have included the using statement
05:39 Using namespace std.
05:41 And the cout and cin function in place of printf and scanf.
05:46 You can see that the rest of the code is similar to our C program.
05:51 Let’s execute the code.
05:53 Come back to the terminal.
05:56 Type g++ space nested-if.cpp space -o space nested1. Press Enter.
06:07 Type ./nested1. Press Enter.
06:11 Enter a number between 0 to 39. I will enter 40.
06:16 The output is displayed as: “number not in range”
06:20 Now let’s see the switch program in C++.
06:24 Come back to our text editor.
06:27 Here also the logic and implementation are same.
06:31 You can see that the header file is iostream.
06:34 Here is the using statement.
06:37 And we have changed the cout and cin function.
06:41 Rest of the code is similar to our switch.c program.
06:45 Let us execute.Come back to our terminal.
06:48 Type g++ space switch.cpp space -o space switch1. Press Enter.
06:58 Type ./switch1. Press Enter.
07:02 Enter a number between 0 to 39.
07:05 I will enter 25.
07:09 The output is displayed as:
07:11 “you have entered the number in the range of 20-29”
07:15 Now let us switch back to our slides.
07:18 We will see the comparison between switch and nested-if statements.
07:23 Switch statement is evaluated according to the result of the expression.
07:28 Netsed-if statement is run only if the result of the expression is true.
07:34 In switch, we treat various values of the variable as cases.
07:39 In nested-if we have to write the conditional statement for each value of the variable.
07:45 Switch statement can only check the integer values.
07:50 Nested if can check for both, integer and fractional values.
07:55 This brings us to the end of this tutorial.
07:58 Let us summarize.
08:00 In this tutorial, we learnt * 'nested if' statement. e.g. else if( y/10 equals to 0)
08:08 switch statement. e.g. switch(x) and
08:12 Difference between nested-if and switch statements.
08:16 As an assignment,write a program to check whether the age of the employee is between 20 to 60.
08:23 Watch the video available at the link shown http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial.
08:26 It summarizes the Spoken Tutorial project.
08:29 If you do not have good bandwidth, you can download and watch it.
08:33 The Spoken Tutorial Project Team, Conducts workshops using spoken tutorials.
08:38 Gives certificates to those who pass an online test.
08:42 For more details please write to contact @spoken-tutorial.org
08:49 Spoken Tutorial Project is a part of the Talk to a Teacher project.
08:52 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
08:58 More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro.
09:04 The script is contributed by Chaitanya Mokashi. This is Ashwini patil from IIT Bombay signing off. Thank you for joining.