🟢 Beginner  ·  Lesson 11

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:

Syntax
if (condition) {
    // Code runs only if condition is true (non-zero)
}
C Language
#include <stdio.h>
int main() {
    int marks = 75;
    if (marks >= 40) {
        printf("Pass!\n");
    }
    printf("Marks: %d\n", marks);
    return 0;
}
Pass! Marks: 75

The if-else Statement

Execute one block if true, another if false.

C Language
#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;
}
Enter a number: 7 7 is Odd

else-if Ladder

Check multiple conditions one by one. The first true condition executes and the rest are skipped.

C Language – Grade Calculator
#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;
}
Enter marks (0-100): 85 Grade: A (Very Good)

Nested if-else

An if-else inside another if-else. Used for checking multiple interdependent conditions.

C Language – Largest of 3 Numbers
#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;
}
Enter 3 numbers: 45 78 23 Largest: 78

More Example Programs

Leap Year Check

C Language
#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;
}
Enter year: 2024 2024 is a Leap Year

Summary

  • if — runs block only when condition is true
  • if-else — runs one block or another based on condition
  • else-if ladder — checks multiple conditions sequentially
  • nested 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)!
🏋️ Practice

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) हो।

C Language
int marks = 75;
if (marks >= 40) {
    printf("Pass!\n");  // यह तभी चलेगा जब marks >= 40
}

if-else Statement

True होने पर एक block, false होने पर दूसरा block execute करता है।

C Language
int num = 7;
if (num % 2 == 0) {
    printf("Samagra (Even)\n");
} else {
    printf("Visham (Odd)\n");
}
Visham (Odd)

else-if Ladder

एक के बाद एक multiple conditions check करता है। पहली true condition execute होती है, बाकी skip हो जाती हैं।

C Language – Grade Calculator
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");
A (Bahut Achha)

Nested if-else

एक if-else के अंदर दूसरा if-else। Multiple interdependent conditions के लिए use होता है।

C Language – Teen Sankhya mein Sabse Bada
#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

C Language – Voting Eligibility
#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
🏋️ Practice

Programs लिखें: (1) Positive/negative/zero check (2) तीन में से सबसे बड़ा (3) Voting eligibility (4) Number positive, negative, या zero है?

← Back to C Tutorial