C if-else-if Statement

In C, the if-else-if statement is used to execute different blocks of code based on multiple conditions. It allows checking multiple conditions sequentially, and when a condition evaluates to true, its corresponding block is executed, skipping the rest.


Syntax of if-else-if Statement

</>
Copy
if (condition1) {
    // Code executes if condition1 is true
} else if (condition2) {
    // Code executes if condition2 is true
} else if (condition3) {
    // Code executes if condition3 is true
} else {
    // Code executes if none of the conditions are true
}

Explanation of Syntax:

  1. The first if statement checks condition1. If it evaluates to true, its block executes.
  2. If condition1 is false, the program moves to the next else if condition.
  3. Multiple else if blocks can be used for additional conditions.
  4. If none of the conditions are true, the else block executes.

Examples of if-else-if Statements

1. Checking the Grade of a Student

In this example, we will take a student’s marks as input and determine their grade based on predefined ranges.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int marks;

    printf("Enter your marks: ");
    scanf("%d", &marks);

    if (marks >= 90) {
        printf("Grade: A\n");
    } else if (marks >= 75) {
        printf("Grade: B\n");
    } else if (marks >= 50) {
        printf("Grade: C\n");
    } else {
        printf("Grade: F\n");
    }

    return 0;
}

Explanation:

  1. The program asks the user to input their marks.
  2. The first if condition checks if marks are 90 or above and assigns grade “A”.
  3. The second else if condition assigns grade “B” if marks are 75 or above.
  4. The third else if condition assigns grade “C” if marks are 50 or above.
  5. If none of the above conditions are met, the else block assigns grade “F”.

Output:

Enter your marks: 85
Grade: B

2. Checking Whether a Number is Positive, Negative, or Zero

In this example, we will determine whether a number is positive, negative, or zero using an if-else-if statement.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int num;

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

    if (num > 0) {
        printf("The number is positive.\n");
    } else if (num < 0) {
        printf("The number is negative.\n");
    } else {
        printf("The number is zero.\n");
    }

    return 0;
}

Explanation:

  1. The program prompts the user to enter a number.
  2. The first condition checks if the number is greater than zero and prints “positive”.
  3. The second condition checks if the number is less than zero and prints “negative”.
  4. If neither condition is met, the number is zero.

Output:

Enter a number: -7
The number is negative.

3. Determining Voting Eligibility

In this example, we will check if a person is eligible to vote based on their age.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age >= 18) {
        printf("You are eligible to vote.\n");
    } else if (age > 0) {
        printf("You are not eligible to vote yet.\n");
    } else {
        printf("Invalid age entered.\n");
    }

    return 0;
}

Explanation:

  1. The user is asked to enter their age.
  2. The first condition checks if age is 18 or above, meaning the user can vote.
  3. The second condition checks if age is positive but less than 18, meaning the user cannot vote.
  4. If age is negative, an “invalid age” message is displayed.

Output:

Enter your age: 16
You are not eligible to vote yet.

Conclusion

In this tutorial, we learned:

  1. The syntax and working of if-else-if statements.
  2. How multiple conditions can be checked sequentially using if-else-if statement.
  3. Example programs such as grading systems, number checks, and voting eligibility using if-else-if statement.