Python में Control Statements वे statements होते हैं जिनका इस्तेमाल प्रोग्राम के Flow को Control करने के लिए किया जाता है। Control Statements को Control Structures भी कहा जाता है।
Control Statements की मदद से हम Code के Execution को अलग-अलग Conditions के आधार पर Manage कर सकते हैं और decision-making को implement करते हैं।
Python में Control Statements तीन प्रकार के होते हैं:-
- Conditional Statements
- Looping Statements
- Jumping Statements

1:- Conditional Statements
Conditional Statements प्रोग्राम को विशेष condition के अनुसार execute करने की सुविधा प्रदान करते हैं। Conditional Statements के प्रकार निम्नलिखित हैं:-
(a) if Statement
if Statement का उपयोग तब किया जाता है जब हमें किसी Condition के True (सत्य) होने पर किसी कोड को Execute करना हो।
Syntax:
if condition:
# Execute this block of code if condition is True
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
यहां, condition age >= 18 check की जा रही है। यदि age 18 या उससे ज्यादा है, तो “You are eligible to vote.” print होगा।
(2) if else Statement
इसका उपयोग तब किया जाता है जब हमें कंडीशन के true होने पर एक code को और कंडीशन के False होने पर दूसरे कोड को Execute करना हो।
Syntax:
if condition:
# code to be executed if condition is True
else:
# code to be executed if condition is False
Example:
age = 16
if age >= 18:
print("Aap vote de sakte hain")
else:
print("Aap vote nahi de sakte")
यहां, age 16 है, तो output होगा “Aap vote nahi de sakte”।
(3) Nested if else Statement
Nested if..else statement का इस्तेमाल तब किया जाता है जब हमें एक condition के भीतर और दूसरी भी conditions को check करना हो। मतलब इसमें एक if statement के अंदर एक और if statement होता है।
Syntax:
if condition1:
if condition2:
# code to be executed if both conditions are true
else:
# code to be executed if condition1 is true and condition2 is false
else:
# code to be executed if condition1 is false
Example:
x = 10
y = 5
if x > 5:
if y < 10:
print("x is greater than 5 and y is less than 10")
else:
print("x is greater than 5 but y is not less than 10")
else:
print("x is not greater than 5")
Output:
x is greater than 5 and y is less than 10
(4) if-elif-else Statement
if-elif-else statement का इस्तेमाल तब किया जाता है जब हमें दो या दो से अधिक conditions को check करना हो और उन conditions के आधार पर अलग-अलग कार्य करने हों।
इसमें if block के बाद बहुत सारें elif (else if) blocks हो सकते हैं, और अंत में एक else block होता है जो तब execute होता है जब कोई भी condition true (सत्य) न हो।
Syntax:
if condition1:
# code to be executed if condition1 is true
elif condition2:
# code to be executed if condition2 is true
elif condition3:
# code to be executed if condition3 is true
else:
# code to be executed if none of the conditions are true
Example:
x = 10
if x > 15:
print("x is greater than 15")
elif x == 10:
print("x is equal to 10")
else:
print("x is less than 10")
Output:
x is equal to 10
2- Looping Statements
Python में loops का उपयोग तब किया जाता है जब हमें एक ही code को बार-बार execute करना होता है। Loops की मदद से हम बार-बार किए जाने वाले कार्यों को आसानी से handle कर सकते हैं। इसके प्रकार निम्नलिखित हैं:-
(a):- while Loop
while Loop तब तक Execute होते रहता है जब तक कि कंडीशन True रहती है।
Syntax:-
while condition:
# code block
Example:
i = 1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5
इस example में i की value 1 से start होती है और जब तक i की value 5 से कम या बराबर होती है, तो loop चलता रहेगा। i += 1 से i की value हर iteration में बढ़ती है, जिससे loop stop हो जाता है।
(b):- For Loop
For loop का इस्तेमाल तब किया जाता है जब हमें किसी sequence (जैसे list, tuple, string, आदि) के हर element पर iteration करनी हो।
Syntax:-
for variable in sequence:
# code block
Example:
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
(c):- Nested Loops
Nested loops वह loops होते हैं जिनमें एक loop के अंदर दूसरा loop होता है। Python में आप for loop या while loop के अंदर दूसरे loop का use कर सकते हैं। यह तब उपयोगी होते हैं जब हमें multi-dimensional data structures, जैसे कि matrixes, पर काम करना हो।
Example:
for i in range(1, 4): # Outer loop
for j in range(1, 4): # Inner loop
print(i, j)
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
3:- Jumping Statements
Jumping statements का उपयोग program के flow को एक point से दूसरे point पर ट्रांसफर करने के लिए किया जाता है। इसके प्रकार निम्नलिखित हैं:-
(a):- Pass Statement
Python में pass statement का उपयोग placeholder के रूप में किया जाता है। जब हमें किसी block (जैसे loop या function) को खाली छोड़ना होता है या जब हम किसी विशेष logic को implement करना चाहते हैं, लेकिन अभी उसे छोड़ना चाहते हैं, तब हम pass का उपयोग करते हैं।
Example:
for i in range(5):
if i == 3:
pass # Placeholder
else:
print(i)
Output:
0
1
2
4
यहाँ, जब i की value 3 होती है, तो pass statement execute होता है और बाकी के loop की तरह normal flow चलता है।
(b):- Break Statement
break statement का उपयोग loop को बंद करने के लिए किया जाता है। जब हम किसी loop (जैसे for या while) में condition को satisfy करते हैं, तो break statement इस loop को तुरंत बंद कर देता है।
Example:
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
यहाँ, जब i की value 5 होती है, तो break statement लूप को रोक देता है, और 5 के बाद कोई और number print नहीं होता।
(c):- Continue Statement
continue statement का उपयोग loop के वर्तमान iteration को skip करने के लिए किया जाता है। यह अगले iteration पर सीधे jump कर जाता है, बिना बाकी code को execute किए। इसका मतलब है कि जब condition satisfy होती है, तो बाकी के code को छोड़कर loop अगली iteration पर चला जाता है।
Example:
for i in range(5):
if i == 2:
continue
print(i)
Output:
0
1
3
4
यहाँ, जब i की value 2 होती है, तो continue statement के कारण उस iteration को skip कर दिया जाता है और अगले iteration (i = 3) से शुरू किया जाता है।
निवेदन:- आशा है कि यह पोस्ट आपके लिए उपयोगी रही होगी, इसे अपने दोस्तों के साथ share कीजिए और python से related कोई सवाल हो तो नीचे comment करके बताइए।