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.

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:

If(exp 1){
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.

The output of the above program is given below:

Enter your child’s age: 3
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
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.

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.


Is it raining outside?
Enter ‘y’ for yes and ‘n’ for no.
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 1)
if(condition 2){
statement 1
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.

The output of the above program is given below:


Enter three different integers:
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:

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 customer number: 3
Enter units consumed: 500
The charge of Costumer with costumer number=3 is: Rs 300
SHARE C if…else Statment

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *