What are the else-if Statement in C Language ??? with Free Notes

What are the else-if Statement ???

else-if Statement is Conditional Control Statements which is used for check multiple conditions. Like in this if you declare 7 conditions, then our program check first condition if this is true then it will end if false then it will check next and also the second condition is false then it will move on third, and if third is false then move it on next and going so on… The program will end where the condition is matched or true.

Let’s understand this by exploring the syntax of else-if Statement.

Syntax : else-if

Here is the syntax of else-if Statement,

if (condition1) {
    // code block to execute if condition1 is true
} else if (condition2) {
    // code block to execute if condition1 is false but condition2 is true
} else {
    // code block to execute if both condition1 and condition2 are false
}

Here, “condition1” and “condition2” are expressions that evaluate to either true or false.

C Language E-Book

Example:

Here we write a program for else-if Statement,

#include <stdio.h>

int main() {
    int marks = 75;

    // Else-if statement example
    if (marks >= 90) {
        printf("Grade: A\n");
    } else if (marks >= 80) {
        printf("Grade: B\n");
    } else if (marks >= 70) {
        printf("Grade: C\n");
    } else if (marks >= 60) {
        printf("Grade: D\n");
    } else {
        printf("Grade: F\n");
    }

    return 0;
}

Explanation about Example

In this example, we used else-if Statement to check the student grades on the basis of “marks” variable. Here we set the different-different conditions in every else-if-else blocks. And according that grades will be print.

Leave a Comment

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

Scroll to Top