Difference between revisions of "C-and-C++/C2/If-And-Else-If-statement/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(12 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{| border=1 | {| border=1 | ||
− | || | + | || Time |
− | || | + | || Narration |
|- | |- | ||
− | | 00 | + | | 00:02 |
− | | Welcome to the spoken tutorial on '''Conditional statements in C and C++''' | + | | Welcome to the spoken tutorial on '''Conditional statements in C and C++'''. |
|- | |- | ||
− | | 00 | + | | 00:08 |
− | | In this tutorial we will learn | + | | In this tutorial we will learn: |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:11 |
− | | | + | | How to execute a single '''statement''' |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:14 |
| How to execute group of '''statements'''. | | How to execute group of '''statements'''. | ||
− | |||
|- | |- | ||
− | | 00 | + | | 00:16 |
| We will do this through examples. | | We will do this through examples. | ||
− | |||
|- | |- | ||
− | | 00 | + | | 00:19 |
| We will also see some common errors and their solutions. | | We will also see some common errors and their solutions. | ||
|- | |- | ||
− | | 00 | + | | 00:25 |
− | | To record this tutorial, I am using | + | | To record this tutorial, I am using '''Ubuntu Operating system''' version 11.10. |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:31 |
− | | '''gcc''' and '''g++ Compiler''' version 4.6.1 | + | | '''gcc''' and '''g++ Compiler''' version 4.6.1. |
|- | |- | ||
− | | 00 | + | | 00:38 |
− | | Let us start with the | + | | Let us start with the introduction to conditional '''statements'''. |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:43 |
| A '''statement''' in a program controls the flow of program execution. | | A '''statement''' in a program controls the flow of program execution. | ||
− | |||
|- | |- | ||
− | | 00 | + | | 00:49 |
− | | It helps to make decision on | + | | It helps to make decision on what code is to be executed. |
− | + | ||
|- | |- | ||
− | | 00 | + | | 00:55 |
|We can check the conditions, whether true or false. | |We can check the conditions, whether true or false. | ||
− | |||
|- | |- | ||
− | | 01 | + | | 01:00 |
| We can execute a single statement or a group of statements. | | We can execute a single statement or a group of statements. | ||
|- | |- | ||
− | | 01 | + | | 01:07 |
− | | | + | | Let us understand the flow of '''if''' statements. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:13 |
− | | Here, if the condition is true then | + | | Here, if the condition is true then '''statement 1''' will be executed. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:20 |
− | | If the condition is false then ''' | + | | If the condition is false then '''statement 2''' will be executed. |
|- | |- | ||
− | | 01 | + | | 01:29 |
− | | | + | | Now we will see the flow of '''else if''' statement. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:32 |
− | | Here, if ''' | + | | Here, if '''condition 1''' is true then '''statement 1''' will be executed. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:41 |
− | | If ''' | + | | If '''condition 1''' is false then it will check for another condition that is '''condition 2'''. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:49 |
− | | If ''' | + | | If '''condition 2''' is true then '''statement 3''' will be executed. |
− | + | ||
|- | |- | ||
− | | 01 | + | | 01:54 |
− | | And If ''' | + | | And If '''condition 2''' is false then '''statement 2''' will be executed. |
|- | |- | ||
− | | 02 | + | | 02:02 |
| Now Let us move on to our program. | | Now Let us move on to our program. | ||
− | |||
|- | |- | ||
− | | 02 | + | | 02:06 |
| I have already typed the code on the editor. | | I have already typed the code on the editor. | ||
− | |||
|- | |- | ||
− | | 02 | + | | 02:09 |
− | | So let me open it. | + | | So, let me open it. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:13 |
− | | Note that our | + | | Note that our file name is '''ifstmt.c'''. |
|- | |- | ||
− | | 02 | + | | 02:18 |
| In this program we will calculate the sum of two numbers and will check a few conditions. | | In this program we will calculate the sum of two numbers and will check a few conditions. | ||
|- | |- | ||
− | | 02 | + | | 02:26 |
| Let me explain the code now. | | Let me explain the code now. | ||
− | |||
|- | |- | ||
− | | 02 | + | | 02:30 |
| This is our '''header file.''' | | This is our '''header file.''' | ||
|- | |- | ||
− | | 02 | + | | 02:34 |
− | | This is our '''main | + | | This is our '''main()''' function. |
|- | |- | ||
− | | 02 | + | | 02:38 |
− | | Here we have declared three | + | | Here we have declared three '''integer variables''' '''a, b''' and '''sum.''' |
|- | |- | ||
− | | 02 | + | | 02:46 |
− | | | + | | Here we are asking for user input. |
− | + | ||
|- | |- | ||
− | | 02 | + | | 02:49 |
| User will enter the values of a and b. | | User will enter the values of a and b. | ||
− | |||
|- | |- | ||
− | | 02 | + | | 02:52 |
| The values will be stored in '''variable a''' and '''variable b.''' | | The values will be stored in '''variable a''' and '''variable b.''' | ||
|- | |- | ||
− | | 02 | + | | 02:58 |
− | |The 'scanf()''' reads data from the | + | |The '''scanf()''' function reads data from the console. |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:02 |
| It then stores the result in the given '''variable.''' | | It then stores the result in the given '''variable.''' | ||
− | |||
|- | |- | ||
− | | 03 | + | | 03:06 |
− | | The''' format specifier '''in the''' scanf() '''helps to know the type of data. | + | | The '''format specifier''' in the '''scanf()''' helps to know the type of data. |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:10 |
− | | Like here we have %d it denotes that we are dealing with integer data type. | + | | Like here we have '''%d''', it denotes that we are dealing with integer data type. |
|- | |- | ||
− | | 03 | + | | 03:18 |
− | | Here we add the values of''' a''' and '''b.''' | + | | Here we add the values of '''a''' and '''b.''' |
− | + | ||
|- | |- | ||
− | | 03 | + | | 03:22 |
| We will store the result in '''sum.''' | | We will store the result in '''sum.''' | ||
|- | |- | ||
− | | 03 | + | | 03:25 |
| Then we print the result. | | Then we print the result. | ||
|- | |- | ||
− | | 03 | + | | 03:29 |
− | | This is our '''if | + | | This is our '''if''' statement. |
|- | |- | ||
− | | 03 | + | | 03:30 |
|Here, we check the condition whether '''sum''' is greater than 20. | |Here, we check the condition whether '''sum''' is greater than 20. | ||
|- | |- | ||
− | | 03 | + | | 03:36 |
|If the condition is true, then we print '''Sum''' is greater than 20. | |If the condition is true, then we print '''Sum''' is greater than 20. | ||
|- | |- | ||
− | | 03 | + | | 03:42 |
− | | Now let me comment out | + | | Now let me comment out these lines. |
|- | |- | ||
− | |03 | + | |03:48 |
− | |This is our return statement. | + | |This is our '''return''' statement. |
|- | |- | ||
− | | 03 | + | | 03:51 |
− | | | + | | Now click on '''Save'''. |
|- | |- | ||
− | | 03 | + | | 03:53 |
− | |First we will see the execution of if statement. | + | |First we will see the execution of '''if''' statement. |
|- | |- | ||
− | | 03 | + | | 03:58 |
− | | | + | | Please open the terminal window by pressing '''Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard. |
|- | |- | ||
− | |04 | + | |04:09 |
− | | | + | | To compile, type '''gcc space ifstmt dot c space -o space if''' and press Enter. |
|- | |- | ||
− | | 04 | + | | 04:20 |
− | | | + | | To execute, type '''./if''' (dot slash if). Press Enter. |
|- | |- | ||
− | | 04 | + | | 04:26 |
− | | | + | | It is displayed as '''Enter the values of a and b'''. |
|- | |- | ||
− | | 04 | + | | 04:31 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|I will give the values as 10 and 12. | |I will give the values as 10 and 12. | ||
|- | |- | ||
− | | 04 | + | | 04:38 |
− | |The output is displayed as Sum of a and b is 22. | + | |The output is displayed as '''Sum of a and b is 22. Sum is greater than 20'''. |
|- | |- | ||
− | | 04 | + | | 04:45 |
| Now come back to our program. | | Now come back to our program. | ||
|- | |- | ||
− | | 04 | + | | 04:48 |
− | | | + | | We will check another condition. |
|- | |- | ||
− | | 04 | + | | 04:52 |
|Let me remove the comment from here. | |Let me remove the comment from here. | ||
|- | |- | ||
− | | 04 | + | | 04:56 |
|I will give the comment here. | |I will give the comment here. | ||
|- | |- | ||
− | | 05 | + | | 05:00 |
− | |Now click on '''Save.''' | + | |Now click on '''Save.''' This is our '''else-if''' statement. |
|- | |- | ||
− | | 05 | + | | 05:05 |
− | | | + | |Here, we check another condition whether '''Sum''' is greater than 10. |
|- | |- | ||
− | | 05 | + | | 05:11 |
− | | | + | |If the condition is true then we print 'Sum is greater than 10 and less than 20'. |
|- | |- | ||
− | | | + | | 05:18 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
| Now come back to our terminal. | | Now come back to our terminal. | ||
|- | |- | ||
− | | 05 | + | | 05:20 |
− | |Let us compile as before. Let us execute as before. | + | |Let us compile as before. |
+ | |- | ||
+ | | 05:23 | ||
+ | |Let us execute as before. | ||
|- | |- | ||
− | | 05 | + | | 05:26 |
− | | It is displayed as | + | | It is displayed as |
|- | |- | ||
− | | 05 | + | | 05:28 |
− | |Enter the value of a and b. | + | |'''Enter the value of a and b'''. |
|- | |- | ||
− | | 05 | + | | 05:30 |
| I will give the values as 10 and 2. | | I will give the values as 10 and 2. | ||
|- | |- | ||
− | | 05 | + | | 05:35 |
− | |The output is displayed as: Sum of a and b is 12. | + | |The output is displayed as: '''Sum of a and b is 12'''. |
|- | |- | ||
− | | 05 | + | | 05:38 |
− | |Sum is greater than 10 and less than 20. | + | |'''Sum is greater than 10 and less than 20'''. |
|- | |- | ||
− | | 05 | + | | 05:42 |
− | |Let me clear the prompt | + | |Let me clear the prompt. |
|- | |- | ||
− | | | + | | 05:44 |
− | | | + | | Now come back to our program. |
|- | |- | ||
− | | 05 | + | | 05:48 |
− | | | + | | I will remove the comment from here and here. Now click on Save. |
|- | |- | ||
− | | | + | | 05:56 |
− | | | + | | If both the above conditions are false then we print 'Sum is less than 10'. |
|- | |- | ||
− | | 06 | + | | 06:04 |
− | | | + | |This is our '''else''' statement. |
|- | |- | ||
− | | 06 | + | | 06:07 |
− | | | + | | Now let us execute and see. Come back to our terminal. |
|- | |- | ||
− | | | + | | 06:11 |
− | | | + | |Let us compile as before. Let us execute as before. |
|- | |- | ||
− | | 06 | + | | 06:18 |
− | |Enter the value of a and b. | + | | Here it is displayed as '''Enter the value of a and b'''. |
|- | |- | ||
− | | 06 | + | | 06:22 |
|I will give the values as 3 and 5. | |I will give the values as 3 and 5. | ||
|- | |- | ||
− | | 06 | + | | 06:27 |
− | | | + | | The output is displayed as '''sum of a and b is 8'''. |
|- | |- | ||
− | | 06 | + | | 06:31 |
− | |Sum is less than 10. | + | |'''Sum is less than 10'''. |
|- | |- | ||
− | | 06 | + | | 06:34 |
| Now we will see some common errors which we can come across. | | Now we will see some common errors which we can come across. | ||
|- | |- | ||
− | | 06 | + | | 06:38 |
|Come back to our program. | |Come back to our program. | ||
|- | |- | ||
− | | 06 | + | | 06:41 |
− | | Suppose, here at the end of if statement I will type a semicolon. | + | | Suppose, here at the end of '''if''' statement I will type a semicolon (;). |
|- | |- | ||
− | | 06 | + | | 06:47 |
− | |Let see what will happen.Click on '''Save.''' | + | |Let us see what will happen. Click on '''Save.''' |
− | + | ||
− | + | ||
|- | |- | ||
− | | 06 | + | | 06:50 |
| Let us execute. Come back to our terminal. | | Let us execute. Come back to our terminal. | ||
|- | |- | ||
− | | 06 | + | | 06:53 |
|Let us compile as before. | |Let us compile as before. | ||
|- | |- | ||
− | | 06 | + | | 06:56 |
− | | We see an error: else without a previous if | + | | We see an error: ''' 'else' without a previous 'if' ''' |
|- | |- | ||
− | | 07 | + | | 07:02 |
− | | Come back to our program.It is | + | | Come back to our program. It is a syntax error. |
|- | |- | ||
− | | 07 | + | | 07:07 |
− | | | + | |'''if''' statement will never terminate with a semicolon. |
|- | |- | ||
− | | 07 | + | | 07:10 |
− | |And the else if statement will never work without an if. | + | |And the '''else if''' statement will never work without an '''if'''. |
|- | |- | ||
− | | 07 | + | | 07:16 |
− | | Let us fix the error. Delete the semicolon ; here | + | | Let us fix the error. Delete the semicolon (;) here. |
|- | |- | ||
− | | 07 | + | | 07:22 |
| Now Click on '''Save.''' | | Now Click on '''Save.''' | ||
|- | |- | ||
− | | 07 | + | | 07:25 |
| Let us execute. Come back to the terminal. | | Let us execute. Come back to the terminal. | ||
|- | |- | ||
− | | 07 | + | | 07:29 |
| Let us compile as before. Let us execute as before. | | Let us compile as before. Let us execute as before. | ||
|- | |- | ||
− | | 07 | + | | 07:35 |
− | |Enter the value of a and b | + | |Enter the value of a and b. |
|- | |- | ||
− | | 07 | + | | 07:37 |
|I will give the values as 3 and 6. | |I will give the values as 3 and 6. | ||
|- | |- | ||
− | | 07 | + | | 07:43 |
|The output is displayed as | |The output is displayed as | ||
|- | |- | ||
− | | 07 | + | | 07:45 |
− | |Sum of a and b is 9. Sum is less than 10. | + | |'''Sum of a and b is 9. Sum is less than 10'''. |
|- | |- | ||
− | | 07 | + | | 07:52 |
− | + | | NOW WE WILL SEE HOW TO EXECUTE THE SAME PROGRAM IN C++. | |
|- | |- | ||
− | | 07 | + | | 07:57 |
|Come back to our program. | |Come back to our program. | ||
|- | |- | ||
− | | | + | | 07:59 |
− | | | + | | I will change a few things here. |
|- | |- | ||
− | | 08 | + | | 08:03 |
− | | Press '''Shift, Ctrl and S''' keys simultaneously on your keyboard. | + | | Press '''Shift, Ctrl''' and '''S''' keys simultaneously on your keyboard. |
|- | |- | ||
− | | 08 | + | | 08:11 |
− | |Now save the file with an extension | + | |Now save the file with an extension '''dot cpp''' and click on ''' Save'''. |
|- | |- | ||
− | | 08 | + | | 08:20 |
− | | | + | | We will change the header file as '''iostream'''. |
|- | |- | ||
− | | 08 | + | | 08:26 |
| Let us include the '''using '''statement here. | | Let us include the '''using '''statement here. | ||
|- | |- | ||
− | | 08 | + | | 08:30 |
− | | Now click on the | + | | Now click on the Search for and replace text option. |
|- | |- | ||
− | | 08 | + | | 08:35 |
− | | | + | |Let us replace the '''printf '''statement with the '''cout''' statement. |
|- | |- | ||
− | | 08 | + | | 08:40 |
− | |Click on '''Replace all''' and click on | + | |Click on '''Replace all''' and click on Close. |
− | + | ||
− | + | ||
|- | |- | ||
− | | 08 | + | | 08:46 |
− | | Now delete the | + | | Now delete the closing brackets here. |
|- | |- | ||
− | | 08 | + | | 08:49 |
− | |Replace the '''scanf''' statement with the '''cin''' statement. | + | |Replace the '''scanf()''' statement with the '''cin''' statement. |
|- | |- | ||
− | | 08 | + | | 08:54 |
− | |Type '''cin and two closing angle brackets >>''' | + | |Type '''cin''' and '''two closing angle brackets >>'''. |
|- | |- | ||
− | | 09 | + | | 09:00 |
− | |As we use '''cin >> | + | |As we use '''cin >>''' function to read a line in C++. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:05 |
| Now delete the '''format specifiers'''. | | Now delete the '''format specifiers'''. | ||
|- | |- | ||
− | | 09 | + | | 09:09 |
− | |Delete the '''comma''' and '''ampersand | + | |Delete the '''comma''' and '''ampersand &'''. |
|- | |- | ||
− | | 09 | + | | 09:12 |
|Delete the comma here and type two closing angle brackets. | |Delete the comma here and type two closing angle brackets. | ||
|- | |- | ||
− | | 09 | + | | 09:17 |
− | |Again delete the '''ampersand'''and the closing brackets. Now | + | |Again delete the '''ampersand'''and the closing brackets. Now click on '''Save'''. |
|- | |- | ||
− | | 09 | + | | 09:25 |
| Here delete the closing bracket and the comma. | | Here delete the closing bracket and the comma. | ||
|- | |- | ||
− | | 09 | + | | 09:31 |
− | |Now delete the | + | |Now delete the '''backslash n (\n)''' and '''format specifier'''. |
− | + | ||
|- | |- | ||
− | | 09 | + | | 09:37 |
− | | Now | + | | Now type two opening brackets. |
|- | |- | ||
− | | 09 | + | | 09:42 |
− | |Again type two opening angle brackets and''' | + | |Again type two opening angle brackets and '''within the double quotes type backslash n “\n”.''' |
|- | |- | ||
− | | 09 | + | | 09:49 |
|Here also we will delete the closing bracket. | |Here also we will delete the closing bracket. | ||
|- | |- | ||
− | | 09 | + | | 09:53 |
|Now again delete the closing bracket here and here. | |Now again delete the closing bracket here and here. | ||
|- | |- | ||
− | | 09 | + | | 09:59 |
− | |Now | + | |Now click on '''Save'''. |
|- | |- | ||
− | | 10 | + | | 10:02 |
| Let us execute. | | Let us execute. | ||
|- | |- | ||
− | | 10 | + | | 10:04 |
− | || Come back to the terminal. | + | || Come back to the terminal. Let me clear the prompt. |
|- | |- | ||
− | | 10 | + | | 10:10 |
− | |To compile | + | |To compile, type '''g++ space ifstmt.cpp space -o space if1''' |
|- | |- | ||
− | | 10 | + | | 10:20 |
− | |Here we have | + | |Here we have 'if1' because we don't want to overwrite the output parameter '''if '''for the file 'ifstmt.c'. |
|- | |- | ||
− | | 10 | + | | 10:31 |
− | |Now Press '''Enter''' | + | |Now Press 'Enter'.To execute type '''./if1''' (dot slash if1) and press '''Enter'''. |
|- | |- | ||
− | | 10 | + | | 10:39 |
− | | | + | |Enter the value of '''a''' and '''b'''. I will give the values as 20 and 10. |
|- | |- | ||
− | | 10 | + | | 10:48 |
− | | | + | |The output is displayed as '''Sum of a and b is 30''' |
|- | |- | ||
− | | 10 | + | | 10:52 |
− | | | + | |'''Sum is greater than 20''' |
|- | |- | ||
− | | 10 | + | | 10:56 |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|This brings us to the end of this tutorial. | |This brings us to the end of this tutorial. | ||
|- | |- | ||
− | | 10 | + | | 10:59 |
| Now come back to our slides. | | Now come back to our slides. | ||
+ | |||
|- | |- | ||
− | | 11 | + | | 11:02 |
| Let us summarize. | | Let us summarize. | ||
|- | |- | ||
− | | 11 | + | | 11:04 |
− | |In this tutorial we learned | + | |In this tutorial we learned 'if' statement eg. if(condition), |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | 11 | + | | 11:11 |
− | | | + | | And 'else if' statement eg. else if(condition). |
|- | |- | ||
− | | 11 | + | | 11:17 |
− | |Write a program to check | + | | As an assignment, Write a program to check 'a' is greater than 'b' or less than 'b'. |
|- | |- | ||
− | | 11 | + | | 11:24 |
− | | | + | |Hint: use 'if' statement. |
|- | |- | ||
− | | 11 | + | | 11:28 |
− | |Write another program to check which value is greater '' | + | |Write another program to check which value is greater 'a', 'b' or 'c'. |
|- | |- | ||
− | | 11 | + | | 11:34 |
− | | | + | |Hint: use 'else-if' statement. |
|- | |- | ||
− | | 11 | + | | 11:38 |
− | | Watch the video available at the link shown below. | + | | Watch the video available at the link shown below. |
|- | |- | ||
− | | 11 | + | | 11:41 |
|It summarizes the Spoken Tutorial project. | |It summarizes the Spoken Tutorial project. | ||
|- | |- | ||
− | | 11 | + | | 11:44 |
|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. | ||
|- | |- | ||
− | |11 | + | |11:48 |
|| The Spoken Tutorial Project Team, | || The Spoken Tutorial Project Team, | ||
|- | |- | ||
− | | 11 | + | | 11:50 |
|Conducts workshops using spoken tutorials. | |Conducts workshops using spoken tutorials. | ||
|- | |- | ||
− | | 11 | + | | 11:54 |
|Gives certificates to those who pass an online test. | |Gives certificates to those who pass an online test. | ||
|- | |- | ||
− | | 11 | + | | 11:57 |
− | |For more details, please write to, | + | |For more details, please write to, contact @ spoken hyphen tutorial dot org. |
|- | |- | ||
− | | 12 | + | | 12:04 |
| 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. | ||
|- | |- | ||
− | | 12 | + | | 12:09 |
|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. | ||
|- | |- | ||
− | | 12 | + | | 12:15 |
− | |More information on this Mission is available at the link shown below. | + | |More information on this Mission is available at the link shown below. |
|- | |- | ||
− | | 12 | + | | 12:20 |
− | | This is Ashwini Patil from IIT Bombay | + | | This is Ashwini Patil from IIT Bombay. |
Thank You for watching. | Thank You for watching. | ||
|} | |} |
Latest revision as of 11:17, 5 September 2017
Time | Narration |
00:02 | Welcome to the spoken tutorial on Conditional statements in C and C++. |
00:08 | In this tutorial we will learn: |
00:11 | How to execute a single statement |
00:14 | How to execute group of statements. |
00:16 | We will do this through examples. |
00:19 | We will also see some common errors and their solutions. |
00:25 | To record this tutorial, I am using Ubuntu Operating system version 11.10. |
00:31 | gcc and g++ Compiler version 4.6.1. |
00:38 | Let us start with the introduction to conditional statements. |
00:43 | A statement in a program controls the flow of program execution. |
00:49 | It helps to make decision on what code is to be executed. |
00:55 | We can check the conditions, whether true or false. |
01:00 | We can execute a single statement or a group of statements. |
01:07 | Let us understand the flow of if statements. |
01:13 | Here, if the condition is true then statement 1 will be executed. |
01:20 | If the condition is false then statement 2 will be executed. |
01:29 | Now we will see the flow of else if statement. |
01:32 | Here, if condition 1 is true then statement 1 will be executed. |
01:41 | If condition 1 is false then it will check for another condition that is condition 2. |
01:49 | If condition 2 is true then statement 3 will be executed. |
01:54 | And If condition 2 is false then statement 2 will be executed. |
02:02 | Now Let us move on to our program. |
02:06 | I have already typed the code on the editor. |
02:09 | So, let me open it. |
02:13 | Note that our file name is ifstmt.c. |
02:18 | In this program we will calculate the sum of two numbers and will check a few conditions. |
02:26 | Let me explain the code now. |
02:30 | This is our header file. |
02:34 | This is our main() function. |
02:38 | Here we have declared three integer variables a, b and sum. |
02:46 | Here we are asking for user input. |
02:49 | User will enter the values of a and b. |
02:52 | The values will be stored in variable a and variable b. |
02:58 | The scanf() function reads data from the console. |
03:02 | It then stores the result in the given variable. |
03:06 | The format specifier in the scanf() helps to know the type of data. |
03:10 | Like here we have %d, it denotes that we are dealing with integer data type. |
03:18 | Here we add the values of a and b. |
03:22 | We will store the result in sum. |
03:25 | Then we print the result. |
03:29 | This is our if statement. |
03:30 | Here, we check the condition whether sum is greater than 20. |
03:36 | If the condition is true, then we print Sum is greater than 20. |
03:42 | Now let me comment out these lines. |
03:48 | This is our return statement. |
03:51 | Now click on Save. |
03:53 | First we will see the execution of if statement. |
03:58 | Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
04:09 | To compile, type gcc space ifstmt dot c space -o space if and press Enter. |
04:20 | To execute, type ./if (dot slash if). Press Enter. |
04:26 | It is displayed as Enter the values of a and b. |
04:31 | I will give the values as 10 and 12. |
04:38 | The output is displayed as Sum of a and b is 22. Sum is greater than 20. |
04:45 | Now come back to our program. |
04:48 | We will check another condition. |
04:52 | Let me remove the comment from here. |
04:56 | I will give the comment here. |
05:00 | Now click on Save. This is our else-if statement. |
05:05 | Here, we check another condition whether Sum is greater than 10. |
05:11 | If the condition is true then we print 'Sum is greater than 10 and less than 20'. |
05:18 | Now come back to our terminal. |
05:20 | Let us compile as before. |
05:23 | Let us execute as before. |
05:26 | It is displayed as |
05:28 | Enter the value of a and b. |
05:30 | I will give the values as 10 and 2. |
05:35 | The output is displayed as: Sum of a and b is 12. |
05:38 | Sum is greater than 10 and less than 20. |
05:42 | Let me clear the prompt. |
05:44 | Now come back to our program. |
05:48 | I will remove the comment from here and here. Now click on Save. |
05:56 | If both the above conditions are false then we print 'Sum is less than 10'. |
06:04 | This is our else statement. |
06:07 | Now let us execute and see. Come back to our terminal. |
06:11 | Let us compile as before. Let us execute as before. |
06:18 | Here it is displayed as Enter the value of a and b. |
06:22 | I will give the values as 3 and 5. |
06:27 | The output is displayed as sum of a and b is 8. |
06:31 | Sum is less than 10. |
06:34 | Now we will see some common errors which we can come across. |
06:38 | Come back to our program. |
06:41 | Suppose, here at the end of if statement I will type a semicolon (;). |
06:47 | Let us see what will happen. Click on Save. |
06:50 | Let us execute. Come back to our terminal. |
06:53 | Let us compile as before. |
06:56 | We see an error: 'else' without a previous 'if' |
07:02 | Come back to our program. It is a syntax error. |
07:07 | if statement will never terminate with a semicolon. |
07:10 | And the else if statement will never work without an if. |
07:16 | Let us fix the error. Delete the semicolon (;) here. |
07:22 | Now Click on Save. |
07:25 | Let us execute. Come back to the terminal. |
07:29 | Let us compile as before. Let us execute as before. |
07:35 | Enter the value of a and b. |
07:37 | I will give the values as 3 and 6. |
07:43 | The output is displayed as |
07:45 | Sum of a and b is 9. Sum is less than 10. |
07:52 | NOW WE WILL SEE HOW TO EXECUTE THE SAME PROGRAM IN C++. |
07:57 | Come back to our program. |
07:59 | I will change a few things here. |
08:03 | Press Shift, Ctrl and S keys simultaneously on your keyboard. |
08:11 | Now save the file with an extension dot cpp and click on Save. |
08:20 | We will change the header file as iostream. |
08:26 | Let us include the using statement here. |
08:30 | Now click on the Search for and replace text option. |
08:35 | Let us replace the printf statement with the cout statement. |
08:40 | Click on Replace all and click on Close. |
08:46 | Now delete the closing brackets here. |
08:49 | Replace the scanf() statement with the cin statement. |
08:54 | Type cin and two closing angle brackets >>. |
09:00 | As we use cin >> function to read a line in C++. |
09:05 | Now delete the format specifiers. |
09:09 | Delete the comma and ampersand &. |
09:12 | Delete the comma here and type two closing angle brackets. |
09:17 | Again delete the ampersandand the closing brackets. Now click on Save. |
09:25 | Here delete the closing bracket and the comma. |
09:31 | Now delete the backslash n (\n) and format specifier. |
09:37 | Now type two opening brackets. |
09:42 | Again type two opening angle brackets and within the double quotes type backslash n “\n”. |
09:49 | Here also we will delete the closing bracket. |
09:53 | Now again delete the closing bracket here and here. |
09:59 | Now click on Save. |
10:02 | Let us execute. |
10:04 | Come back to the terminal. Let me clear the prompt. |
10:10 | To compile, type g++ space ifstmt.cpp space -o space if1 |
10:20 | Here we have 'if1' because we don't want to overwrite the output parameter if for the file 'ifstmt.c'. |
10:31 | Now Press 'Enter'.To execute type ./if1 (dot slash if1) and press Enter. |
10:39 | Enter the value of a and b. I will give the values as 20 and 10. |
10:48 | The output is displayed as Sum of a and b is 30 |
10:52 | Sum is greater than 20 |
10:56 | This brings us to the end of this tutorial. |
10:59 | Now come back to our slides. |
11:02 | Let us summarize. |
11:04 | In this tutorial we learned 'if' statement eg. if(condition), |
11:11 | And 'else if' statement eg. else if(condition). |
11:17 | As an assignment, Write a program to check 'a' is greater than 'b' or less than 'b'. |
11:24 | Hint: use 'if' statement. |
11:28 | Write another program to check which value is greater 'a', 'b' or 'c'. |
11:34 | Hint: use 'else-if' statement. |
11:38 | Watch the video available at the link shown below. |
11:41 | It summarizes the Spoken Tutorial project. |
11:44 | If you do not have good bandwidth, you can download and watch it. |
11:48 | The Spoken Tutorial Project Team, |
11:50 | Conducts workshops using spoken tutorials. |
11:54 | Gives certificates to those who pass an online test. |
11:57 | For more details, please write to, contact @ spoken hyphen tutorial dot org. |
12:04 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
12:09 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
12:15 | More information on this Mission is available at the link shown below. |
12:20 | This is Ashwini Patil from IIT Bombay.
Thank You for watching. |
Contributors and Content Editors
Ashwini, Devraj, Kavita salve, Krupali, PoojaMoolya, Priyacst, Sakinashaikh, Sandhya.np14, Sneha