Difference between revisions of "C-and-C++/C2/Nested-If-And-Switch-Statement/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(8 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | || Time | + | || '''Time''' |
− | || Narration | + | || '''Narration''' |
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Welcome to the spoken tutorial on Nested if | + | | Welcome to the spoken tutorial on '''Nested if''' and '''Switch statements in C and C++'''. |
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial we will learn , | + | | In this tutorial we will learn, |
|- | |- | ||
| 00:09 | | 00:09 | ||
− | | | + | | how to use '''nested if''' statement and |
|- | |- | ||
| 00:12 | | 00:12 | ||
− | | switch statement. | + | | '''switch''' statement.We will do this with the help of some examples. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 00:17 | | 00:17 | ||
− | | To record this tutorial, I am using | + | | To record this tutorial, I am using |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | Ubuntu operating system version 11.10 | + | | '''Ubuntu operating system version 11.10''', |
|- | |- | ||
| 00:24 | | 00:24 | ||
− | | gcc and g++ Compiler version 4.6.1 on Ubuntu. | + | | '''gcc and g++ Compiler version 4.6.1 on Ubuntu'''. |
|- | |- | ||
| 00:30 | | 00:30 | ||
− | | | + | | First we will learn how to write '''nested if''' and '''switch''' statements with an example. |
|- | |- | ||
| 00:36 | | 00:36 | ||
− | | I have already written the program | + | | I have already written the program. |
|- | |- | ||
| 00:39 | | 00:39 | ||
− | | Let’s have a look | + | | Let’s have a look. In this program we will learn to check the range of integers. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 00:45 | | 00:45 | ||
− | | Note that our file name is '''nested-if.c''' | + | | Note that our file name is '''nested-if.c'''. |
|- | |- | ||
Line 62: | Line 56: | ||
|- | |- | ||
| 00:54 | | 00:54 | ||
− | | This is our main function. | + | | This is our main() function. |
|- | |- | ||
| 00:56 | | 00:56 | ||
− | | Inside the main function we have declared two integer variables ''''x and y'''' | + | | Inside the main() function we have declared two integer variables ''''x and y''''. |
|- | |- | ||
Line 73: | Line 67: | ||
|- | |- | ||
| 01:08 | | 01:08 | ||
− | | | + | | We take the value of '''y''' as input from the user. |
|- | |- | ||
| 01:12 | | 01:12 | ||
− | | This is our if condition | + | | This is our '''if''' condition. |
|- | |- | ||
| 01:14 | | 01:14 | ||
− | | Here, we will check whether y/10=0 | + | | Here, we will check whether y/10=0. |
|- | |- | ||
| 01:19 | | 01:19 | ||
− | | If the condition is true | + | | If the condition is true, we print '''"you have entered the number in the range of 0-9".''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 01:25 | | 01:25 | ||
− | | This is our else-if condition. | + | | This is our '''else-if''' condition. |
|- | |- | ||
Line 100: | Line 90: | ||
|- | |- | ||
| 01:32 | | 01:32 | ||
− | |If the condition is true | + | |If the condition is true, |
|- | |- | ||
| 01:34 | | 01:34 | ||
− | | | + | | we print ''' "you have entered a number in the range of 10-19".''' |
|- | |- | ||
| 01:39 | | 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. |
|- | |- | ||
| 01:45 | | 01:45 | ||
Line 114: | Line 104: | ||
|- | |- | ||
| 01:51 | | 01:51 | ||
− | | This is our else condition. | + | | This is our '''else''' condition. |
|- | |- | ||
| 01:53 | | 01:53 | ||
− | | If all of the above conditions are false | + | | If all of the above conditions are false, |
|- | |- | ||
| 01:55 | | 01:55 | ||
− | | | + | | we print''' "number not in range".''' |
|- | |- | ||
| 01:58 | | 01:58 | ||
− | | And this is our return statement | + | | And this is our '''return''' statement. |
|- | |- | ||
| 02:01 | | 02:01 | ||
Line 133: | Line 123: | ||
|- | |- | ||
| 02:03 | | 02:03 | ||
− | | Please open the terminal window | + | | Please open the terminal window by pressing '''Ctrl+Alt''' and '''T''' keys simultaneously on your keyboard. |
|- | |- | ||
| 02:12 | | 02:12 | ||
− | |To | + | |To execute, type '''gcc space nested-if.c space hyphen o space nested'''. Press '''Enter'''. |
|- | |- | ||
| 02:23 | | 02:23 | ||
− | | | + | | Type dot slash “nested” (./nested). Press '''Enter'''. |
|- | |- | ||
| 02:28 | | 02:28 | ||
− | | We see | + | | We see '''Enter a number between 0 to 39'''. |
|- | |- | ||
| 02:32 | | 02:32 | ||
− | | I will | + | | I will enter '''12'''. |
|- | |- | ||
Line 164: | Line 154: | ||
|- | |- | ||
| 02:42 | | 02:42 | ||
− | | Let's execute again. Press the up arrow key, press | + | | Let's execute again. Press the up arrow key, press '''Enter'''. |
|- | |- | ||
Line 176: | Line 166: | ||
|- | |- | ||
| 02:52 | | 02:52 | ||
− | | you have entered the number | + | | '''you have entered the number in the range of 0-9'''. |
+ | |||
|- | |- | ||
| 02:56 | | 02:56 | ||
Line 183: | Line 174: | ||
|- | |- | ||
| 03:00 | | 03:00 | ||
− | | By using '''switch | + | | By using '''switch''' statement. |
|- | |- | ||
Line 196: | Line 187: | ||
| 03:08 | | 03:08 | ||
| I have already opened the program. | | I have already opened the program. | ||
− | |||
|- | |- | ||
| 03:10 | | 03:10 | ||
− | | Let's switch back to our text editor | + | | Let's switch back to our text editor. |
|- | |- | ||
| 03:13 | | 03:13 | ||
Line 207: | Line 197: | ||
|- | |- | ||
| 03:16 | | 03:16 | ||
− | |So | + | |So I will move on to the '''switch''' statements. |
− | + | ||
|- | |- | ||
| 03:20 | | 03:20 | ||
− | ||Here, we divide the inputs i.e '''y''' | + | ||Here, we divide the inputs i.e '''y''' by 10 and the result is stored in the variable '''x'''. |
|- | |- | ||
Line 228: | Line 217: | ||
|- | |- | ||
| 03:41 | | 03:41 | ||
− | |This is '''case 0''' . If case 0 is satisfied | + | |This is '''case 0''' . If case 0 is satisfied |
|- | |- | ||
| 03:45 | | 03:45 | ||
− | | | + | | then we print '''you have entered the number in the range of 0-9.''' |
|- | |- | ||
Line 248: | Line 237: | ||
|- | |- | ||
| 04:03 | | 04:03 | ||
− | |This is “case 1” . “case 1” means “if the value of '''x '''is 1” | + | |This is “case 1”. “case 1” means “if the value of '''x '''is 1”. |
− | + | ||
|- | |- | ||
Line 273: | Line 261: | ||
|- | |- | ||
| 04:36 | | 04:36 | ||
− | | Here we print number not in range. | + | | Here we print "number not in range". |
− | + | ||
|- | |- | ||
| 04:39 | | 04:39 | ||
− | | And This is our return statement. | + | | And This is our '''return''' statement. |
|- | |- | ||
| 04:41 | | 04:41 | ||
| Let us execute the program. | | Let us execute the program. | ||
− | |||
|- | |- | ||
Line 291: | Line 277: | ||
|- | |- | ||
| 04:46 | | 04:46 | ||
− | |Type | + | |Type '''gcc space switch.c space -o space switch'''. Press Enter. |
|- | |- | ||
| 04:55 | | 04:55 | ||
− | | Type | + | | Type '''./switch'''(dot slash switch). Press Enter. |
− | + | ||
|- | |- | ||
| 05:00 | | 05:00 | ||
− | | Enter a number between | + | | Enter a number between 0 to 39. I will enter 35. |
|- | |- | ||
| 05:06 | | 05:06 | ||
− | | The output is displayed as | + | | The output is displayed as “you have entered the number in the range of 30 to 39”. |
|- | |- | ||
| 05:10 | | 05:10 | ||
| Now we will see how to execute the programs in C++. | | Now we will see how to execute the programs in C++. | ||
+ | |||
|- | |- | ||
| 05:16 | | 05:16 | ||
|Switch back to the text editor. | |Switch back to the text editor. | ||
+ | |||
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | Note that our | + | | Note that our file name is '''nested-if.cpp'''. |
+ | |||
|- | |- | ||
| 05:23 | | 05:23 | ||
− | | Here the logic and implementation are same | + | | Here the logic and implementation are same. |
|- | |- | ||
| 05:27 | | 05:27 | ||
| There are a few changes like: | | There are a few changes like: | ||
+ | |||
|- | |- | ||
| 05:30 | | 05:30 | ||
− | |The header file as iostream in place of stdio.h | + | |The header file as "iostream" in place of "stdio.h". |
+ | |||
|- | |- | ||
| 05:35 | | 05:35 | ||
− | |Here we have included the using statement | + | |Here we have included the '''using''' statement |
+ | |||
|- | |- | ||
| 05:39 | | 05:39 | ||
− | |Using namespace std | + | |'''Using namespace std'''. |
|- | |- | ||
| 05:41 | | 05:41 | ||
− | |And the cout and cin function in place of printf and scanf. | + | |And the '''cout''' and '''cin''' function in place of '''printf''' and '''scanf'''. |
+ | |||
|- | |- | ||
| 05:46 | | 05:46 | ||
Line 342: | Line 334: | ||
| 05:51 | | 05:51 | ||
|Let’s execute the code. | |Let’s execute the code. | ||
+ | |||
|- | |- | ||
| 05:53 | | 05:53 | ||
|Come back to the terminal. | |Come back to the terminal. | ||
+ | |||
|- | |- | ||
| 05:56 | | 05:56 | ||
− | |Type | + | |Type '''g++ space nested-if.cpp space -o space nested1'''. Press Enter. |
+ | |||
|- | |- | ||
| 06:07 | | 06:07 | ||
− | |Type | + | |Type '''./nested1'''. Press Enter. |
+ | |||
|- | |- | ||
| 06:11 | | 06:11 | ||
− | | | + | | Enter a number between 0 to 39. I will enter 40. |
+ | |||
|- | |- | ||
| 06:16 | | 06:16 | ||
| The output is displayed as: “number not in range” | | The output is displayed as: “number not in range” | ||
+ | |||
|- | |- | ||
| 06:20 | | 06:20 | ||
− | |Now let’s see the switch program in C++ | + | |Now let’s see the switch program in C++. |
+ | |||
|- | |- | ||
| 06:24 | | 06:24 | ||
− | | | + | | Come back to our text editor. |
|- | |- | ||
| 06:27 | | 06:27 | ||
− | | | + | | Here also the logic and implementation are same. |
+ | |||
|- | |- | ||
| 06:31 | | 06:31 | ||
− | | You can see that the header file is iostream | + | | You can see that the header file is '''iostream'''. |
+ | |||
|- | |- | ||
| 06:34 | | 06:34 | ||
− | | Here is the using statement. | + | | Here is the '''using''' statement. |
+ | |||
|- | |- | ||
| 06:37 | | 06:37 | ||
− | | And we have changed the cout and cin function. | + | | And we have changed the '''cout''' and '''cin''' function. |
+ | |||
|- | |- | ||
| 06:41 | | 06:41 | ||
− | | Rest of the code is similar to our switch.c program | + | | Rest of the code is similar to our switch.c program. |
+ | |||
|- | |- | ||
| 06:45 | | 06:45 | ||
− | | Let us execute. | + | | Let us execute.Come back to our terminal. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 06:48 | | 06:48 | ||
− | |Type | + | |Type '''g++ space switch.cpp space -o space switch1'''. Press Enter. |
|- | |- | ||
| 06:58 | | 06:58 | ||
− | | Type ./switch1. Press Enter | + | | Type '''./switch1'''. Press Enter. |
|- | |- | ||
| 07:02 | | 07:02 | ||
− | | Enter a number between 0 | + | | Enter a number between 0 to 39. |
|- | |- | ||
| 07:05 | | 07:05 | ||
Line 409: | Line 410: | ||
|- | |- | ||
| 07:18 | | 07:18 | ||
− | |We will see the comparison between | + | |We will see the comparison between switch and nested-if statements. |
− | + | ||
|- | |- | ||
Line 417: | Line 417: | ||
|- | |- | ||
| 07:28 | | 07:28 | ||
− | |Netsed-if statement is run | + | |Netsed-if statement is run only if the result of the expression is true. |
|- | |- | ||
| 07:34 | | 07:34 | ||
− | |In switch we treat various values of the variable as cases. | + | |In '''switch''', we treat various values of the variable as '''cases'''. |
|- | |- | ||
| 07:39 | | 07:39 | ||
Line 427: | Line 427: | ||
|- | |- | ||
| 07:45 | | 07:45 | ||
− | | Switch statement can only check the integer values | + | | Switch statement can only check the integer values. |
|- | |- | ||
| 07:50 | | 07:50 | ||
− | | Nested if can check for both integer and fractional values. | + | | Nested if can check for both, integer and fractional values. |
|- | |- | ||
| 07:55 | | 07:55 | ||
Line 441: | Line 441: | ||
|- | |- | ||
| 08:00 | | 08:00 | ||
− | | In this tutorial we learnt | + | | In this tutorial, we learnt * 'nested if' statement. e.g. else if( y/10 equals to 0) |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | |08:08 |
− | | switch statement. | + | |switch statement. e.g. switch(x) and |
− | e.g. | + | |
|- | |- | ||
| 08:12 | | 08:12 | ||
− | | | + | |Difference between nested-if and switch statements. |
− | + | ||
|- | |- | ||
| 08:16 | | 08:16 | ||
− | | As an assignment, | + | | 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 | | 08:26 | ||
− | | It summarizes the Spoken Tutorial project | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
Line 485: | Line 477: | ||
|- | |- | ||
| 08:42 | | 08:42 | ||
− | | For more details please write to | + | | For more details please write to contact @spoken-tutorial.org |
|- | |- | ||
| 08:49 | | 08:49 | ||
− | | Spoken Tutorial Project is a part of the Talk to a Teacher project | + | | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
|- | |- | ||
| 08:52 | | 08:52 | ||
− | | It is supported by the National Mission on Education through ICT, MHRD, Government of India | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 08:58 | | 08:58 | ||
− | | More information on this Mission is available at: [http://spoken-tutorial.org/ http://spoken-tutorial.org]\NMEICT-Intro | + | | More information on this Mission is available at: [http://spoken-tutorial.org/ http://spoken-tutorial.org]\NMEICT-Intro. |
|- | |- |
Latest revision as of 13:04, 24 March 2017
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. |
Contributors and Content Editors
Ashwini, Kavita salve, Krupali, PoojaMoolya, Pratik kamble, Priyacst, Sandhya.np14, Sneha