C-and-C++/C2/If-And-Else-If-statement/English
Title of script: Conditional statements
Author: Ashwini R. Patil
Keywords: if, if-else, Video Tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on Conditional statements in C and C++ |
Slide 2
|
In this tutorial we will learn how,
To execute a single statement. To execute group of statements. We will do this through examples. We will also see some common errors and their solutions. |
Slide 3
|
To record this tutorial, I am using,
Ubuntu Operating system version 11.10. gcc and g++ Compiler version 4.6.1 on Ubuntu. |
Slide 4 | Let us start with the Introduction to statements.
A statement in a program controls the flow of program execution. It helps to make decision on, what code is to be executed. We can check the conditions, whether true or false. We can execute a single statement or a group of statements. |
Slide 5 | Let us understand the flow of if statements.
Here, if the condition is true, statement1 will be executed. If the condition is false then statement2 will be executed. |
Slide 6 | Now we will see the flow of else if statement,
Here, if condition1 is true then statement1 will be executed. If condition1 is false then it will check for another condition that is condition2. If condition2 is true, then statement3 will be executed. If condition2 is false, then statement2 will be executed |
Let us switch to the file ifstmt.c
|
Now Let us move on to our program.
I have already typed the code on the editor. So let me open it. Note that our filename is ifstmt.c In this program we will calculate the sum of two numbers and will check a few conditions. |
Highlight
#include <stdio.h> |
Let me explain the code now.
This is our header file. |
Highlight
int main() { |
This is our main function. |
Highlight
int a, b, sum; |
Here we have declared three integer variables
a, b and sum. |
Highlight
printf("Enter the value of a and b is \n"); scanf("%d%d",&a,&b); |
Here we are asking for user input.
User will enter the values of a and b. The values will be stored in variable a and variable b. |
Highlight
scanf("%d%d",&a,&b); |
scanf() reads data from the console.
It then stores the result in the given variable. The format specifier in the scanf() helps to know the type of data. Like here we have %d it denotes that we are dealing with integer data type. |
Highlight
sum=a+b;
|
Here we add the values of a and b.
We will store the result in sum. |
Highlight
printf("Sum of a and b is %d\n", c); |
Then we print the result. |
Highlight
if(sum > 20) { printf("Sum is greater than20 \n"); } |
This is our if statement.
Here, we check the condition whether sum is greater than 20. If the condition is true, then we print Sum is greater than 20. |
Type
/* */ |
Now let me comment these lines. |
Highlight
return 0; |
This is our return statement. |
Now click on Save to save the program.
First we will see the execution of if statement. | |
Ctrl, Alt and T keys simultaneously | Please open the terminal window by pressing,
Ctrl, Alt and T keys simultaneously on your keyboard. |
Type
gcc ifstmt.c -o if |
To compile the program type,
gcc ifstmt.c -o if |
Type
./if |
To execute the program type,
./if |
Here it is displayed as,
Enter the value of a and b. I will give the values as 10 and 12. The output is displayed as Sum of a and b is 22. C is greater than 20. | |
Now come back to our program. | |
We will check another condition.
Let me remove the comment from here. I will give the comment here. Now click on Save. | |
Remove /*
|
This is our else-if statement.
Here, we check another condition whether the Sum is greater than 10. If the condition is true. Then we print Sum is greater than 10 and less than 20. |
On the terminal | Now come back to our terminal.
Let us compile as before. Let us execute as before. |
It is displayed as,
Enter the value of a and b. I will give the values as 10 and 2. The output is displayed as: Sum of a and b is 12. Sum is greater than 10 and less than 20. Let me clear the prompt. | |
Now come back to our program. | |
Remove /* | I will remove the comment from here and here..
Now click on Save. |
Highlight
if(c > 20) { printf("Sum is greater than20 \n"); } else if(c > 10) { printf("Sum is greater than 10 \n"); } Highlight else { printf("Sum is less than 10 \n"); } |
If both the conditions are false, then we print Sum is less than 10.
This is our else statement. Now let us execute and see. |
On the terminal | Now come back to our terminal.
Let us compile as before. Let us execute as before. |
Here it is displayed,
Enter the value of a and b. I will give the values as 3 and 5. The output is displayed as, the sum of a and b is 8. Sum is less than 10. | |
Errors | Now we will see the common errors which we can come across.
Come back to our program. |
Type
%d
|
Suppose, here at the end of if statement I will type a semicolon.
Let see what will happen. |
Click on save | Click on Save. |
On the terminal | Let us execute.
Come back to our terminal. Let us compile as before. |
We see an error:
else without a previous if Come back to our program. It is an syntax error. If statement will never terminate with a semicolon. And else if statement will never work without an if. | |
Let us fix the error.
Delete the ; here | |
Click on Save. | |
Let us execute.
Come back to our terminal. | |
Let us compile aa before.
Let us execute as before. Enter the value of a and b I will give the value as 3 and 6. The output is displayed as Sum of a and b is 9. Sum is less than 10. | |
NOW WE WILL SEE HOW TO EXECUTE THE SAME PROGRAM IN C++.
Come back to our program. | |
I will change a few things here. | |
Shift, Ctrl and S simultaneously | Press Shift, Ctrl and S simultaneously on your keyboard.
Now save the file with an extension .cpp and click on Save |
Type
<iostream> |
We will change the header file as,
iostream |
Type
using namespace std; |
Let us include the using statement here. |
Type
cout<< |
Now replace all printf statement with cout statement.
As we use cout<< function to print a line in C++. |
Delete
( ) |
Delete the opening and the closing brackets in the printf statement. |
Type
scanf() |
Now click on the search and replace option text
Let us replace the printf( statement with the cout << statement. Click on Replace all and click on Close Now delete the closing bracket here. Replace the scanf statement with the cin statement. Type cin >> As we use cin >> function to read a line in C++. It is used to get input from the console. |
Delete
%d |
Now delete the format specifiers.
Delete the comma and & Delete the comma here and type two closing angle brackets. Again delete the & and the closing brackets Now Click on Save |
Type
<< |
Here delete the closing bracket and the comma.
Now delete the \n and format specifier Type two opening brackets Again type two opening brackets << “\n”. Here also we will delete the closing bracket. Now again delete the closing bracket here and here. |
Click on Save | Click on Save |
Let us execute. | |
On the terminal | Come back to the terminal.
To compile the program type g++ ifstmt.cpp -o if1 Here we have if1 because we don't want to overwrite the output parameter for the file ifstmt.c Press Enter To execute type ./if1 Press Enter Enter the value of a and b. I will give 20 and 30. The output is displayed as, Sum of a and b is 50. Sum is greater than 20. This brings us to the end of this tutorial. Now come back to our slide. |
Let us summarize.
In this tutorial we learned, if statement eg. if(condition) {…........ } else if statement eg. else if(condition) {…......... } | |
Slide 5
|
As an assignment,
Write a program to check a is greater than b or less than b. Hint: use if statement. Write another program to check which value is greater a, b or c. Hint: use else-if statement. |
Slide 6
About the Spoken Tutorial Project |
Watch the video available at the link shown below.
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial It summarizes the Spoken Tutorial project. If you do not have good bandwidth, you can download and watch it. |
Slide 7
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team,
Conducts workshops using spoken tutorials. Gives certificates to those who pass an online test. For more details, please write to, contact [at] spoken hyphen tutorial dot org. |
Slide 8
|
Spoken Tutorial Project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at the link shown below. |
This is Ashwini Patil from IIT Bombay
Thank You for watching. |