Difference between revisions of "C-and-C++/C2/If-And-Else-If-statement/English-timed"
From Script | Spoken-Tutorial
Sakinashaikh (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
Line 8: | Line 8: | ||
|- | |- | ||
− | | 00. | + | | 00.08 |
| In this tutorial we will learn, | | In this tutorial we will learn, | ||
Line 23: | Line 23: | ||
|- | |- | ||
− | | 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. | ||
Line 37: | Line 37: | ||
|- | |- | ||
− | | 00. | + | | 00.31 |
| '''gcc''' and '''g++ Compiler''' version 4.6.1 | | '''gcc''' and '''g++ Compiler''' version 4.6.1 | ||
Line 51: | Line 51: | ||
|- | |- | ||
− | | 00. | + | | 00.49 |
| It helps to make decision on, what code is to be executed. | | 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. | | Let us understand the flow of if statements. | ||
Line 94: | Line 94: | ||
|- | |- | ||
− | | 01. | + | | 01.49 |
| If '''condition2''' is true, then '''statement3''' will be executed. | | If '''condition2''' is true, then '''statement3''' will be executed. | ||
|- | |- | ||
− | | 01. | + | | 01.54 |
| And If '''condition2''' is false, then '''statement2''' will be executed | | And If '''condition2''' is false, then '''statement2''' will be executed | ||
|- | |- | ||
− | | 02. | + | | 02.02 |
| Now Let us move on to our program. | | Now Let us move on to our program. | ||
Line 122: | Line 122: | ||
|- | |- | ||
− | | 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.''' | ||
Line 143: | Line 143: | ||
|- | |- | ||
− | | 02. | + | | 02.46 |
| Here we are asking for user input. | | Here we are asking for user input. | ||
Line 158: | Line 158: | ||
|- | |- | ||
| 02.58 | | 02.58 | ||
− | |The 'scanf()''' reads data from the '''console.''' | + | |The 'scanf()''' function reads data from the '''console.''' |
Line 172: | Line 172: | ||
|- | |- | ||
− | | 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.''' | ||
Line 185: | Line 185: | ||
|- | |- | ||
− | | 03. | + | | 03.25 |
| Then we print the result. | | Then we print the result. | ||
Line 193: | Line 193: | ||
|- | |- | ||
− | | 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. | ||
Line 201: | Line 201: | ||
|- | |- | ||
− | | 03. | + | | 03.42 |
| Now let me comment out these lines. | | Now let me comment out these lines. | ||
Line 222: | Line 222: | ||
|- | |- | ||
|04.09 | |04.09 | ||
− | | To compile type, '''gcc ifstmt | + | | To compile type, '''gcc space ifstmt dot c space -o space if'''and press enter |
|- | |- | ||
Line 237: | Line 237: | ||
|- | |- | ||
− | | 04. | + | | 04.31 |
|I will give the values as 10 and 12. | |I will give the values as 10 and 12. | ||
Line 249: | Line 249: | ||
|- | |- | ||
− | | 04. | + | | 04.48 |
| We will check another condition. | | 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. | ||
Line 274: | Line 274: | ||
|- | |- | ||
| 05.11 | | 05.11 | ||
− | |If the condition is true | + | |If the condition is true then we print '''Sum''' is greater than 10 and less than 20. |
|- | |- | ||
Line 288: | Line 288: | ||
|- | |- | ||
− | | 05. | + | | 05.26 |
| It is displayed as, | | It is displayed as, | ||
Line 303: | Line 303: | ||
|- | |- | ||
− | | 05. | + | | 05.38 |
|Sum is greater than 10 and less than 20. | |Sum is greater than 10 and less than 20. | ||
Line 317: | Line 317: | ||
|- | |- | ||
| 05.48 | | 05.48 | ||
− | | I will remove the comment from here and here.Now click on save, | + | | I will remove the comment from here and here. Now click on save, |
|- | |- | ||
Line 328: | Line 328: | ||
|- | |- | ||
− | | 06. | + | | 06.07 |
| Now let us execute and see. come back to our terminal. | | Now let us execute and see. come back to our terminal. | ||
|- | |- | ||
− | | 06. | + | | 06.11 |
|Let us compile as before. Let us execute as before. | |Let us compile as before. Let us execute as before. | ||
Line 369: | Line 369: | ||
|- | |- | ||
| 06.47 | | 06.47 | ||
− | |Let see what will happen.Click on '''Save.''' | + | |Let see what will happen. Click on '''Save.''' |
Line 422: | Line 422: | ||
|- | |- | ||
− | | 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. | ||
Line 438: | Line 438: | ||
|- | |- | ||
− | | | + | | 07.59 |
| I will change a few things here. | | I will change a few things here. | ||
Line 447: | Line 447: | ||
|- | |- | ||
| 08.11 | | 08.11 | ||
− | |Now save the file with an extension | + | |Now save the file with an extension ''' dot cpp''' and''' '''click''' '''on''' Save''' |
|- | |- | ||
Line 461: | Line 461: | ||
| Now click on the search for and replace text option . | | Now click on the search for and replace text option . | ||
|- | |- | ||
− | | 08. | + | | 08.35 |
− | | | + | |Let us replace the '''printf '''statement with the '''cout''' '''statement.''' |
|- | |- | ||
Line 475: | Line 475: | ||
|- | |- | ||
− | | 08. | + | | 08.49 |
|Replace the '''scanf''' statement with the '''cin''' statement. | |Replace the '''scanf''' statement with the '''cin''' statement. | ||
Line 488: | Line 488: | ||
|- | |- | ||
− | | 09. | + | | 09.05 |
| Now delete the '''format specifiers'''. | | Now delete the '''format specifiers'''. | ||
Line 508: | Line 508: | ||
|- | |- | ||
− | | 09. | + | | 09.31 |
|Now delete the ''''backslash n'''' and ''''format specifier''' | |Now delete the ''''backslash n'''' and ''''format specifier''' | ||
' | ' | ||
Line 524: | Line 524: | ||
|- | |- | ||
− | | 09. | + | | 09.53 |
|Now again delete the closing bracket here and here. | |Now again delete the closing bracket here and here. | ||
Line 532: | Line 532: | ||
|- | |- | ||
− | | 10. | + | | 10.02 |
| Let us execute. | | Let us execute. | ||
|- | |- | ||
| 10.04 | | 10.04 | ||
− | || Come back to the terminal. | + | || Come back to the terminal .Let me clear the prompt. |
|- | |- | ||
Line 544: | Line 544: | ||
|- | |- | ||
− | | 10. | + | | 10.20 |
|Here we have '''if1''' because we don't want to overwrite the output parameter '''if '''for the file ifstmt.c | |Here we have '''if1''' because we don't want to overwrite the output parameter '''if '''for the file ifstmt.c | ||
Line 552: | Line 552: | ||
|- | |- | ||
− | | 10. | + | | 10.32 |
|To execute type ./if1 and Press '''Enter''' | |To execute type ./if1 and Press '''Enter''' | ||
Line 564: | Line 564: | ||
|- | |- | ||
− | | 10. | + | | 10.52 |
|Sum is greater than 20. | |Sum is greater than 20. | ||
Line 575: | Line 575: | ||
| Now come back to our slides. | | Now come back to our slides. | ||
|- | |- | ||
− | | 11. | + | | 11.02 |
| Let us summarize. | | Let us summarize. | ||
Line 582: | Line 582: | ||
|In this tutorial we learned, if statement eg. if(condition) | |In this tutorial we learned, if statement eg. if(condition) | ||
− | |||
− | |||
− | |||
|- | |- | ||
− | | 11. | + | | 11.11 |
| And else if statement eg. else if(condition) | | And else if statement eg. else if(condition) | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | 11. | + | | 11.17 |
| As an assignment, | | As an assignment, | ||
|- | |- | ||
− | | 11. | + | | 11.18 |
|Write a program to check '''a''' is greater than '''b''' or less than '''b.''' | |Write a program to check '''a''' is greater than '''b''' or less than '''b.''' | ||
Line 615: | Line 608: | ||
|- | |- | ||
− | | 11. | + | | 11.38 |
− | | Watch the video available at the link shown below. | + | | Watch the video available at the link shown below. |
|- | |- | ||
Line 627: | Line 620: | ||
|- | |- | ||
− | |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. | ||
Line 639: | Line 632: | ||
|- | |- | ||
− | | 11. | + | | 11.57 |
− | |For more details, please write to, contact | + | |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. | ||
Line 651: | Line 644: | ||
|- | |- | ||
− | | 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 | + | | This is Ashwini Patil from IIT BombayA |
Thank You for watching. | Thank You for watching. | ||
|} | |} |
Revision as of 11:35, 5 February 2014
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 condition 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 , statement1 will be executed.
|
01.20 | If the condition is false then statement2 will be executed. |
01.29 | Now we will see the flow of else if statement,
|
01.32 | Here, if condition1 is true then statement1 will be executed.
|
01.41 | If condition1 is false then it will check for another condition that is condition2.
|
01.49 | If condition2 is true, then statement3 will be executed.
|
01.54 | And If condition2 is false, then statement2 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 filename 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 ifand press enter |
04.20 | To execute type, ./if press enter |
04.26 | it is displayed as, |
04.27 | Enter the value 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. |
05.03 | 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, |
06.19 | Enter the value of a and b. |
06.22 | I will give the values as 3 and 5. |
06.27 | the output is 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 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 an 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' 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 |
10.32 | To execute type ./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, |
11.18 | 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 BombayA
Thank You for watching. |
Contributors and Content Editors
Ashwini, Devraj, Kavita salve, Krupali, PoojaMoolya, Priyacst, Sakinashaikh, Sandhya.np14, Sneha