Else If Ladder in C Programming in Hindi with Example

C में जब एक ही value पर कई conditions एक के बाद एक check करनी हों, तो सबसे सीधा structure else if ladder होता है। इस article में syntax, execution order, condition ordering, mistakes और switch और else if ladder में difference — सब code के साथ समझेंगे। सभी programs  compile करके test किए गए हैं; output वही है जो आपको मिलेगा।

C में Decision Making क्या है?

हर condition एक expression होती है जिसकी एक value निकलती है। C में 0 का मतलब false है और कोई भी non-zero value (1, -7, 250) का मतलब true। इसीलिए C में if (marks) भी valid है — marks की value ही condition बन जाती है।

Decision making के लिए C में ये structures हैं:

  • if — condition true हो तभी काम करो।
  • if-else — दो में से एक रास्ता चुनो।
  • else if ladder — कई Condition में से एक रास्ता चुनो।
  • switch — एक integer value को fixed options से match करो।
  • Ternary ?: — छोटे दो-रास्ते decision के लिए।

What is Else If Ladder in C?

Else if ladder कई conditions को ऊपर से नीचे जोड़ती है। जो पहली condition true मिलती है, उसका block चलता है, और बाकी सारी conditions check ही नहीं होतीं। कोई भी true न मिले तो अंतिम else block चलता है।

ध्यान दें: else if कोई एक keyword नहीं है। यह दो अलग keywords हैं — else और if। C इसे ऐसे पढ़ता है:

else if (c2) S2 else S3  /* जो आप लिखते हैं */

else { if (c2) S2 else S3 }  /* जो C समझता है */

यानी ladder असल में else के अंदर बैठी हुई if है। इसी वजह से एक बार कोई branch चल गई तो पूरा ढांचा खत्म हो जाता है — नीचे वाली conditions तक control पहुँचता ही नहीं।

Else If Ladder का Syntax

if (condition1)
{
    /* Executes when condition1 is true */
}
else if (condition2)
{
    /* Executes when condition1 is false
       and condition2 is true */
}
else
{
    /* Executes when none of the above conditions are true */
}

else लिखना optional है, लेकिन गलत input को पकड़ने के लिए इसे रखना अच्छा practice है। ध्यान रखें कि else के बाद कोई condition नहीं आती — else (marks > 0) लिखना compile error है। else हमेशा ladder की आखिरी branch होती है।

else if Ladder कैसे Execute होता है

  1. condition1 evaluate होती है। True मिली तो वह block चलता है और ladder खत्म।
  2. False मिली तो condition2 evaluate होती है; यही क्रम नीचे तक चलता है।
  3. सारी false निकलीं तो else चलता है (अगर लिखा हो)।

मान लीजिए marks = 72 है:

StepConditionResultअगला कदम
172 >= 90falseअगली condition
272 >= 80falseअगली condition
372 >= 70trueBlock चला, ladder बंद
472 >= 60check नहीं हुई—

आखिरी line सबसे ज़रूरी है: 72 >= 60 सच है, फिर भी वह block नहीं चला, क्योंकि ऊपर ही match मिल गया था। और अगर सारी conditions false हों और else भी न लिखा हो, तो program चुपचाप ladder से आगे बढ़ जाता है — कोई error नहीं, कोई output नहीं। इसीलिए default else रखना उपयोगी है।

Flowchart

Else if ladder flowchart in C showing conditions checked one by one, execution of the first true block, and the final else block when all conditions are false.

Example Program: Grade System

इस program में user से लिए गए marks के आधार पर Grade display किया जाता है, जैसे marks >= 90 Grade A+, marks >= 80 Grade A, marks >= 70 Grade B और marks >= 60 Grade C.

#include <stdio.h>

int main(void)
{
    int marks;

    printf("Enter marks (0-100): ");

    /* Check whether the input is a valid number */
    if (scanf("%d", &marks) != 1) {
        printf("Error: enter a number only.\n");
        return 1;
    }

    /* Reject marks outside the valid range (0-100) */
    if (marks < 0 || marks > 100) {
        printf("Invalid marks.\n");
    }
    else if (marks >= 90) {
        /* Marks are 90 or above */
        printf("Grade: A+\n");
    }
    else if (marks >= 80) {
        /* Marks are 80 or above */
        printf("Grade: A\n");
    }
    else if (marks >= 70) {
        /* Marks are 70 or above */
        printf("Grade: B\n");
    }
    else if (marks >= 60) {
        /* Marks are 60 or above */
        printf("Grade: C\n");
    }
    else if (marks >= 40) {
        /* Marks are 40 or above */
        printf("Grade: D\n");
    }
    else {
        /* Marks are below 40 */
        printf("Grade: F (Fail)\n");
    }

    return 0;
}
▶️ Output Result
Enter marks (0-100): 75 Grade: B

