if, if-elif, if-elif-else, nested if statements

Conditional statements आपके code को dynamically चुनाव करने देते हैं, real time balance limits के आधार पर execution paths route करते हुए।

10 min read · 12 cards · 3 checks

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


Theory

College Canteen में चुनाव

कल्पना कीजिए अपने college canteen counter पर एक गरम samosa की तलब के साथ खड़े होना। अपना order देने से पहले, आपका दिमाग़ अपने-आप एक त्वरित जाँच करता है: क्या मेरे wallet में काफ़ी पैसे हैं? अगर हाँ, ख़रीदिए। अगर नहीं, छोड़िए। आपके Semester 1 BCA104 C labs में, इन चुनावों को लिखने के लिए अंतहीन parentheses और curly braces चाहिए थे। Python इन बिल्कुल, असली दुनिया के फ़ैसलों को brackets में डूबे बिना साफ़ और पठनीय कैसे दिखाता है?

Theory

Railway Track Switch

conditional statements को एक railway track switcher की तरह सोचिए। एक train एक अकेले track पर आगे बढ़ती है। जब यह एक junction पर पहुँचती है, lever की position तय करती है कि train बाएँ Platform 1 की ओर मुड़ती है या दाएँ Platform 2 की ओर switch करती है। train कभी एक साथ दोनों रास्तों पर नहीं जा सकती। lever की position एक true या false condition दर्शाती है जो आपके program को बिल्कुल एक track पर route करती है।

Theory

Decision Elements औपचारिक रूप से

Python में, conditional statements expressions को true या false के रूप में evaluate करके execution के flow को नियंत्रित करते हैं। if statement प्राथमिक gate mark करता है। elif (else if का संक्षिप्त) कई वैकल्पिक conditions को क्रमिक रूप से सँभालता है, पारंपरिक C switch-case block की जगह लेते हुए। else clause सारे बाक़ी cases पकड़ता है जब हर पिछली condition विफल होती है। गहरे logic checks जोड़ने के लिए, आप एक conditional block को सीधे दूसरे के अंदर embed करके एक nested if structure लिख सकते हैं।

At a glance

Table 1: Python conditional statement किस्में और design use cases।

Structure StyleKeyword Patternइसे कब इस्तेमाल करें
Simple Conditionifजब आप एक अकेले true outcome के लिए एक block चलाना चाहते हैं।
Alternative Choiceif-elseजब आपके पास बिल्कुल दो रास्ते हैं, एक true और एक false।
Multi-way Branchif-elif-elseजब आपको क्रम में कई conditions test करनी हों।
Layered Checksnested ifजब एक secondary condition एक primary condition के pass होने पर निर्भर हो।

Theory

Worked Example: Smart Expense Alerts

आइए अपने चलते project, PocketMoney के लिए एक पूर्ण tiered conditional tree लागू करें। हम अपना मौजूदा account balance जाँचेंगे और अलग resource thresholds के आधार पर customized financial health status messages print करेंगे।

Practical

PocketMoney Financial Status Routing

balance = 120

if balance > 500:
    print("Safe Zone: Stable balance.")
elif balance >= 100:
    print("Warning Zone: Control your spends!")
    if balance == 120:
        print("Alert: Exact budget match detected.")
else:
    print("Critical Zone: Overspent completely!")

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

Think first

Conditional Path को ट्रेस करें

ऊपर के Python script का मन में विश्लेषण कीजिए। 120 के एक balance value के साथ, terminal पर कौन सी बिल्कुल printed lines भेजी जाएँगी? tap करने से पहले चरण ट्रेस कीजिए।

Show the answer

script output करेगी:

Warning Zone: Control your spends!

Alert: Exact budget match detected.

क्यों? Python line 3 (120 > 500) को False के रूप में evaluate करता है, तो यह पहला block skip करता है। यह line 5 (120 >= 100) पर जाता है, जो True evaluate होती है। यह इस block में घुसता है, warning message print करता है, और फिर line 7 (120 == 120) पर nested if से टकराता है। चूँकि वह भी True है, यह alert output करता है। else block पूरी तरह अनदेखा किया जाता है।

Quiz

BCA204 Python और BCA104 C में conditional routing के बीच एक बड़ा संरचनात्मक अंतर क्या है?

  1. Python को condition statements के इर्द-गिर्द parentheses चाहिए।
  2. Python curly braces पूरी तरह छोड़ देता है और एक trailing colon से mark किए सख़्त indentation blocks इस्तेमाल करता है।
  3. Python एक अकेले if block से जुड़े कई else blocks की अनुमति देता है।
  4. Python में एक elif keyword नहीं है, developers को switch-case tools इस्तेमाल करने पर मजबूर करते हुए।
Show the answer

Python curly braces पूरी तरह छोड़ देता है और एक trailing colon से mark किए सख़्त indentation blocks इस्तेमाल करता है।

Python अनिवार्य condition parentheses और grouping curly braces को पूरी तरह ख़त्म कर देता है। बजाय इसके, यह condition line के अंत में एक trailing colon character पर निर्भर करता है, nested code statements के लिए एक जैसे indentation spaces के साथ मिलकर।

Quiz

इस समस्याग्रस्त code fragment पर विचार कीजिए: balance = 600 if balance > 100: print("Medium Balance") elif balance > 500: print("High Balance") execution पर क्या print होगा?

  1. Medium Balance
  2. High Balance
  3. दोनों statements print होंगे।
  4. SyntaxError: overlapping conditions
Show the answer

Medium Balance

Python conditional structures को ऊपर से नीचे evaluate करता है। जिस पल यह एक मेल खाती condition पाता है जो True देती है, यह उस ख़ास block को execute करता है और तुरंत पूरी statement structure से बाहर निकलता है। क्योंकि 600 > 100 True है, line 3 चलती है और बाद वाला elif पूरी तरह skip हो जाता है।

Watch out

Classic जाल: Colon Collapse

university exam papers में सबसे बार-बार marks-गँवाने वाली ग़लती if balance < 100 जैसे expressions लिखना और line के अंत में अनिवार्य colon character : जोड़े बिना अगली line पर जाना है। Python engine इस punctuation token से एक execution block खोलने की उम्मीद करता है। इसे छोड़ना किसी भी code के execute होने से पहले एक runtime SyntaxError trigger करता है।

Theory

Conditions को Semester 3 से जोड़ना

Conditional checks सुरक्षित database interaction के द्वारपाल हैं। Semester 3 (BCA303) में, जब SQLite tables द्वारा powered dynamic applications बनाते हैं, आप alterations commit करने से पहले text constraints validate करने के लिए conditional lines इस्तेमाल करेंगे, जैसे if user_role == "Admin": execute_db_delete()।

Summary

Key takeaways

  • Conditional structures expressions को evaluate करके execution flow dynamically route करती हैं।
  • elif keyword क्रमिक multi-way testing देता है, C switch-case की जगह लेते हुए।
  • Python पहला True block execute करने के तुरंत बाद expression matching समाप्त करता है।
  • Nested loops और nested conditional blocks को सख़्त, एकसमान spacing levels चाहिए।
  • Trailing colons सारे choice keywords के लिए अनिवार्य संरचनात्मक घटक हैं।
  • Memory Hook: Colons blocks खोलते हैं, indentation tracks नियमित करता है!

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 Conditions, Loops and Strings

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

if, if-elif, if-elif-else, nested if statements · Programming Skills · Gri-Learn