Introduction
Decision‑making is a key part of programming. Often, a program must choose between different actions depending on certain conditions. Conditional statements make this possible by allowing programs to execute specific blocks of code when a condition is true, and skip or choose alternatives when it is false.
In C programming, conditional statements control the flow of execution and are essential for building interactive applications. The most common conditional statements are:
ifstatementif...elsestatementNested
ifstatementif...else ifladderswitchstatement
This guide explains each type with syntax, examples, and real‑world applications.
πΉ Program Execution Type
1. Sequential Execution
Statements run one after another in order.
#include <stdio.h>
int main() {
printf("Step 1\n");
printf("Step 2\n");
printf("Step 3\n");
return 0;
}
Output:
Step 1
Step 2
Step 3
2. Conditional Execution
Execution depends on conditions.
printf("Eligible to vote");
}
Runs only if age >= 18.
πΉ What Are Conditional Statements?
Conditional statements allow programs to make decisions.
Benefits:
Enable decision‑making
Improve flexibility
Enhance user interaction
Solve problems efficiently
1.
if...elseStatement
Executes one block if the condition is true, another if false.
Syntax:
if(condition) {
// true block
} else {
// false block
}
Example: Absolute Value
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num < 0)
printf("Absolute value = %d", abs(num));
else
printf("Number = %d", num);
return 0;
}
2. Nested if StatementAn if inside another if. Useful for multiple checks.
Example: Largest Number
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if(a > b) {
if(a > c)
printf("%d is largest", a);
else
printf("%d is largest", c);
} else {
if(b > c)
printf("%d is largest", b);
else
printf("%d is largest", c);
}
return 0;
}
3. if...else if LadderChecks multiple conditions sequentially.
Example: Student Grades
int main() {
int percentage;
printf("Enter percentage: ");
scanf("%d", &percentage);
if(percentage >= 75)
printf("Distinction");
else if(percentage >= 60)
printf("First Class");
else if(percentage >= 50)
printf("Second Class");
else if(percentage >= 40)
printf("Pass");
else
printf("Fail");
return 0;
}
4. switch StatementUsed when multiple fixed choices exist.
Syntax:
switch(expression) {
case value1: statements; break;
case value2: statements; break;
default: statements;
}
Example: Simple Calculator
int main() {
int choice, n1, n2;
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
printf("Enter two numbers: ");
scanf("%d %d", &n1, &n2);
printf("Enter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: printf("Addition = %d", n1+n2); break;
case 2: printf("Subtraction = %d", n1-n2); break;
case 3: printf("Multiplication = %d", n1*n2); break;
case 4: printf("Division = %d", n1/n2); break;
default: printf("Invalid Choice");
}
return 0;
}
πΉ Difference Between if-else and switchif-else
switch
- Handles complex conditions
- Works with single expressions
- Supports relational operators
- Uses fixed constant values
- Good for ranges
- Good for menu-driven programs
- Slower with many conditions
- Cleaner and faster with multiple choices
πΉ Real-World Applications
if-else | switch |
|---|---|
|
|
|
|
|
|
|
|
Banking Systems: Loan eligibility, balance checks
E-commerce: Discounts, product recommendations
Student Systems: Grade calculation, attendance checks
Login Systems: Username/password validation
Games: Player actions, scoring logic
Banking Systems: Loan eligibility, balance checks
E-commerce: Discounts, product recommendations
Student Systems: Grade calculation, attendance checks
Login Systems: Username/password validation
Games: Player actions, scoring logic
πΉ Best Practices
Keep conditions simple and readable.
Use switch for menu-driven programs.
Avoid deeply nested if statements.
Use meaningful variable names.
Test all possible conditions.
Keep conditions simple and readable.
Use switch for menu-driven programs.
Avoid deeply nested if statements.
Use meaningful variable names.
Test all possible conditions.
Conclusion
Conditional statements are the backbone of logical programming in C. They allow programs to make decisions, respond to user input, and execute different actions based on conditions.
By mastering if, if...else, nested if, if...else if ladder, and switch, you’ll gain the ability to build smarter, interactive, and efficient applications.
Learn conditional statements — and you’ll unlock the logic behind every powerful C program.
