C if…else Statment
C programming deploys If statement for efficient decision making in programs. In this tutorial, you will learn about how If statement works, its types and its simple implementation. We can use different types of If statements. If statements may be of different forms. Which depends upon the complexity of conditions to be tested. They are listed below:
- Simple If statement
- If else statement
- Nested If else
- Else if ladder statement
Now we will go through all the types of If Statement which can be used in our program.
Contents
What is simple If statement?
A simple if statement is called a branching statement. It is called so because it provides a function where the program selects a path and follows. The general syntax of simple if statement is listed below:
Statement 1
}
remaining statement
If the given exp 1 is true then Statement 1 is executed and if it is not true then the remaining statement is executed.
Flowchart of simple If statement:
Following example helps you in understanding more about simple If statement.
#include<stdio.h> int main(){ int age; printf("Enter your child's age: '"); scanf("%d", &age); if(age<5){ printf("Is your child polio vaccinated?\n"); } printf("You are now at City hospital."); }
The output of the above program is given below:
Is your child polio vaccinated?
You are now at City Hospital.
What is the If-Else statement?
The If else statement is an extension of simple if statement. When the given condition is false, If else statement executes another section of code. If else statement helps in decision making followed by checking the given condition. As can be seen, the control of the program jumps in different statements as per the given condition.
The general syntax of If Else statement is given below:
{
Statement 1
}
else
{
Statement 2
}
remaining statement
As we see in the above code that, there can be two paths for a given condition in If Else Statement. One path is followed when the given condition is true and another is followed when the given condition is false.
Flowchart of If-else statement:
Now let’s move onto our first program using If Else statement in C. Following program demonstrates about this topic more.
//C Program to demostrate the use of If Else in decision making #include<stdio.h> int main(){ char rain; printf("Is it raining outside?\n"); printf("Enter 'y' for yes and 'n' for no.\n"); scanf("%c",&rain); if(rain=='y'){ printf("Carry an umbrella."); } else{ printf("You donot need to carry an umbrella."); } }
In the above code, it is evident that the use of If Else statement can assist us in decision making. With this in mind, we can efficiently implement these things in more programs in the near future. Now let’s what output does the above code displays.
Output:
Enter ‘y’ for yes and ‘n’ for no.
y
Carry an umbrella.
What is Nested If else?
When we have to make a series of decisions, we will have to use more than one if else statement. In such case, Nested If else comes into the play. Nested If else will include more than one conditions and branching will be done on the basis of matching conditions. Whenever the given condition is true, the statement inside it executes. In Nested If else, there would sometime be a condition inside a condition and the program will execute in the same way by executing the matching condition’s statements.
The general syntax of Nested If else is given below:
{
if(condition 2){
statement 1
}
else{
statement 2
}
}
else {
statement 2
}
remaining statements
The Flowchart of the above syntax is shown below.
Now let’s move onto our first example implementing the Nested If else in our program.
//C program to illustrate the use of Nested If else in the program //This program calculates the largest of any three given numbers #include<stdio.h> int main(){ int num1, num2, num3; printf("Enter three different integers:\n"); scanf("%d%d%d", &num1, &num2, &num3); if(num1 > num2){ if(num1 >num3){ printf("%d is largest.",num1); } else{ printf("%d is largest.",num3); } } else{ if(num2 > num3){ printf("%d is largest", num2); } else{ printf("%d is largest",num3); } } }
The output of the above program is given below:
Output:
3
2
7
7 is the largest.
What is Else If ladder statement?
Else If ladder statement is used for multiple conditions if any one condition among all of them is true then rest of all will be false. Whenever the condition in the program is true then the statement inside executes bypassing all the remaining conditions and statements and when none of the mentioned condition is true then the statement of the else block executes. The general syntax of the Else if ladder statement is given below:
//An example of the Else if ladder //Program that takes in costumer number and unit consumed as input and calculate charges under certain conditions #include<stdio.h> int main(){ int costumer_number, unit; float charge; printf("Enter costumer number: "); scanf("%d",&costumer_number); printf("Enter units consumed: "); scanf("%d",&unit); if(unit<=200){ charge=0.5*unit; } else if(unit>200 && unit<=400){ charge = 100+0.65*(unit-200); } else if(unit>400 && unit<=600){ charge = 250+0.8*(unit-400); } else{ charge =390+1*(unit-600); } printf("The charge of Customer with customer number=%d is: Rs%f", costumer_number, charge); }
In the above program, the rate of charge is calculated on the basis of the unit consumed. When the unit consumed is in between 0-200 then the rate is Rs 0.5 per unit. Similarly, other charges are calculated on the basis of the units they consume.
The program will generate the output by calculating all the charges as below:
Enter units consumed: 500
The charge of Costumer with costumer number=3 is: Rs 300