Table of Contents
Introduction
Now, we are ready to deal with actual coding, which begins from this chapter. Go through the previous articles to clear each concept. In real life, decision-making skills are essential. Sometimes there are some conditions, and based on those conditions, we have to make decisions that should work well. For instance, a 10th pass-out student decides to opt for science if he gets more than 85%, else he plans to opt for commerce. In C language, we deal with some of the decision-making (if else) statements that will be discussed in the article.
if Statement
Syntax:
if (condition)
{
Code … ;
……..;
}
The syntax consists of an ‘if’ keyword followed by the condition that will be a deciding factor, and if that condition is true, then the body of the if statement written in ‘{ }’ braces will execute. Otherwise, not.
Example
Let’s consider an example.
include<stdio.h> int main() { int a=8; if ( a%2==0) { printf("a is a even number"); } }
Output:
if (a%2==0) => Here (a%2==0) is the condition.
Condition is always written in ‘( )’ braces followed by the ‘if’ keyword. Since the condition is true, statements written inside ‘{ }’ braces get printed as shown in the output.
if…else Statement
if …. else in C programming consists of 2 choices. If the condition of the if part is true, then its body gets executed. Otherwise, the else part with the 2nd choice gets executed as the output.
Example
Let’s write the above code with the else part.
#include<stdio.h> int main() { int a=7; if ( a%2==0) { printf("a is a even number"); } else { Printf(“ a is odd number”); } }
Output
In the same way as above, the condition in if will be checked first, and since it is false ( 7%2 does not give 0 ), the statements in else are executed.
It is advantageous in real-life examples too. For example, a student can sit in exams only if their attendance is equal to or more than 75%. Otherwise, not.
Then the code can be written as:
#include<stdio.h> int main() { int marks; printf("enter total marks obtained out of 500\n"); scanf("%d",&marks); int p=(marks/500.0)*100; printf("percentage:%d",p); if (p>=75) { printf("\nAllowed to sit in exams"); } else { printf("\nNot allowed to sit in exams"); } return 0; }
Output:
This example is very easy to understand. The condition in if is (p>=75). This means that if ‘p’ is more than or equal to 75, it will print statements inside if. Otherwise, the statements inside else will be printed.
Note:
- else is not followed by any condition.
- If statement can be used without else, but vice-versa is not true.
else if Statements
Many times, ‘if’ and ‘else’ are not sufficient to complete the code. For example, if a teacher wants to give grades to the students according to their percentage, then there are many cases and in such problems ‘else if’ statements make the code possible.
Example
Consider this example:
#include<stdio.h> int main() { int marks; printf("enter total marks obtained out of 500\n"); scanf("%d",&marks); int p=(marks/500.0)*100; printf("percentage:%d\n",p); if (p>=90) { printf("Grade A+"); } else if (p>=80 && p<90) { printf("Grade A"); } else if ( p>=70 && p<80) { printf("Grade B"); } else if ( p>=60 && p<70) { printf(" Grade C"); } else { printf("Grade D"); } return 0; }
Output:
‘else if’ statement will become clear to you with the help of this program. The program is quite simple. Since we have many options, we use the ‘else if’ statement with a particular condition in each statement. We can use as many ‘else if’ statements as per the need between if and else.
Note: If there is a single statement inside the body of if, else, or else if, we can skip the ‘{ }’ braces.
#include<stdio.h> int main() { int age; printf("enter your age:\n"); scanf("%d",&age); if (age<=17) printf("You are a child"); else if (age>17 && age<=20) printf("You are a teenager"); else printf("You are an adult"); return 0; }
Output:
Nested if/else
Nested if/else means using if or else inside another if or else.
Look at the example for nested if/else.
Example: To print whether a number is the greatest or not.
#include<stdio.h> int main() { int x,y,z; printf("enter three numbers:\n"); scanf("%d",&x); scanf("%d",&y); scanf("%d",&z); if(x>y) { if(x>z) { printf("%d is the greatest number"); } } }
Output
The condition written in the first ‘if’ statement is true (21>20). Therefore, the control moves inside the body of ‘if’ and the statement enclosed in ‘{ }’ is executed.
Within the curly braces, the first statement, i.e. if(x>z), will be executed first. Since the condition is true, the statement inside the body of ‘if’ gets printed on the screen.
In short, the comparison between ‘x’ and ‘y’ is made first. If that comes out to be true, then ‘x’ is compared with ‘z’. It looks simple enough because ‘x’ is the greatest of all among the values entered by the user.
If the input of ‘x’ given by the user is lesser than ‘y’ and ‘z’, there will be no output.
So, we have to write a complete code that compares every case and prints the desired output.
Go through the code given below and practice it.
#include<stdio.h> int main() { int x,y,z; printf("enter three numbers:\n"); scanf("%d %d %d",&x,&y,&z); if(x>y) { if(x>z) { printf("%d is the greatest number",x); } else { printf("%d is the greatest number",z); } } else if(y>x) { if(y>z) { printf("%d is the greatest number",y); } else { printf("%d is the greatest number",z); } } else { printf("% is greatest number",z); } }
Output:
The above code can be simplified as shown below:
#include<stdio.h> int main() { int x,y,z; printf("enter three numbers:\n"); scanf("%d %d %d",&x,&y,&z); if(x>y && x>z) { printf("%d is the greatest number",x); } else if(y>x && y>z) { printf("%d is the greatest number",y); } else { printf("%d is the greatest number",z); } return 0; }
Output:
In the above example, we have three numbers, x, y, and z, to find the greatest among them. For that, we will first compare the first number with others, i.e. ‘x’ with ‘y’, and ‘z’. If the condition if(x>y && x>z) comes out to be true, the body of this statement written in the curly braces gets executed, and ultimately the rest of all the conditions will become false, and the program ends. However, if the condition turns out to be false, the control will move to the else if(y>x && y>z) statement and follow the same process as the previous statement. Now, if this also becomes false, there is no option except the ‘else’ statement. Thus, whatever is written in the body of ‘else’ will be executed.
Another form of if-else: Ternary operator in C
A ternary operator is the second form of if-else, which can be used rather than using if-else statements. First, it will check whether a given condition is true or not and then the expressions will be evaluated accordingly. If the condition is true, then expression 1 gets evaluated. Otherwise, expression 2 gets evaluated.
Syntax: condition? expression 1: expression 2;
Example
#include<stdio.h> int main() { int age; printf("Enter age\n"); scanf("%d",&age); (age>18)? printf("eligible to vote") : printf("not eligible to vote"); return 0; }
Output
Explanation
If the condition ( age>18 ) is true, then expression 1 will get executed. Otherwise, expression 2 will get evaluated. Since 22 is more than 18, expression 1 gets executed, and “eligible to vote” gets printed.
0 Comments