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:
- The first
if
statement checkscondition1
. If it evaluates totrue
, its block executes. - If
condition1
is false, the program moves to the nextelse if
condition. - Multiple
else if
blocks can be used for additional conditions. - 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:
- The program asks the user to input their marks.
- The first
if
condition checks if marks are 90 or above and assigns grade “A”. - The second
else if
condition assigns grade “B” if marks are 75 or above. - The third
else if
condition assigns grade “C” if marks are 50 or above. - 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:
- The program prompts the user to enter a number.
- The first condition checks if the number is greater than zero and prints “positive”.
- The second condition checks if the number is less than zero and prints “negative”.
- 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:
- The user is asked to enter their age.
- The first condition checks if age is 18 or above, meaning the user can vote.
- The second condition checks if age is positive but less than 18, meaning the user cannot vote.
- 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:
- The syntax and working of
if-else-if
statements. - How multiple conditions can be checked sequentially using
if-else-if
statement. - Example programs such as grading systems, number checks, and voting eligibility using
if-else-if
statement.