Python Control Flow Statements - Detailed Guide
Control Flow Statements kya hoti hain?
Control Flow Statements programming mein code ke execution ke order ko control karte hain. Matlab hum decide karte hain ki program me kaunsa code kab chalega. Yeh programming me bahut important hai kyunki real world problems me humko decisions lene hote hain, baar-baar kaam repeat karne hote hain, aur conditions ke hisaab se alag-alag action lena hota hai.
1. Conditional Statements
Conditional statements se hum apne program ko decisions lene laayak banate hain. Matlab kuch condition true hone par hi koi specific code chalta hai, warna dusra code. Python me conditional statements hain: if
, if-else
, aur if-elif-else
.
If Statement
Agar ek simple condition check karni ho jisme code tabhi chale jab condition true ho, to if
ka use karte hain:
temperature = 30 if temperature > 25: print("Garmiyon ka mausam hai.")
Yahan print tabhi chalega jab temperature 25 se bada ho.
If-Else Statement
Agar do options me se ek choose karna ho, to if-else
ka use hota hai:
age = 17 if age >= 18: print("Aap vote de sakte hain.") else: print("Aap abhi vote nahi de sakte.")
If-Elif-Else Chain
Kabhi kabhi hume multiple conditions check karni hoti hain, to elif
ka use karte hain:
marks = 85 if marks >= 90: print("Grade: A") elif marks >= 75: print("Grade: B") elif marks >= 60: print("Grade: C") else: print("Fail")
2. Looping Statements (Repeating code)
Loops se hum koi code block baar-baar chala sakte hain jab tak condition satisfy nahi hoti. Python me do prakar ke loops hote hain: for
loop aur while
loop.
For Loop
Jab humko pata ho kitni baar repeat karna hai ya kisi sequence pe repeat karna hai, tab for
loop use karte hain:
for i in range(1, 6): print("Number:", i)
Yeh 1 se 5 tak numbers print karega.
While Loop
Jab humko repeat karna hai jab tak koi condition true hai, tab while
loop use karte hain:
count = 1 while count <= 5: print("Count:", count) count += 1
3. Loop Control Statements
Loops ke andar kuch control statements bhi hote hain jo loop ke behaviour ko badalte hain:
Break Statement
Loop turant rokna ho kisi condition par, to break
ka use karte hain:
for i in range(1, 10): if i == 5: break print(i) # Output: 1 2 3 4
Continue Statement
Loop ke current iteration ko skip karna ho aur next se continue karna ho, tab continue
use hote hain:
for i in range(1, 6): if i == 3: continue print(i) # Output: 1 2 4 5
Pass Statement
Agar loop ya condition me abhi koi code nahi likhna chahe, par syntax error nahi chahiye to pass
statement use karte hain:
for i in range(1, 4): if i == 2: pass # Yahan future me code add karenge else: print(i) # Output: 1 3
4. Examples Milake Samajhte Hain
Example 1: Agar user se age le kar check karen ki wo vote de sakta hai ya nahi.
age = int(input("Apni age batao: ")) if age >= 18: print("Aap vote dene ke liye eligible hain.") elif age > 12: print("Abhi aap school jate honge.") else: print("Aap abhi bahut chhote ho.")
Example 2: 1 se 10 tak even numbers print karna for loop se:
for num in range(1, 11): if num % 2 == 0: print(num, "ye even number hai")
Example 3: Countdown while loop se:
count = 5 while count > 0: print("Countdown:", count) count -= 1 print("Blast Off!")