85 >= 90 false, 85 >= 80 true — इसलिए A मिला और नीचे की conditions skip हो गईं। 150 डालने पर validation वाली पहली condition true हो जाती है, इसलिए grade print ही नहीं होता; बिना validation यही program 150 पर A+ output दिखता।

Else if ladder Practical Programs

1. Temperature classification (range + &&)

 इस program में user से temperature पूछा जाता है, और उस temperature के अनुसार message print किया जाता है जैसे : freezing, Cold, Pleasant या Hot.

#include <stdio.h>

int main(void)
{
    float temp;

    printf("Temperature: ");
    scanf("%f", &temp);

    /* Check which temperature range the value falls into */
    if (temp < 0) {
        printf("Freezing\n");
    }
    else if (temp >= 0 && temp < 15) {
        printf("Cold\n");
    }
    else if (temp >= 15 && temp < 30) {
        printf("Pleasant\n");
    }
    else if (temp >= 30 && temp < 40) {
        printf("Hot\n");
    }
    else {
        printf("Extreme heat\n");
    }

    return 0;
}
▶️ Output Result
Temperature: 22 Pleasant

Input 22 → Pleasant. यहाँ && दोनों शर्तें एक साथ check करता है।

2. Largest of three numbers

यह भी else if ladder का basic और common program है जिसमें user से 3 number input लिये जाते हैं, उन तीनों number में सबसे बड़े नंबर को find किया जाता है। 

#include <stdio.h>

int main(void)
{
    int a, b, c;

    printf("Enter three numbers: ");

    /* Make sure all three numbers are entered successfully */
    if (scanf("%d %d %d", &a, &b, &c) != 3) {
        printf("Error: Please enter three valid integers.\n");
        return 1;
    }

    /* Check whether a is greater than or equal to both b and c */
    if (a >= b && a >= c) {
        printf("Largest: %d\n", a);
    }
    /* If a is not the largest, check whether b is the largest */
    else if (b >= a && b >= c) {
        printf("Largest: %d\n", b);
    }
    else {
        /* If neither a nor b is the largest, c must be the largest */
        printf("Largest: %d\n", c);
    }

    return 0;
}
▶️ Output Result
Enter three numbers: 10 25 15 Largest: 25

10 25 15 → Largest: 25. >= की जगह > लिखने पर बराबर numbers (जैसे 5 5 5) पर कोई condition true नहीं होती और else गलत जवाब देता है।

3. Character classification (char ranges)

यह C Program user से एक character लेता है और if-else Condition aur ASCII range की मदद से बताता है कि वह character किस तरह का है, जैसे lower case, upper case, digit या special symbols.

#include <stdio.h>

int main(void)
{
    char ch;

    printf("Enter one character: ");
    scanf(" %c", &ch);

    /* Check whether the character is a lowercase letter */
    if (ch >= 'a' && ch <= 'z') {

        /* Check whether the lowercase letter is a vowel */
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            printf("Small vowel\n");
        }
        else {
            printf("Small consonant\n");
        }

    }
    /* Check whether the character is an uppercase letter */
    else if (ch >= 'A' && ch <= 'Z') {
        printf("Capital letter\n");
    }
    /* Check whether the character is a digit (0-9) */
    else if (ch >= '0' && ch <= '9') {
        printf("Digit\n");
    }
    else {
        /* The character does not belong to any of the above categories */
        printf("Special character\n");
    }

    return 0;
}
▶️ Output Result
Enter one character: b Small consonant

b → Small consonant, 7 → Digit, @ → Special character. ASCII में ‘a’ से ‘z’ तक values लगातार होती हैं, इसलिए range comparison काम करता है।

Conditions का Order सही रखना

Ladder पहली true condition पर रुक जाती है, इसलिए specific condition पहले और broad condition बाद में आनी चाहिए। नीचे एक ही value, marks = 95, दो अलग क्रम में चलाई गई है:

#include <stdio.h>

int main(void)
{
    int marks = 95;

    printf("Wrong order: ");

    /* This condition is true, so the remaining conditions are skipped */
    if (marks >= 40)
        printf("D\n");

    else if (marks >= 90)
        printf("A+\n");  /* This condition never gets a chance to execute */

    printf("Right order: ");

    /* Check the higher marks range first */
    if (marks >= 90)
        printf("A+\n");

    else if (marks >= 40)
        printf("D\n");

    return 0;
}
▶️ Output Result
Wrong order: D Right order: A+

पहले क्रम में 95 को भी D मिला, क्योंकि 95 >= 40 पहले ही true हो गई और marks >= 90 तक control पहुँचा ही नहीं। नियम सरल है: जो condition दूसरों को अपने अंदर समेट लेती है, वह नीचे जानी चाहिए।

