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

if code तभी चलाता है जब condition सच हो, if-else 2 रास्तों में से चुनता है, else-if ladder कई में से एक चुनता है (grades के लिए perfect), और nested if एक सवाल के अंदर सवाल पूछता है।

11 min read · 10 cards · 3 checks

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


Theory

Program का पहला फ़ैसला

अब तक marks program 92 और 12 को एक जैसा मानता है: compute करो, print करो, बस। कोई judgement नहीं।

पर एक असली marksheet कुछ कहती है: pass या fail, और कौन सा grade।

आज program अपना रास्ता 2-राहा बनाता है: if यह, तो वह करो, else दूसरा करो। Decision-making वहीं है जहाँ programs calculator होना छोड़कर software बनने लगते हैं, और exams में Unit 2 आम तौर पर ठीक इसी परिवार से खुलता है।

Theory

Toll booth की क़तार

क़तार में लगे toll booths सोचिए, हर एक के पास एक सवाल। Simple if: एक booth, उसे संतुष्ट कीजिए तो gate खुलता है, नहीं तो आप बस आगे निकल जाते हैं। if-else: एक दो-राहा, एक सवाल, 2 रास्ते, एक तो लेना ही होगा। else-if ladder: क़तार में booths, जिस PEHLE वाले को संतुष्ट किया वह आपको बाहर मोड़ देता है, बाद के booths आपको देखते ही नहीं। Nested if: toll plaza की VIP lane के अंदर एक booth, एक सवाल जो पहले वाले को पार करने के बाद ही सामने आता है।

Theory

चारों रूप, ठीक-ठीक

  • Simple if: if (marks >= 35) { printf("Pass"); } block तभी चलाता है जब condition सच (non-zero) हो।
  • if-else: दूसरा रास्ता जोड़ता है: else { printf("Fail"); }
  • if-else if-else ladder: कई में से एक रास्ता, ऊपर से नीचे जाँचा जाता है, पहला सच जीतता है, बाक़ी छोड़ दिए जाते हैं।
  • Nested if: किसी if के block के अंदर एक if: पहले pass जाँचिए, और उसी के अंदर distinction जाँचिए।

Braces { } हर रास्ते के block को घेरते हैं। एक statement के लिए भी इस्तेमाल कीजिए; आगे वाले आप शुक्रगुज़ार होंगे।

Practical

Grade वाली ladder

#include <stdio.h>

int main() {
    int marks;
    printf("Enter marks: ");
    scanf("%d", &marks);

    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.

Think first

Ladder पर चलिए

ऊपर के code में marks = 65 को booth-दर-booth trace कीजिए। कौन सी conditions जाँची जाती हैं, कौन जीतती है, और क्या print होता है?

Show the answer

65 >= 70? नहीं, आगे बढ़िए। 65 >= 60? हाँ: Grade B print, और ladder बाहर निकल जाती है: 65 >= 35 कभी जाँचा ही नहीं जाता। पहला सच जीतता है, बाक़ी छूट जाते हैं।

इसीलिए ladder सबसे ऊँचा grade पहले जाँचती है: क्रम उलट दीजिए (35 पहले) और हर pass होता student Grade C बन जाता है, एक logic bug जिस पर compiler की कोई शिकायत नहीं।

Quiz

marks = 20 के लिए यह क्या print करता है? if (marks >= 35); { printf("Pass\n"); }

  1. Pass, semicolon ने if ख़त्म कर दिया, इसलिए block हमेशा चलता है
  2. कुछ नहीं, क्योंकि 20 condition में fail है
  3. semicolon पर compile error
  4. Fail
Show the answer

Pass, semicolon ने if ख़त्म कर दिया, इसलिए block हमेशा चलता है

if(...) के ठीक बाद वाला वह semicolon एक पूरा, ख़ाली statement है: "if condition, कुछ मत करो"। उसके बाद का { printf } block ALAG code है जो बिना शर्त चलता है, इसलिए 20 भी Pass print करता है। यह साफ़ compile होता है, इसीलिए = बनाम == के बाद यह C का सबसे मशहूर trap है। हर if को आवारा semicolon के लिए जाँचिए।

Quiz

Grade ladder में marks = 90 marks >= 70 और marks >= 60 दोनों संतुष्ट करता है। क्या print होता है?

  1. सिर्फ़ Grade A: ladder पहली सच condition पर रुक जाती है
  2. Grade A और Grade B, दोनों conditions सच हैं
  3. Grade B, बाद वाला match पहले वाले को override करता है
  4. Compile error: overlapping conditions
Show the answer

सिर्फ़ Grade A: ladder पहली सच condition पर रुक जाती है

else-if ladder एक फ़ैसला है, कई अलग जाँचों की श्रृंखला नहीं: ऊपर से नीचे, PEHLI सच condition execution अपने नाम कर लेती है और बाक़ी rungs पूरी तरह छूट जाते हैं। दोनों-print वाले जवाब ladder को अलग ifs मानते हैं, यही ग़लतफ़हमी पकड़ने के लिए यह सवाल बना है।

Watch out

3 if killers

if(...) के बाद semicolon: ख़ाली statement, block हमेशा चलता है (ऊपर)। condition में == की जगह =: assign करता है और सच पढ़ा जाता है, Unit 1 का trap अपने असली ठिकाने में। ग़लत ladder क्रम: 70 से पहले 35 जाँचिए और toppers को Grade C मिलता है। Nested ifs की bonus बारीकी: एक else सबसे नज़दीकी unmatched if के साथ जुड़ता है, इसलिए braces से वही कहिए जो आपका मतलब है।

Theory

Selection खुल गया

Structured programming के 3 आकार याद हैं? अब दूसरा आपके पास है: selection। Marks program judge कर सकता है। पर 5-option वाले MENU (marks देखो, percentage, grade, exit...) के लिए if-else ladder जल्दी बेढंगी हो जाती है, और C exact-match menus के लिए एक ख़ास औज़ार रखता है: switch-case, अगला lesson, break statements समेत।

Summary

Key takeaways

  • Simple if: condition सच हो तो block चलाओ; if-else: 2 अलग-अलग रास्ते।
  • else-if ladder: कई रास्ते, ऊपर से नीचे जाँचे जाते हैं, PEHLA सच जीतता है, बाक़ी छूटते हैं।
  • Ladder क्रम मायने रखता है: सबसे ऊँचा grade पहले जाँचिए।
  • Nested if: सवाल के अंदर सवाल (pass, फिर distinction)।
  • Killer traps: if(...) के बाद semicolon, == की जगह =, ग़लत ladder क्रम।
  • याद रखने का hook: toll booths, पहला संतुष्ट booth आपको बाहर मोड़ता है।

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