C-and-C++/C2/If-And-Else-If-statement/English-timed

From Script | Spoken-Tutorial
Revision as of 16:27, 12 November 2013 by Sakinashaikh (Talk | contribs)

Jump to: navigation, search
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.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.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.47 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() 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.11 Like here we have %d it denotes that we are dealing with integer data type.
03.19 Here we add the values of a and b.


03.22 We will store the result in sum.
03.26 Then we print the result.
03.29 This is our if statement.
03.31 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.43 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 ifstmt.c -o 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.32 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.49 We will check another condition.
04.53 Let me remove the comment from here.
04.57 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.27 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.39 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.08 Now let us execute and see. come back to our terminal.
06.12 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.44 The output is displayed as
07.46 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.
08.00 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 .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.54 Type cin and two closing angle brackets >>
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 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.32 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.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++ space ifstmt.cpp space -o space if1
10.21 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.33 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.53 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.03 Let us summarize.
11.04 In this tutorial we learned, if statement eg. if(condition)

{…........

}

11.12 And 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 watching.