🟢 Beginner  ·  Lesson 11

if, if-else, else-if Ladder

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.

Frequently Asked Questions

What is the difference between if and if-else in C?
An if statement runs its block only when the condition is true and does nothing otherwise. An if-else statement runs one block when the condition is true and a second block when it is false, so exactly one of the two always executes.
Can we use if without else in C?
Yes. The else part is optional. You use a plain if when you only need to do something when a condition is true, and no action is required when it is false.
What is an else-if ladder in C?
An else-if ladder chains multiple conditions using if, else if, else ifelse. C checks each condition top to bottom and runs the block of the first true condition; the final else handles all remaining cases.
Why does if (a = 5) always run in C?
Because a = 5 is assignment, not comparison. It assigns 5 to a and the expression itself evaluates to 5, which is non-zero (true). To compare, use a == 5 with two equal signs.
What values are treated as true and false in C?
In C, any non-zero value is treated as true and 0 is treated as false. There is no separate boolean type in classic C, so integers act as conditions directly.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.