C-and-C++/C2/If-And-Else-If-statement/English-timed
From Script | Spoken-Tutorial
| Time' | 'Narration |
| 00.02 | Welcome to the spoken tutorial on Conditional statements in C and C++ |
| 00.09 | In this tutorial we will learn,
|
| 00.11 | how to execute a single statement.
|
| 00.14 | How to execute group of statements.
|
| 00.17 | We will do this through examples.
|
| 00.20 | 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.32 | 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.50 | It helps to make decision on, what code is to be executed.
|
| 00.56 | We can check the conditions, whether true or false.
|
| 01.01 | We can execute a single statement or a group of statements. |
| 01.08 | 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.33 | 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.50 | If condition2 is true, then statement3 will be executed.
|
| 01.55 | And If condition2 is false, then statement2 will be executed |
| 02.03 | 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.19 | In this program we will calculate the sum of two numbers and will check a few conditions. |
| 02.27 | Let me explain the code now.
|
| 02.31 | 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.48 | Here we are asking for user input.
|
| 02.51 | User will enter the values of a and b.
|
| 02.53 | The values will be stored in variable a and variable b. |
| 03.03 | 'scanf() reads data from the console.
|
| 03.06 | It then stores the result in the given variable.
|
| 03.10 | The format specifier in the scanf() helps to know the type of data.
|
| 03.15 | Like here we have %d it denotes that we are dealing with integer data type. |
| 03.25 | Here we add the values of a and b.
|
| 03.27 | We will store the result in sum. |
| 03.33 | Then we print the result. |
| 03.38 | This is our if statement. |
| 03.40 | Here, we check the condition whether sum is greater than 20. |
| 03.44 | If the condition is true, then we print Sum is greater than 20. |
| 03.51 | Now let me comment out these lines. |
| 04.00 | This is our return statement. |
| 04.03 | Now click on Save |
| 04.05 | First we will see the execution of if statement. |
| 04.10 | Please open the terminal window by pressing,Ctrl, Alt and T keys simultaneously on your keyboard. |
| 04.27 | To compile type, gcc ifstmt.c -o ifand press enter |
| 04.38 | To execute type, ./if press enter |
| 04.44 | it is displayed as, |
| 04.45 | Enter the value of a and b. |
| 04.50 | I will give the values as 10 and 12. |
| 04.58 | The output is displayed as Sum of a and b is 22. Sum is greater than 20. |
| 05.07 | Now come back to our program. |
| 05.10 | We will check another condition. |
| 05.14 | Let me remove the comment from here. |
| 05.20 | I will give the comment here. |
| 05.25 | Now click on Save. |
| 05.31 | This is our else-if statement. |
| 05.34 | Here, we check another condition whether Sum is greater than 10 |
| 05.38 | If the condition is true. Then we print Sum is greater than 10 and less than 20. |
| 05.51 | Now come back to our terminal. |
| 05.53 | Let us compile as before. Let us execute as before. |
| 06.00 | It is displayed as, |
| 06.01 | Enter the value of a and b. |
| 06.03 | I will give the values as 10 and 2. |
| 06.10 | The output is displayed as: Sum of a and b is 12. |
| 06.14 | Sum is greater than 10 and less than 20. |
| 06.18 | Let me clear the prompt. Now come back to our program. |
| 06.25 | I will remove the comment from here and here.Now click on save, |
| 06.39 | If both the conditions are false, then we print Sum is less than 10. |
| 06.48 | This is our else statement. |
| 06.52 | Now let us execute and see. come back to our terminal. |
| 06.56 | Let us compile as before. Let us execute as before. |
| 07.02 | Here it is displayed as, |
| 07.04 | Enter the value of a and b. |
| 07.06 | I will give the values as 3 and 5. |
| 07.12 | the output as, sum of a and b is 8. |
| 07.16 | Sum is less than 10. |
| 07.22 | Now we will see the common errors which we can come across. |
| 07.26 | Come back to our program. |
| 07.30 | Suppose, here at the end of if statement I will type a semicolon. |
| 07.36 | Let see what will happen.Click on Save.
|
| 07.39 | Let us execute. Come back to our terminal. |
| 07.42 | Let us compile as before. |
| 07.45 | We see an error: else without a previous if |
| 07.52 | Come back to our program.It is an syntax error. |
| 07.59 | If statement will never terminate with a semicolon. |
| 08.03 | And the else if statement will never work without an if. |
| 08.09 | Let us fix the error. Delete the ; here |
| 08.15 | Now Click on Save. |
| 08.17 | Let us execute. Come back to the terminal. |
| 08.22 | Let us compile aa before. Let us execute as before. |
| 08.28 | Enter the value of a and b |
| 08.30 | I will give the value as 3 and 6. |
| 08.36 | The output is displayed as |
| 08.38 | Sum of a and b is 9. Sum is less than 10. |
| 08.47 | NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++. |
| 08.52 | Come back to our program. |
| 08.54 | I will change a few things here. |
| 09.01 | Press Shift, Ctrl and S keys simultaneously on your keyboard. |
| 08.11 | Now save the file with an extension .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.36 | let us replace printf statement with cout statement. |
| 08.40 | Click on Replace all and click on Close
|
| 08.46 | Now delete the closing brackets here. |
| 08.50 | Replace the scanf statement with the cin statement. |
| 08.55 | Type cin >> |
| 09.00 | As we use cin >> function to read a line in C++.
|
| 09.06 | Now delete the format specifiers. |
| 09.09 | Delete the comma and & |
| 09.12 | Delete the comma here and type two closing angle brackets. |
| 09.17 | Again delete the & and the closing brackets Now Click on Save |
| 0925 | Here delete the closing bracket and the comma. |
| 09.32 | Now delete the \n and format specifier |
| 09.37 | Now Type two opening brackets |
| 09.42 | Again type two opening brackets “\n”. |
| 09.49 | Here also we will delete the closing bracket. |
| 09.54 | Now again delete the closing bracket here and here. |
| 09.59 | Now Click on Save |
| 10.03 | Let us execute. |
| 10.04 | Come back to the terminal.let me clear the prompt. |
| 10.10 | To compile type g++ ifstmt.cpp -o if1 |
| 10.21 | Here we have if1 because we don't want to overwrite the output parameter for the file ifstmt.c |
| 10.31 | Now Press Enter |
| 10.33 | To execute type ./if1 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.53 | Sum is greater than 20. |
| 10.57 | This brings us to the end of this tutorial. |
| 10.59 | Now come back to our slides. |
| 11.03 | Let us summarize. |
| 11.04 | In this tutorial we learned, if statement eg. if(condition)
{…........ } |
| 11.12 | else if statement eg. else if(condition)
{…......... } |
| 11.18 | As an assignment, |
| 11.19 | 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.39 | Watch the video available at the link shown below. http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial |
| 11.41 | It summarizes the Spoken Tutorial project. |
| 11.44 | If you do not have good bandwidth, you can download and watch it. |
| 11.49 | The Spoken Tutorial Project Team, |
| 11.51 | Conducts workshops using spoken tutorials. |
| 11.54 | Gives certificates to those who pass an online test. |
| 11.58 | For more details, please write to, contact [at] spoken hyphen tutorial dot org. |
| 12.05 | 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.16 | More information on this Mission is available at the link shown below. (http://spoken-tutorial.org\NMEICT-Intro) |
| 12.21 | This is Ashwini Patil from IIT Bombay
Thank You for joining |
Contributors and Content Editors
Ashwini, Devraj, Kavita salve, Krupali, PoojaMoolya, Priyacst, Sakinashaikh, Sandhya.np14, Sneha