if … elif … else – Python Conditional Statements

if … elif … else – Python Conditional Statements – कई बार हमें हमारे Program में ऐसी जरूरत पड़ती है, जहां Multiple Conditions में से किसी एक Condition के ही True होने की सम्‍भावना होती है। इस तरह की स्थिति को Handle करने करने के लिए Python हमें if … elif … else Conditional Statement Provide करता है, जिसका Syntax निम्‍नानुसार होता है-

[code]
# Simple if ... elif ... else Conditional Statement
if(TestCondition): ifStatement
elif: elifStatement
else: elseStatement
otherStatement

if(TestCondition):
    ifStatement
elif:
    elifStatement
else:
    elseStatement
otherStatement

# Compound if ... elif ... else Conditional Statement
if(TestCondition):
    ifStatement 1
    ifStatement 2
    ...
   ifStatement n
elif(TestCondition):
    ifStatement 1
    ifStatement 2
    ...
    ifStatement n
else:
    ifStatement 1
    ifStatement 2
    ...
    ifStatement n
otherStatement
[/code]

इस Statement के अनुसार यदि TestCondition True Return करता है, तो Program Control, ifStatement अथवा ifStatements Block का Execution करके सीधे ही otherStatement पर चला जाता है।

लेकिन यदि if का TestCondition True Return न करे, तो Program Control, if Condition से Associated Statement/Statement Block को Skip करके Just अगले elif TestCondition पर चला जाता है और elif के TestCondition को Test करता है।

अब यदि elif का TestCondition True Return करता है, तो Program Control elifStatement / elifStatement Block का Execution करके सीधे ही otherStatement पर चला जाता है। लेकिन यदि इस पहले elif का TestCondition True Return न करे, तो Program Control, इस पहले elif Condition से Associated Statement/Statement Block को Skip करके Just अगले elif TestCondition पर चला जाता है और इसी प्रक्रिया को फिर से Repeat करता है और तब तक Repeat करता रहता है, जब तक कि कोई Successive elif TestCondition True न हो जाए।

जबकि यदि कोई भी elif Statement का TestCondition True नहीं होता, तो अन्‍त में Default Statement के रूप में Program Control else Statement Block को Execute करके otherStatement पर चला जाता है।

यानी किसी भी स्थिति में इस पूरे Syntax के अनुसार केवल एक ही Statement Block का Execution हो सकता है और जैसे ही किसी Statement Block का Execution होता है, Program Control सीधे ही otherStatement पर चला जाता है।

इस तरह से इस Statement Syntax के अनुसार if, elif अथवा else Statement Block में से किसी एक ही Statement Block का Execution हो सकता है और किसी भी स्थिति में कभी भी ऐसा नहीं हो सकता कि तीनों ही Statement Blocks Execute न हों, न ही कभी कोई ऐसी स्थिति हो सकती है कि if, elifelse तीनों Statement Blocks एक साथ Execute हो जाऐं। जबकि otherStatement हमेंशा Execute होता है, फिर इस बात से कोई फर्क नहीं पड़ता कि if, elif या else Statement Block में से कौनसा Statement Block Execute हो रहा है।

इस तरह से Multiple Conditions में से किसी एक ही Condition के True हो सकने की स्थिति को Handle करने के लिए Python में हमें if … elif … else Statement को Use करना होता है। Python में इस Statement को Multiway – Branching Statement के नाम से भी जाना जाता है और ये काफी हद तक C/C++, Java, C# के if … else if … else Statement के समान ही है। लेकिन Python में Switch Statement जैसा कोई Concept नहीं है क्‍योंकि C/C++, Java, C# जैसी Languages के Switch Statement से सम्‍बंधित जरूरतों को Python में if … elif … else Statement द्वारा आसानी से Fulfill किया जा सकता है।

Python in Hindi - BccFalna.comये Article इस वेबसाईट पर Selling हेतु उपलब्‍ध EBook Python in Hindi से लिया गया है। इसलिए यदि ये Article आपके लिए उपयोगी है, तो निश्चित रूप से ये EBook भी आपके लिए काफी उपयोगी साबित होगी।

Python in Hindi | Page: 602 | Format: PDF

BUY NOW GET DEMO REVIEWS