if…else

Python programming में decision making एक बहुत important concept है। Program हर बार केवल instructions को सीधा-सीधा execute नहीं करता, बल्कि कई situations में उसे condition के आधार पर decide करना होता है कि आगे कौन-सा block run होगा।
इसी काम के लिए if…else statement का use किया जाता है।

यह topic programming logic की शुरुआत करता है, क्योंकि यहीं से student यह सीखता है कि program को सोचने जैसी structure कैसे दी जाती है। जब कोई condition true हो, तो एक काम किया जाए, और यदि वह false हो, तो दूसरा काम किया जाए—यही if…else का basic idea है।

if statement क्या होता है

if statement का use तब किया जाता है जब किसी condition के true होने पर कोई block execute कराना हो।

इसका basic syntax होता है:

if condition:
statement

यदि condition true होगी, तो indented block run होगा।
यदि condition false होगी, तो वह block skip हो जाएगा।

Example

age = 18
if age >= 18:
print("You are eligible to vote")

Output

You are eligible to vote

यहाँ condition age >= 18 true है, इसलिए print() statement execute हुआ।


if…else क्या होता है

कई बार केवल true condition के लिए instruction देना काफी नहीं होता।
हमें false condition के लिए भी अलग action define करना पड़ता है।
ऐसी स्थिति में if...else statement use की जाती है।

इसका basic syntax होता है:

if condition:
statement1
else:
statement2

यदि condition true होगी, तो if block run होगा।
यदि condition false होगी, तो else block run होगा।


Basic Example of if…else

num = 7
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")

Output

Odd Number

यहाँ num % 2 == 0 false है, इसलिए else block execute हुआ।

यह example बहुत important है, क्योंकि इससे condition checking और decision making दोनों clear होते हैं।


if…else में condition कैसे काम करती है

Python में condition का result हमेशा True या False होता है।
Program इसी result के आधार पर decide करता है कि कौन-सा block run होगा।

उदाहरण:

marks = 40
if marks >= 33:
print("Pass")
else:
print("Fail")

Output

Pass

यहाँ condition marks >= 33 true है, इसलिए student pass माना गया।

यदि marks 33 से कम होते, तो else block चलता।


Indentation का महत्व

Python में if...else statements के साथ indentation बहुत important होती है।
जो statements किसी block के अंदर आती हैं, उन्हें properly indent करना जरूरी होता है।

Example

x = 10
if x > 5:
print("x is greater than 5")
print("Condition is true")
else:
print("Condition is false")

यहाँ print() की दोनों lines if block के अंदर हैं, क्योंकि वे same indentation के साथ लिखी गई हैं।

यदि indentation गलत हो जाए, तो program error दे सकता है।


Positive और Negative number check करने का example

num = -4
if num >= 0:
print("Positive Number")
else:
print("Negative Number")

Output

Negative Number

यह example real-life basic classification दिखाता है।


User input के साथ if…else

अब एक interactive example देखते हैं जहाँ user input लेता है और फिर condition check होती है।

age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")

Example Input

16

Output

Not eligible to vote

इस example से यह clear होता है कि if...else का use real input-based programs में कैसे होता है।


if…elif…else क्या होता है

कई बार केवल दो options नहीं होते।
हमें multiple conditions check करनी पड़ती हैं।
ऐसी स्थिति में elif use किया जाता है।

इसका syntax होता है:

if condition1:
statement1
elif condition2:
statement2
else:
statement3

यहाँ program पहले if condition check करेगा।
यदि वह false हुई, तो elif check होगा।
और यदि दोनों false हों, तो else block चलेगा।


Example of if…elif…else

marks = 75
if marks >= 80:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")

Output

Grade B

यहाँ पहली condition false है, लेकिन दूसरी condition true है, इसलिए Grade B print हुआ।


Multiple conditions का example

num = 0
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")

Output

Zero

यह example बहुत common है और if...elif...else structure को अच्छी तरह समझाता है।


Nested if क्या होता है

जब एक if statement के अंदर दूसरी if statement use की जाए, तो उसे nested if कहते हैं।

Example

num = 12
if num > 0:
if num % 2 == 0:
print("Positive Even Number")
else:
print("Positive Odd Number")
else:
print("Number is not positive")

Output

Positive Even Number

यहाँ पहले check किया गया कि number positive है या नहीं।
फिर उसके अंदर यह check हुआ कि वह even है या odd।

Nested if complex logic के लिए useful होता है।

Comparison operators का role

if...else statements में comparison operators बहुत important होते हैं।
Conditions बनाने के लिए यही operators use किए जाते हैं।

मुख्य comparison operators हैं:

  • == → equal to
  • != → not equal to
  • > → greater than
  • < → less than
  • >= → greater than or equal to
  • <= → less than or equal to

Example

x = 10
y = 20
if x < y:
print("x is smaller than y")
else:
print("x is greater than or equal to y")

Output

x is smaller than y

Logical operators के साथ if…else

Conditions को combine करने के लिए logical operators भी use किए जाते हैं।

मुख्य logical operators हैं:

  • and
  • or
  • not

Example with and

age = 20
citizen = True
if age >= 18 and citizen:
print("Eligible")
else:
print("Not eligible")

Output

Eligible

Example with or

day = "Sunday"
if day == "Sunday" or day == "Saturday":
print("Weekend")
else:
print("Weekday")

Output

Weekend

Real-life example: Largest of two numbers

a = 12
b = 8
if a > b:
print("a is greater")
else:
print("b is greater")

Output

a is greater

यह example बहुत common है और comparison का practical use दिखाता है।


Real-life example: Password check

password = input("Enter password: ")
if password == "python123":
print("Access Granted")
else:
print("Wrong Password")

यह example authentication जैसे basic real-life logic को दिखाता है।


Common mistakes in if…else

Beginners अक्सर कुछ common mistakes करते हैं।

सबसे पहली गलती = और == में confusion की होती है।
= assignment के लिए होता है, जबकि == comparison के लिए होता है।

दूसरी गलती indentation में होती है।
अगर block सही indentation के साथ न लिखा जाए, तो program error देगा।

तीसरी गलती conditions को गलत order में लिखने की होती है, especially if...elif...else में।

इसीलिए if...else सीखते समय syntax के साथ logic भी साफ होना चाहिए।


Simple complete program

num = int(input("Enter a number: "))
if num > 0:
print("Positive Number")
elif num < 0:
print("Negative Number")
else:
print("Zero")

Example Input

-9

Output

Negative Number

यह program conditions, input और decision making तीनों concepts को एक साथ दिखाता है।

इस topic की मुख्य बात

if...else Python में decision making का सबसे basic और सबसे important tool है।
इसकी मदद से program condition के आधार पर different actions ले सकता है।

इस topic में student यह सीखता है:

  • condition क्या होती है
  • if कैसे काम करता है
  • if...else कैसे use होता है
  • if...elif...else कब use करना चाहिए
  • nested if क्या होता है

यही logic आगे loops, functions और बड़े programs की foundation बनाता है।


Leave a Comment

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

Scroll to Top