if statements: simple if, if-else, if-else if-else, Nested if

if अपना block तभी चलाता है जब condition nonzero हो, else दूसरा रास्ता लेता है, और ladder सिर्फ़ अपनी पहली true rung चलाती है।

5 min read · 12 cards · 5 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक program जो झूठ बोलता है

राहुल के 20 marks आए, और result program ने Pass print कर दिया:

if (marks >= 35);

printf("Pass");

यह compile हो जाता है, फिर भी एक character झूठ बोल रहा है। कौन-सा?

Think first

झूठा पकड़िए

जिस character पर शक है, उसे type कीजिए।

Show the answer

(marks >= 35) के बाद वाला semicolon। वह if को एक खाली statement पर ख़त्म कर देता है, इसलिए printf if के बाहर है और हर किसी के लिए चलता है।

Theory

if, else, else if

if (condition) { } अपना block तभी चलाता है जब condition true हो, और C में true का मतलब है nonzero। else { } दूसरा रास्ता जोड़ता है, इसलिए दोनों में से ठीक एक चलता है। else if की rungs जोड़ते जाइए तो एक ladder बनती है, जो ऊपर से जाँची जाती है।

Practical

Grade वाली ladder

#include <stdio.h>

int main() {
    int marks = 65;

    if (marks >= 70) {
        printf("Grade A\n");
    } else if (marks >= 60) {
        printf("Grade B\n");
    } else if (marks >= 35) {
        printf("Grade C\n");
    } else {
        printf("Fail\n");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

ऊपर की ladder में marks 65 है। क्या print होता है?

  1. Grade A
  2. Grade B
  3. Grade B, फिर Grade C
  4. Grade C
Show the answer

Grade B

65 >= 60 पहली true rung है, इसलिए Grade B print होता है और ladder रुक जाती है; 65 >= 35 जाँचा ही नहीं जाता।

Think first

Ladder उलट दीजिए

Rungs उलट दीजिए: पहले >= 35, आख़िर में >= 70। 92 marks वाले topper को क्या मिलेगा?

Show the answer

Grade C। 92 >= 35 पहली ही rung पर true है, इसलिए ladder वहीं रुक जाती है, न कोई error, न कोई warning। सबसे सख़्त rung सबसे पहले रखिए।

Theory

if के अंदर if

Nested if किसी दूसरे if के block के अंदर बैठा होता है, इसलिए वह बाहर वाले if की हाँ के बाद ही चलता है। Braces के बिना else उस सबसे पास वाले if से जुड़ता है जिसका अपना else अभी नहीं है, इसलिए braces लगाइए।

Practical

Nested if

#include <stdio.h>

int main() {
    int marks = 80;

    if (marks >= 35) {
        printf("Pass\n");
        if (marks >= 75) {
            printf("Distinction\n");
        }
    } else {
        printf("Fail\n");
    }
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Trace कीजिए

ऊपर के program में marks 80 है। जो कुछ print होता है, सब type कीजिए।

Show the answer

Pass, फिर Distinction। अंदर वाला if बाहर वाले block के अंदर एक और statement है, इसलिए वह एक line जोड़ता है; Pass की जगह नहीं लेता।

Practical

Exam-style: output बताइए

#include <stdio.h>

int main() {
    int x = 0;

    if (x = 5)
        printf("Five");
    else
        printf("Zero");

    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

Exam-style: ऊपर वाले program का output क्या है?

  1. Five
  2. Zero
  3. Compile error
  4. कुछ भी print नहीं होता
Show the answer

Five

x = 5, x में 5 assign करता है, और उसकी value 5 nonzero है, इसलिए condition true है। = को == पढ़ने से ग़लत जवाब, Zero, आता है।

Summary

Key takeaways

  • if अपना block तभी चलाता है जब condition nonzero हो।
  • Ladder सिर्फ़ अपनी पहली true rung चलाती है, इसलिए सबसे सख़्त rung पहले रखिए।
  • Nested if बाहर वाले की हाँ का इंतज़ार करता है; else सबसे पास वाले ऐसे if से जुड़ता है जिसका else अभी नहीं है।
  • याद रखने का hook: if के बाद semicolon नहीं, comparison के लिए ==।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Decision Making and Iterative statements

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

if statements: simple if, if-else, if-else if-else, Nested if · Computer Programming and Programming Methodology (CPPM) · Gri-Learn