If-Else Statement in C Programming
If-Else Statement in C Programming
Introduction to If-Else Statement
The if-else statement in C programming is used to make decisions when there are two possible outcomes. It executes one block of code if the condition is true and another block if the condition is false.
The if-else statement helps in handling alternative conditions in programs.
Syntax of If-Else Statement
// code if condition is true
} else {
// code if condition is false
}
Example of If-Else Statement
int main() {
int age = 16;
if(age >= 18) {
printf(“You are eligible to vote”);
} else {
printf(“You are not eligible to vote”);
}
return 0;
}
How If-Else Statement Works
- The condition is checked
- If true, the if block executes
- If false, the else block executes
If-Else with User Input
int main() {
int number;
printf(“Enter a number: “);
scanf(“%d”, &number);
if(number % 2 == 0) {
printf(“Even number”);
} else {
printf(“Odd number”);
}
return 0;
}
Nested If-Else Statement
You can use if-else inside another if-else statement.
int main() {
int marks = 85;
if(marks >= 50) {
if(marks >= 80) {
printf(“Grade A”);
} else {
printf(“Grade B”);
}
} else {
printf(“Fail”);
}
return 0;
}
Why If-Else Statement is Important
Key Benefits
- Handles multiple outcomes
- Improves decision-making
- Used in real-world applications
- Helps in logical branching
Start Learning C Programming
Practice if-else programs to build strong logic in C programming.
Summary
The if-else statement in C programming is used to execute different blocks of code based on conditions. It is essential for handling decisions in programs.
FAQs
What is if-else statement in C programming?
It is used to execute one block of code if a condition is true and another if false.
What is the difference between if and if-else?
If executes only when true, while if-else handles both true and false cases.
Can we use nested if-else?
Yes, we can use multiple if-else statements inside each other.
Where is if-else used?
It is used in decision-making and logical conditions.



