Else If Statement in C Language

To show a multi-way decision based on several conditions, we use the else if statement.

This works by cascading of several comparisons. As soon as one of the conditions is true, the statement or block of statements following them is executed and no further comparisons are performed.

The syntax of Else If Statement in C Language

if (condition_1)
{
Statements_1_Block;
}
else if (condition_2)
{
Statements_2_Block;
}

else if (condition_n)
{
Statements_n_Block;
}
else
Statements_x;

Here, the conditions are evaluated in order from top to bottom.

As soon as any condition evaluates to true, then the statement associated with the given condition is executed and control is transferred to Statements_x skipping the rest of the conditions following it. But if all conditions evaluate false, then the statement following final else is executed followed by the execution of Statements_x.

 

Else If Statement in C Language
Else If Statement in C

A program to illustrate Else If Statement in C Language

Write a program to award grades to students depending upon the criteria mentioned below:

  • Marks less than or equal to 50 are given “D” grade
  • Marks above 50 but below 60 are given “C” grade
  • Marks between 60 to 75 are given “B” grade
  • Marks greater than 75 are given “A” grade.

/* Program to award grades */

#include <stdio.h>

main()

{

int result;

printf(“Enter the total marks of a student:\n”);

scanf(“%d”,&result);

if (result <= 50)

printf(“Grade D\n”);

else if (result <= 60)

printf(“Grade C\n”);

else if (result <= 75)

printf(“Grade B\n”);

else

printf(“Grade A\n”);

}

OUTPUT

Enter the total marks of a student:
80
Grade A

 

One thought on “Else If Statement in C Language

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
?>