BCA Sem 1 | Unit 2 | Computer Concepts & C Programming
If Statements (All Types) in C — BKNMU Junagadh
Simple If, If-Else, Nested If, and Else-If Ladder Decision Control Structures
if family of statements evaluates a condition as True (non-zero) or False (zero) to decide whether to execute or skip a block of code.
C provides four primary structural variations of the if statement to handle different levels of decision-making complexity:
1. Simple If
Executes a single block of code only when the given condition evaluates to True. Has no alternative branch.
2. If-Else
Provides two execution paths: one block executes if True, and the alternative block executes if False.
3. Else-If Ladder
Evaluates multiple conditions sequentially in a top-to-bottom chain until a True condition is encountered.
4. Nested If
Places an if or if-else statement inside another if block for multi-level condition verification.
Here is a detailed comparative summary of all four types of if statements used in C programming:
| Statement Type | Syntax & Structural Format | When to Use in Programs |
|---|---|---|
Simple If | if (condition) { | When an action is required ONLY if a condition is satisfied, and nothing needs to happen if it fails. |
If-Else | if (condition) { | When you have exactly two mutually exclusive outcomes (e.g., checking if a number is even or odd). |
Else-If Ladder | if (cond1) { ... } | When choosing among three or more distinct options or checking value ranges (e.g., student grading schemes). |
Nested If | if (cond1) { | When a second condition must be evaluated only AFTER a primary condition has already passed True. |
if statement (like if(5) or if(-10)) evaluates as True! Only an explicit zero (0) evaluates as False.
1. Simple If & If-Else Example:
#include <stdio.h>
int main() {
int age = 18;
// Simple If
if (age >= 18) {
printf("You are eligible to vote.\n");
}
// If-Else Statement
int num = 7;
if (num % 2 == 0) {
printf("%d is Even\n", num);
} else {
printf("%d is Odd\n", num);
}
return 0;
}
2. Else-If Ladder Example (Grading Scheme):
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 90) {
printf("Grade: Distinction\n");
} else if (marks >= 75) {
printf("Grade: First Class\n");
} else if (marks >= 50) {
printf("Grade: Second Class\n");
} else {
printf("Grade: Fail\n");
}
return 0;
}
3. Nested If Example (Find Largest of Three Numbers):
#include <stdio.h>
int main() {
int a = 12, b = 25, c = 9;
if (a >= b) {
if (a >= c)
printf("%d is the largest\n", a);
else
printf("%d is the largest\n", c);
} else {
if (b >= c)
printf("%d is the largest\n", b);
else
printf("%d is the largest\n", c);
}
return 0;
}
Enclose Conditions in Parentheses
The logical condition inside if(...) MUST always be placed within parentheses.
Avoid Semicolons After If
Never put a semicolon directly after the condition line (e.g., writing if(a > b); ends the statement prematurely!).
Use Braces `{}` for Multi-line Blocks
If a block contains more than one statement, you MUST enclose the block within curly braces {}.
else clause always pairs with the **nearest un-paired if** in the same scope level unless forced otherwise using curly braces {}.
if (x = 5) instead of if (x == 5) assigns 5 to x, which evaluates to non-zero (True) every time! Always use double equal signs == for comparison.
- Control Type: Conditional branching decision statement.
- 4 Variations: Simple If, If-Else, Else-If Ladder, Nested If.
- True / False Values: Non-zero = True; Zero (0) = False.
- Relational / Logical Operators: Used within conditions (
==,!=,>,<,&&,||). - Dangling Else: Matched to the nearest un-paired
if.