Nested if in Python in Hindi – Syntax, Flowchart, Programs

इस Nested if in Python in Hindi के chapter में हम सीखेंगे कि Python में Nested if का इस्तेमाल क्यों और कैसे किया जाता है। इसे और अच्छे से और आसानी से समझने के लिए इसमें flow-chart, examples और programs भी दिए गए हैं।

Nested if Statement

जब एक if statement को दूसरे if statement के अंदर रखा जाता है, तो उसे Nested if Statement कहते हैं। इसका इस्तेमाल तब किया जाता है जब कोई decision एक से ज़्यादा conditions पर निर्भर करता है, जिन्हें एक के बाद एक check करना ज़रूरी होता है।

Syntax:

if condition1:
    # Block of code executed if condition1 is True
    if condition2:
        # Block of code executed if both condition1 and condition2 are True
        statement(s)
    else:
        # Block of code executed if condition1 is True but condition2 is False
        statement(s)
else:
    # Block of code executed if condition1 is False
    statement(s)

Flow of Execution (execution का क्रम) – Nested if in Python

  • सबसे पहले condition1 को check किया जाता है।
  • अगर condition1 True है, तो program condition2 को check करता है।
  • अगर condition2 भी True है, तो inner if block execute होता है।
  • वरना, inner else block execute होता है।
  • और अगर condition1 ही False है, तो outer else block execute होता है।

Flow chart of Nested if

Example: Program to check No is postitve (Nested if का उपयोग करके)

num = float(input(“Enter a number”))
if num >= 0:
    if num == 0:
        print(“Zero”)
    else:
        print(“Positive Number”)
else:
    print(“Negative Number”)

Output:
Positive Number

Explanation

इस program में, बाहर वाला (outer) if check करता है कि number non-negative है या नहीं। उसके बाद अंदर वाला (inner) if check करता है कि वो zero है या positive.

Example: Largest Among Three Numbers (Nested if का उपयोग करके)

a = int(input(“Enter first number”))
b = int(input(“Enter second number”))
c = int(input(“Enter third number”))
if a >= b:
    if a >= c:
        print(“a is the largest:”, a)
    else:
        print(“c is the largest:”, c)
else:
    if b >= c:
        print(“b is the largest:”, b)
    else:
        print(“c is the largest:”, c)

Output:
(अगर a = 10, b = 25, c = 15 डालें तो)
b is the largest: 25

Explanation:

इस program में, सबसे पहले outer if check करता है कि a, b से बड़ा या बराबर है या नहीं।

  • अगर हाँ, तो inner if check करता है कि a, c से भी बड़ा या बराबर है या नहीं — अगर है, तो “a” सबसे बड़ा है, वरना “c” सबसे बड़ा है।
  • अगर a, b से बड़ा नहीं है, तो outer else चलता है, और वहाँ एक और inner if check करता है कि b, c से बड़ा या बराबर है या नहीं — अगर है, तो “b” सबसे बड़ा है, वरना “c” सबसे बड़ा है।

इस तरह nested if की मदद से तीनों numbers को आपस में compare करके largest number पता किया जाता है।

Similar Posts

Leave a Reply

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