Important Notice:

Nested if Statement

Nested if Statement

21 views 2 min read

Nested if Statement :-

Nested if statement Python में एक Conditional Statement (शर्तीय कथन) है, जिसमें एक if statement के अंदर दूसरा if statement लिखा जाता है। इसका उपयोग multiple conditions को step-by-step check करने के लिए किया जाता है।

सबसे पहले बाहरी if की condition check होती है। यदि वह condition True (सही) होती है, तभी उसके अंदर मौजूद दूसरा if statement check किया जाता है।

यदि बाहरी if condition False (गलत) होती है, तो उसके अंदर का पूरा code execute नहीं होता।

Python में Nested if के प्रत्येक block में Indentation (रिक्त स्थान) देना आवश्यक होता है। सामान्यतः प्रत्येक level के लिए 4 spaces का indentation प्रयोग किया जाता है।

English

A Nested if statement is a conditional statement in Python in which one if statement is placed inside another if statement. It is used to check multiple conditions step-by-step.

First, the outer if condition is checked. If it is True, the inner if statement is checked.

If the outer if condition is False, the code inside it is not executed.

In Python, proper Indentation (spaces) is required for each nested block. Generally, 4 spaces are used for each level of indentation.

Syntax:-
 
if condition1:
    if condition2:
        statement
 
Example:
 
age = 20
has_id = True

if age >= 18:
    if has_id:
        print("Entry Allowed")
 
Output:
 
Entry Allowed
 
Because:
20 >= 18 → True
has_id → True

दोनों conditions True हैं, इसलिए Entry Allowed print होगा।

Another Example:-
 
marks = 75
if marks >= 40:
    if marks >= 60:
        print("First Division")
 
Output:
First Division
 
Because:
75 >= 40 → True
75 >= 60 → True

इसलिए inner if का code execute होगा।

Nested if with else :-
 
age = 16
if age >= 18:
    if age >= 60:
        print("Senior Citizen")
    else:
        print("Adult")
else:
    print("Minor")
 
Output:
 
Minor
 
Flow:-
 
age >= 18
    ↓
 False
    ↓
  Minor
 
Important Points :-
  • एक if के अंदर दूसरा if → Nested if
  • Outer if पहले check होता है।
  • Inner if तभी check होता है जब outer if True हो।
  • Nested if में proper indentation बहुत महत्वपूर्ण है।
  • प्रत्येक nested level पर सामान्यतः 4 spaces का indentation दिया जाता है।
  • Nested if का उपयोग complex decision-making में किया जाता है।

Related Notes