if Statement in C
The if statement lets your program make decisions — execute different code based on whether a condition is true or false.
Types of if Statements
c
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
// ── Type 1: Simple if ─────────────────────────
if (marks >= 40)
printf("You have passed!\n");
// ── Type 2: if-else ───────────────────────────
if (marks >= 40)
printf("Result: PASS\n");
else
printf("Result: FAIL\n");
// ── Type 3: if-else-if Ladder (Grade System) ──
printf("\nGrade: ");
if (marks >= 90) printf("A+ (Outstanding)\n");
else if (marks >= 80) printf("A (Excellent)\n");
else if (marks >= 70) printf("B (Good)\n");
else if (marks >= 60) printf("C (Average)\n");
else if (marks >= 40) printf("D (Pass)\n");
else printf("F (Fail — Work harder!)\n");
// ── Type 4: Nested if ─────────────────────────
if (marks >= 40) {
if (marks >= 90)
printf("Eligible for scholarship!\n");
else
printf("Passed — keep improving!\n");
}
return 0;
}▶ Output
Enter marks (0-100): 85 You have passed! Result: PASS Grade: A (Excellent) Passed — keep improving!
📌 Note: Curly braces
{} are optional if the body has only ONE statement. But best practice is to ALWAYS use them to avoid confusion and bugs.