if, if-else, else-if Ladder
if-else Statement
The if Statement
The if statement executes a block of code only when a condition is true.
Syntax:
if (condition) { // Code runs only if condition is true (non-zero) }
#include <stdio.h> int main() { int marks = 75; if (marks >= 40) { printf("Pass!\n"); } printf("Marks: %d\n", marks); return 0; }
The if-else Statement
Execute one block if true, another if false.
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (num % 2 == 0) { printf("%d is Even\n", num); } else { printf("%d is Odd\n", num); } return 0; }
else-if Ladder
Check multiple conditions one by one. The first true condition executes and the rest are skipped.
#include <stdio.h> int main() { int marks; printf("Enter marks (0-100): "); scanf("%d", &marks); if (marks >= 90) { printf("Grade: A+ (Excellent!)\n"); } else if (marks >= 80) { printf("Grade: A (Very Good)\n"); } else if (marks >= 70) { printf("Grade: B (Good)\n"); } else if (marks >= 60) { printf("Grade: C (Average)\n"); } else if (marks >= 40) { printf("Grade: D (Pass)\n"); } else { printf("Grade: F (Fail)\n"); } return 0; }
Nested if-else
An if-else inside another if-else. Used for checking multiple interdependent conditions.
#include <stdio.h> int main() { int a, b, c; printf("Enter 3 numbers: "); scanf("%d %d %d", &a, &b, &c); if (a >= b && a >= c) { printf("Largest: %d\n", a); } else if (b >= a && b >= c) { printf("Largest: %d\n", b); } else { printf("Largest: %d\n", c); } return 0; }
More Example Programs
Leap Year Check
#include <stdio.h> int main() { int year; printf("Enter year: "); scanf("%d", &year); if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { printf("%d is a Leap Year\n", year); } else { printf("%d is NOT a Leap Year\n", year); } return 0; }
Summary
if— runs block only when condition is trueif-else— runs one block or another based on conditionelse-if ladder— checks multiple conditions sequentiallynested if— if inside another if, for complex conditions- Condition in C: any non-zero value = true, 0 = false
- Don't use
=(assignment) when you mean==(comparison)!
Write programs: (1) Check positive/negative/zero (2) Triangle type from 3 sides (3) Voting eligibility (age >= 18) (4) BMI calculator with category.
if Statement
if statement एक block of code को तभी execute करता है जब condition true (non-zero) हो।
int marks = 75; if (marks >= 40) { printf("Pass!\n"); // यह तभी चलेगा जब marks >= 40 }
if-else Statement
True होने पर एक block, false होने पर दूसरा block execute करता है।
int num = 7; if (num % 2 == 0) { printf("Samagra (Even)\n"); } else { printf("Visham (Odd)\n"); }
else-if Ladder
एक के बाद एक multiple conditions check करता है। पहली true condition execute होती है, बाकी skip हो जाती हैं।
int marks = 85; if (marks >= 90) printf("A+ (Excellent)\n"); else if (marks >= 80) printf("A (Bahut Achha)\n"); else if (marks >= 70) printf("B (Achha)\n"); else if (marks >= 40) printf("Pass\n"); else printf("Fail\n");
Nested if-else
एक if-else के अंदर दूसरा if-else। Multiple interdependent conditions के लिए use होता है।
#include <stdio.h> int main() { int a, b, c; printf("Teen numbers daalen: "); scanf("%d %d %d", &a, &b, &c); if (a >= b && a >= c) printf("Sabse Bada: %d\n", a); else if (b >= c) printf("Sabse Bada: %d\n", b); else printf("Sabse Bada: %d\n", c); return 0; }
और Programs
#include <stdio.h> int main() { int age; printf("Apni age daalen: "); scanf("%d", &age); if (age >= 18) printf("Aap vote de sakte hain!\n"); else printf("Aap vote nahi de sakte. %d saal baad aana.\n", 18-age); return 0; }
सारांश
if— condition true होने पर block execute होता हैif-else— condition के basis पर दो blocks में से एक चलता हैelse-if ladder— multiple conditions को sequentially check करता हैnested if— complex conditions के लिए if के अंदर if- C में: कोई भी non-zero value = true, 0 = false
Programs लिखें: (1) Positive/negative/zero check (2) तीन में से सबसे बड़ा (3) Voting eligibility (4) Number positive, negative, या zero है?