if-else Statement in C
if-else Statement in C
Introduction
if-else Statement in C is a decision-making statement used to execute one block of code when a condition is true and another block of code when the condition is false. It helps programs choose between two different actions based on conditions.
In this C Programming Course in Jaipur, students learn how to use the if-else statement to build logical and interactive programs.
What is if-else Statement in C?
The if-else statement checks a condition.
- If the condition is true, the
ifblock executes. - If the condition is false, the
elseblock executes.
Syntax:
if(condition) {
// code if condition is true
}
else {
// code if condition is false
}
The if-else statement allows programs to make two-way decisions.
Flow of if-else Statement
Condition → True → if Block Executes
Condition → False → else Block Executes
Only one block executes at a time.
Example of if-else Statement
#include<stdio.h>
int main() {
int age = 16;
if(age >= 18) {
printf("Eligible to vote");
}
else {
printf("Not eligible to vote");
}
return 0;
}
Output:
Not eligible to vote
Since the condition is false, the else block executes.
Understanding Conditions
Conditions are expressions that return:
- True
- False
Example:
age >= 18
If true:
1
If false:
0
Using Relational Operators in if-else
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example:
if(number == 10)
Example Using User Input
#include<stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number % 2 == 0) {
printf("Even Number");
}
else {
printf("Odd Number");
}
return 0;
}
Output:
Even Number
or
Odd Number
depending on input.
Multiple Statements Inside if-else
Example:
#include<stdio.h>
int main() {
int marks = 45;
if(marks >= 50) {
printf("Pass\n");
printf("Congratulations");
}
else {
printf("Fail\n");
printf("Try Again");
}
return 0;
}
Output:
Fail
Try Again
if-else Ladder in C
The if-else ladder checks multiple conditions.
Syntax:
if(condition1) {
// code
}
else if(condition2) {
// code
}
else {
// code
}
Example:
#include<stdio.h>
int main() {
int marks = 75;
if(marks >= 90) {
printf("Grade A");
}
else if(marks >= 60) {
printf("Grade B");
}
else {
printf("Grade C");
}
return 0;
}
Output:
Grade B
Nested if-else Statement
An if-else statement inside another if-else statement is called nested if-else.
Example:
#include<stdio.h>
int main() {
int age = 25;
if(age >= 18) {
if(age <= 60) {
printf("Eligible");
}
else {
printf("Senior Citizen");
}
}
else {
printf("Minor");
}
return 0;
}
Output:
Eligible
Real-World Applications of if-else Statement
The if-else statement is used in:
- ATM systems
- Login authentication
- Banking software
- Online shopping applications
- Student result systems
- Traffic signal systems
Students learning C Programming Course in Jaipur use if-else statements in many real-world projects.
Common Errors in if-else Statement
Using Assignment Operator Instead of Comparison
Incorrect:
if(a = 10)
Correct:
if(a == 10)
Missing Curly Braces
Incorrect:
if(number > 0)
printf("Positive");
printf("Done");
Correct:
if(number > 0) {
printf("Positive");
printf("Done");
}
Best Practices
- Use proper indentation
- Write meaningful conditions
- Avoid unnecessary nesting
- Use braces for readability
- Test all possible conditions
Good coding practices improve program readability and reduce logical errors.
Importance of if-else Statement in C
The if-else statement helps:
- Make decisions
- Execute alternative actions
- Control program flow
- Build interactive applications
Decision-making statements are essential in software development.
Summary
if-else Statement in C is a decision-making statement used to execute one block of code if a condition is true and another block if the condition is false.
This lesson explained syntax, conditions, relational operators, nested if-else, if-else ladder, examples, and real-world applications in C programming.
FAQs
What is if-else Statement in C?
The if-else statement executes different blocks of code depending on a condition.
What happens if the condition is false?
The else block executes when the condition is false.
What is if-else ladder in C?
The if-else ladder checks multiple conditions one after another.
What is nested if-else in C?
Nested if-else means placing one if-else statement inside another.
Why is if-else important in programming?
It helps programs make decisions and execute different actions dynamically.