Common Mistakes

गलतीगलत codeसही codeक्या होता है
Range गलत लिखनाif (10 < x < 20)if (x > 10 && x < 20)C पहले 10 < x का result (0 या 1) निकालता है, फिर 1 < 20 — हमेशा true
= बनाम ==if (x = 5)if (x == 5)Assignment हो जाता है; expression की value 5 (non-zero) यानी हमेशा true
if के बाद semicolonif (x > 0);if (x > 0) { … }; एक खाली statement है; block हर हाल में चलता है
Braces छोड़नाif (a) if (b) … else …हर block में { }else नज़दीकी unmatched if से जुड़ जाता है
अलग-अलग ifif (m>=90) A; if (m>=40) Pass;if … else if …एक input पर दो output छप सकते हैं
Validation न करनासीधे scanf फिर ladderपहले range check150 पर भी grade print हो जाता है

पाँचवीं गलती देखिए: if (m >= 90) printf(“A+ “); if (m >= 40) printf(“Pass “); — m = 95 पर A+ Pass दोनों print होते हैं। Ladder में सिर्फ A+ print होगा।

Nested If vs Else If Ladder

Ladder में conditions समानांतर होती हैं (एक value के अलग-अलग ranges)। Nested if में दूसरी जाँच पहली के अंदर होती है, यानी दूसरी condition पहली के true होने पर ही मायने रखती है।

int user_ok = 1, pass_ok = 0;
/* Check the password only if the username is correct */
if (user_ok == 1) {
    if (pass_ok == 1)
        printf("Login successful\n");
    else
        printf("Wrong password\n");
}
else {
    printf("Unknown user\n");
}

यहाँ ladder काम नहीं करेगी, क्योंकि “wrong password” का मतलब तभी है जब user मिल चुका हो। वहीं marks, age या temperature जैसे एक-दूसरे को exclude करने वाले cases के लिए ladder पढ़ने में साफ़ और छोटी रहती है।

Else If Ladder vs Switch

पहलूelse if ladderswitch
Condition का typeकोई भी expression (, &&, ||)सिर्फ एक integer-type expression (int, char, enum)
Range checkहाँनहीं (case में constant ही चलेगा)
Exact value matchहाँ, पर हर बार == लिखना पड़ता हैहाँ, सीधे case 3:
Floating point / stringहाँ (सावधानी से)नहीं, compile error
Readabilityranges और logic में बेहतरबराबर options की लंबी list में बेहतर

एक और अंतर: switch में हर case के बाद break भूलने पर control अगले case में गिर जाता है (fall-through), जबकि ladder में यह गलती संभव ही नहीं।

दोनों में से कोई “हमेशा बेहतर” नहीं है। day == 1 … day == 7 जैसी fixed list के लिए switch साफ़ दिखता है; marks >= 80 जैसी range के लिए ladder ही एकमात्र option है।

Floating point की एक बात: 0.1 + 0.2 == 0.3 C में false है, क्योंकि binary में ये values exactly store नहीं होतीं। इसलिए fabs(a – b) < 1e-9 जैसा tolerance use करें, सीधे == नहीं।

Best Practices

  • सबसे specific condition सबसे ऊपर रखें।
  • Ladder से पहले input validate करें।
  • हर block में braces लिखें, चाहे एक ही statement हो।
  • else को default/error case की तरह इस्तेमाल करें, और comparison में == लिखें।
  • बहुत लंबी ladder दिखे तो switch या table-driven approach सोचें।

Practice Questions

  1. Age categories बनाइए: 0–12 Child, 13–19 Teenager, 20–59 Adult, 60+ Senior — और negative age reject कीजिए।
  2. बिजली बिल slabs: पहले 100 units ₹3/unit, अगले 200 पर ₹5, उससे ऊपर ₹7।
  3. तीन numbers में से सबसे छोटा निकालिए।
  4. ऐसा program लिखिए जो बताए कि number 3 और 5 दोनों से divisible है, सिर्फ 3 से, सिर्फ 5 से, या किसी से नहीं।
  5. if (10 < x < 20) को सही कीजिए और तीन अलग values पर test कीजिए।

Conclusion ( निष्कर्ष )

इस post में आपने else if ladder in C को detail में सीखा, कैसे condition execute  होती हैं, Syntax Practice program, Common mistakes और Practice.

Else if ladder का पूरा behaviour दो बातों से तय होता है: conditions ऊपर से नीचे check होती हैं, और पहली true condition के बाद ladder बंद हो जाती है। Order सही रखें, input validate करें, और range के लिए && use करें — बाकी सब इसी नियम से निकलता है।

और यह post आपको कैसा लगा comment करके बतायें।  आपके दोंस्तों में जो C programming सीख रहें हैं उनके साथ share करें। 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top