Else if Statement in C

By | February 20, 2023

Prerequisite – Control Statements in C
In C, the “else if” statement is used to check multiple conditions after an initial “if” statement.

Syntax:
The basic syntax of the “if-else if” statement in C is:

if (condition1) {
  // Code to execute if condition1 is true
} else if (condition2) {
  // Code to execute if condition2 is true
} else if (condition3) {
  // Code to execute if condition3 is true
} else {
  // Code to execute if all conditions are false
}

Here, the code inside the first set of braces will be executed if condition1 is true; if not, the code inside the second set of braces will be executed if condition2 is true; if not, the code inside the third set of braces will be executed if condition3 is true; if none of the conditions are true, the code inside the last set of braces will be executed.

You can have as many “else if” blocks as you need in order to check multiple conditions.

Examples:
Here are some examples of how the “else if” statement can be used in C:

Example-1:

#include <stdio.h>

int main() {
   int num;
   
   printf("Enter a number: ");
   scanf("%d", &num);
   
   if (num == 0) {
      printf("The number is zero.\n");
   } else if (num > 0) {
      printf("The number is positive.\n");
   } else {
      printf("The number is negative.\n");
   }
   
   return 0;
}

In this example, the “if-else if” statement is used to check whether the number entered by the user is zero, positive, or negative. If the number is zero, the code inside the first set of braces is executed, which prints “The number is zero.” to the console. If the number is greater than zero, the code inside the second set of braces is executed, which prints “The number is positive.” to the console. If the number is less than zero, the code inside the third set of braces is executed, which prints “The number is negative.” to the console.

Example-2:

#include <stdio.h>

int main() {
   int grade;
   
   printf("Enter your grade (0-100): ");
   scanf("%d", &grade);
   
   if (grade >= 90) {
      printf("You got an A.\n");
   } else if (grade >= 80) {
      printf("You got a B.\n");
   } else if (grade >= 70) {
      printf("You got a C.\n");
   } else if (grade >= 60) {
      printf("You got a D.\n");
   } else {
      printf("You got an F.\n");
   }
   
   return 0;
}

In this example, the “if-else if” statement is used to assign a letter grade based on a numeric grade entered by the user. If the grade is greater than or equal to 90, the code inside the first set of braces is executed, which prints “You got an A.” to the console. If the grade is between 80 and 89, the code inside the second set of braces is executed, which prints “You got a B.” to the console, and so on.

Example-3:

#include <stdio.h>

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else if (num % 3 == 0) {
        printf("%d is odd and divisible by 3.\n", num);
    } else {
        printf("%d is odd and not divisible by 3.\n", num);
    }

    return 0;
}

In this example, the user enters a number and the program checks if the number is even or odd and divisible by 3 or not. If the number is even, the code inside the first set of braces is executed, which prints “%d is even.” to the console. If the number is odd and divisible by 3, the code inside the second set of braces is executed, which prints “%d is odd and divisible by 3.” to the console. If the number is odd and not divisible by 3, the code inside the third set of braces is executed, which prints “%d is odd and not divisible by 3.” to the console.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.