Table of Contents
What is an If-Else Statement in C?
An if-else statement is used to execute a block of code when a condition is true, and another block when the condition is false.Syntax:
The condition is usually a comparison or logical expression that returns either true (1) or false (0).if (condition) {
// code executed if condition is true
} else {
// code executed if condition is false
}
Simple If-Else Program in C
Example: Check if a Number is Positive or Negative
// C program to check if a number is
// positive or negative
#include stdio.h
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num = 0)
{
printf("The number is positive.\n");
}
else
{
printf("The number is negative.\n");
}
return 0;
}
Output:
Explanation:Enter a number: -34
The number is negative.
The program takes an integer input from the user. It checks whether the number is greater than or equal to zero. If the condition is true, it prints that the number is positive. Otherwise, it prints that the number is negative.
If-Else with Multiple Conditions
Sometimes, you need to check more than one condition. In such cases, you can use else if.Example: Check Grade Based on Marks
// C program to check grade based on marks
#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: Fail\n");
}
return 0;
}
Output:
Explanation:Enter your marks: 55
Grade: C
The program checks conditions from top to bottom. As soon as one condition becomes true, the corresponding block executes, and the rest are skipped. This ensures only one grade is printed.
Nested If-Else Statement
You can also place one if-else statement inside another. This is called nested if-else.Example: Check Eligibility for Voting and Senior Citizen
// C program to check eligibilty for
// voting and senior citizen
#include stdio.h
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age = 18)
{
printf("You are eligible to vote.\n");
if (age = 60)
{
printf("You are also a senior citizen.\n");
}
}
else
{
printf("You are not eligible to vote.\n");
}
return 0;
}
Output:
Explanation:Enter your age: 35
You are eligible to vote.
The outer if checks whether the person can vote. If true, the inner if checks if the person is also a senior citizen. This shows how conditions can be layered.
Common Mistakes to Avoid
- Using Assignment Instead of Comparison: Using = instead of == can lead to incorrect results.
- Missing Curly Braces: Skipping braces can cause logical errors when adding more statements later.
- Wrong Order in Else If Ladder: Conditions should be written from highest to lowest priority.
- Ignoring Edge Cases: Always consider boundary values like 0, negative numbers, or maximum limits.
- Overusing Nested If: Too many nested if statements make code hard to read. Use else-if where possible.
Conclusion
The if-else statement in C forms the backbone of decision-making in programming. It allows your program to react intelligently based on conditions. Once you get comfortable with simple, multiple, and nested if-else structures, you can handle most logical problems with ease. The key is to write clear conditions and maintain proper structure in your code.Frequently Asked Questions
1. What is the difference between if and if-else in C?2. Can we use multiple else statements?The if statement executes code only when the condition is true, while if-else provides an alternative block when the condition is false.
3. What happens if no condition is true in else-if ladder?No, you can only use one else with an if, but you can use multiple else if conditions.
4. Is else mandatory in C?The else block executes if none of the conditions are true.
5. Can we nest multiple if statements?No, the else part is optional. You can use only if when needed.
Yes, you can nest multiple if statements, but too much nesting reduces readability.
0 Comments