if, elif, else Statements
Control Flow ဆိုတာ ဘာလဲ?
Control Flow ဆိုတာ Program ရဲ့ စီးဆင်းမှုကို ထိန်းချုပ်တာ။ Code တွေက အပေါ်ကနေ အောက်ကို တစ်ကြောင်းချင်း Run သွားတယ်။ ဒါပေမယ့် တစ်ခါတစ်လေ ကျနော်တို့က ဆုံးဖြတ်ချက်ချရတယ်။
ဥပမာ: အသက် 18 ကျော်ရင် ဝင်လို့ရတယ်။ မကျော်ရင် အိမ်ပြန်ရမယ်။
အဲ့လို ဆုံးဖြတ်ချက်ချဖို့ if, elif, else Statements တွေကို သုံးတယ်။
if (ပထမဆုံး စစ်မယ်)
if ဆိုတာ ပထမဆုံး မေးခွန်း။ ဒါ မှန်လား? (Is this True?)
Syntax (ရေးနည်း) က အရေးကြီးတယ်:
- if လို့ရေးမယ်။
- စစ်ချင်တဲ့ condition (ဥပမာ age > 18) ကို ရေးမယ်။
- Colon (:) ကို လုံးဝ မမေ့နဲ့။ ဒါက အသက်။
- INDENTATION (Space ၄ ချက် / Tab ၁ ခေါက်)။
if age > 18: ဆိုရင် မှန်တယ်။
if age > 18 ဆိုရင် Error တက်မယ်။
Indentation ဆိုတာ ဘာလဲ?
Python မှာ if အောက်က code က အတွင်း (Space ၄ ချက်) ရွှေ့ဝင်သွားရတယ်။ အဲ့ဒီ ရွှေ့ဝင်သွားတဲ့ code တွေက if မှန် (True) မှ အလုပ်လုပ်ရမယ့် သူ့ တပည့်တွေ။
Indentation အမှန်:
age = 20
if age > 18:
# ဒီ line က Space ၄ ချက် ရွှေ့ထားတယ် (if ရဲ့ တပည့်)
print("bro Club ဝင်လို့ရပြီ။")
print("Welcome!")
# ဒီ line က if ရဲ့ အပြင်မှာ (Indentation မရှိ)
print("ဒါက if အပြင်မှာ။ အမြဲတမ်း run မယ်။")မှားနေတဲ့ Indentation:
age = 20
if age > 18:
print("Error! Indentation မရှိဘူး") # IndentationError တက်မယ်- if အောက်က code မှာ Space/Tab မထည့်ရင် IndentationError တက်တယ်။
- Space ၄ ချက် သို့မဟုတ် Tab ၁ ခေါက် သုံးရမယ်။
- Space နဲ့ Tab ကို မရောသုံးနဲ့။ တစ်ခုတည်း သုံးပါ။
- Python က Indentation နဲ့ Code Block ကို ခွဲခြားတယ်။
အခြေခံ if Statement:
age = 20
# စစ်ပြီ... (20 > 18) က True လား?
if age > 18:
# True တယ်... ဒါကြောင့် အတွင်းကကောင်ကို Run မယ်
print("bro Club ဝင်လို့ရပြီ။")
print("ဒါက if အပြင်မှာ။ အမြဲတမ်း run မယ်။")Output:
bro Club ဝင်လို့ရပြီ။
ဒါက if အပြင်မှာ။ အမြဲတမ်း run မယ်။else (ဒါမှ မဟုတ်ရင်)
if က မှား (False) သွားရင် ဘာလုပ်မလဲ? အဲ့ဒီအတွက် else က Plan B အနေနဲ့ ရှိတယ်။
if က မှားခဲ့ရင်... ဆိုတဲ့ အဓိပ္ပာယ်။
if နဲ့ else:
age = 16 # အသက် မပြည့်ဘူး
# စစ်ပြီ... (16 > 18) က True လား?
if age > 18:
# False သွားပြီ... ဒါကြောင့် ဒီ code ကို ကျော်သွားမယ်
print("bro Club ဝင်လို့ရပြီ။")
else:
# if က မှားလို့ ဒီ else အောက်ကကောင်ကို Run မယ်
print("Sorry။ အိမ်ပြန်။")Output:
Sorry။ အိမ်ပြန်။else က if မှားတာနဲ့ တန်း run တာ။
elif (နောက်ထပ် Option တွေ)
elif ဆိုတာ Else If ကို အတိုကောက်။
bro မှာ စစ်ဆေးစရာတွေက ၂ ခုမကဘူး။ Option တွေ အများကြီး ရှိနေရင် သုံးတယ်။
ဥပမာ - Cinema လက်မှတ်စစ်မယ်:
- if bro က VIP... (Sofa)
- elif (ဒါမှမဟုတ်) bro က First Class... (Good seat)
- elif (ဒါမှမဟုတ်) bro က Economy... (Normal seat)
- else... (လက်မှတ်မရှိရင် အိမ်ပြန်)
ဥပမာ (၃) - Grade စစ်မယ်:
score = 85
if score >= 90:
print("Grade A+... စာဂျပိုး။")
elif score >= 80: # 90 မကျော်ဘူး၊ ဒါပေမယ့် 80 ကျော်လား?
print("Grade A... မဆိုးဘူး။")
elif score >= 60:
print("Grade B... အိုကေ။")
else:
# အပေါ်က တစ်ကောင်မှ မမှန်တော့ဘူးဆိုရင်
print("Grade C... သွားပြီ။")Output:
Grade A... မဆိုးဘူး။score = 85 က score >= 80 မှာ မှန်သွားတာနဲ့၊ သူက Grade A... မဆိုးဘူး။ ကို print ထုတ်ပြီး၊ ကျန်တဲ့ elif နဲ့ else တွေကို လုံးဝ ပြန်မစစ်တော့ဘူး။ တစ်တွဲလုံးက ပြီးသွားပြီ။ ကိစ္စရှင်းတယ်။
- if - အမြဲတမ်း အရင်ဆုံး။
- elif - if နောက်မှာ၊ else ရှေ့မှာ။ အများကြီး ထည့်လို့ရတယ်။
- else - အမြဲတမ်း နောက်ဆုံး။
Multiple Conditions
if Statement မှာ Comparison Operators နဲ့ Logical Operators တွေကို ပေါင်းသုံးလို့ရတယ်။
1. Comparison Operators နဲ့ သုံးတာ:
age = 25
score = 95
# Greater than
if age > 18:
print("အရွယ်ရောက်ပြီ")
# Equal to
if score == 100:
print("Perfect!")
else:
print("Good!")
# Not equal to
if age != 30:
print("အသက် 30 မဟုတ်ဘူး")2. Logical Operators (and, or, not) နဲ့ သုံးတာ:
and - နှစ်ခုလုံး မှန်မှ True:
age = 20
money = 10000
# Club ဝင်ခွင့် ရဖို့: အသက် 18 ကျော်ရမယ် AND ပိုက်ဆံ 5000 ကျော် ရှိရမယ်
if age > 18 and money > 5000:
print("Club ဝင်လို့ရပြီ!")
else:
print("Club ဝင်မရပါ")or - တစ်ခုခု မှန်ရင် True:
has_ticket = False
has_vip_pass = True
# ဝင်ခွင့် ရဖို့: လက်မှတ် ရှိရမယ် OR VIP Pass ရှိရမယ်
if has_ticket or has_vip_pass:
print("ဝင်ခွင့်ရှိပါတယ်")
else:
print("ဝင်ခွင့်မရှိပါ")not - ပြောင်းပြန်လှန်တာ:
is_raining = False
# မိုးမရွာဘူးဆို ထွက်မယ်
if not is_raining:
print("အပြင်ထွက်လို့ရတယ်")
else:
print("အိမ်မှာနေ")3. Complex Conditions:
ကွင်း () သုံးပြီး condition တွေကို ပေါင်းလို့ရတယ်။
age = 25
income = 50000
has_job = True
# Credit Card လျှောက်ခွင့်:
# (အသက် 21 ကျော် AND ဝင်ငွေ 30000 ကျော်) OR အလုပ်ရှိရမယ်
if (age > 21 and income > 30000) or has_job:
print("Credit Card လျှောက်ခွင့်ရှိပါတယ်")
else:
print("Credit Card လျှောက်ခွင့်မရှိပါ")score = 75
attendance = 80
has_project = True
# Pass ဖို့:
# (Score 60 ကျော် AND Attendance 75% ကျော်) OR Project ပြီးပြီးရင်
if (score >= 60 and attendance >= 75) or has_project:
print("Pass!")
else:
print("Fail!")Nested if Statements (if ထဲက if)
if Statement ထဲမှာ နောက်ထပ် if Statement ထပ်ထည့်လို့ရတယ်။ ဒါကို Nested if လို့ခေါ်တယ်။
ဥပမာ (၁) - ရိုးရှင်းတဲ့ Nested if:
age = 20
has_id = True
# အပြင်က if - အသက်စစ်မယ်
if age >= 18:
print("အသက် 18 ပြည့်ပြီ")
# အတွင်းက if - ID Card စစ်မယ်
if has_id:
print("ID Card လည်း ရှိတယ်")
print("ဝင်ခွင့်ရှိတယ်")
else:
print("ID Card မရှိဘူး")
print("ဝင်ခွင့်မရှိ")
else:
print("အသက် 18 မပြည့်သေးဘူး")
print("ဝင်ခွင့်မရှိ")ဥပမာ (၂) - Grade အဆင့်ခွဲတာ:
score = 92
if score >= 90:
print("Grade: A")
# A ရဲ့ အတွင်းမှာ + နဲ့ ++ ခွဲမယ်
if score >= 95:
print("Level: A++")
else:
print("Level: A+")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C")- Indentation Rules ကိုလိုက်နာရမယ်။
- if တစ်ထပ်ထပ်တိုင်း Space ၄ ချက် ထပ်ရွှေ့ရမယ်။
- နက်တဲ့ Nested if က Code ကို ရှုပ်စေတယ်။
- ၃ ထပ်ထက် ပိုသုံးရင် elif သုံးတာ ပိုကောင်းတယ်။
Practical Examples
Example 1: Cinema Ticket System
print("=== Cinema Ticket System ===")
ticket_type = input("Ticket Type (VIP/First/Economy): ").upper()
if ticket_type == "VIP":
print("\n=== VIP Ticket ===")
print("Seat: Sofa")
print("Price: 15000 ကျပ်")
print("Perks: Free Popcorn + Drink")
elif ticket_type == "FIRST":
print("\n=== First Class Ticket ===")
print("Seat: Good Seat")
print("Price: 8000 ကျပ်")
print("Perks: Free Drink")
elif ticket_type == "ECONOMY":
print("\n=== Economy Ticket ===")
print("Seat: Normal Seat")
print("Price: 5000 ကျပ်")
print("Perks: None")
else:
print("\nInvalid Ticket Type!")
print("လက်မှတ်မရှိရင် အိမ်ပြန်")Example 2: Login System
print("=== Login System ===")
# အချက်လက် အမှန်
correct_username = "admin"
correct_password = "pass123"
max_attempts = 3
# User input
username = input("Username: ")
password = input("Password: ")
# Username စစ်မယ်
if username == correct_username:
# Username မှန်တယ်၊ Password စစ်မယ် (Nested if)
if password == correct_password:
print("\n Login Success!")
print("Welcome!")
else:
print("\n Password မှားနေပါတယ်")
print(f"သင့်မှာ {max_attempts - 1} ခါ ကျန်ပါတယ်")
else:
print("\n Username မရှိ")
print("အကောင့်သစ် ဖွင့်ပါ")Example 3: Password Strength Checker
print("=== Password Strength Checker ===")
password = input("Password ရိုက်ထည့်ပါ: ")
length = len(password)
# အရှည် စစ်မယ်
if length < 6:
print("\n Password တိုလွန်းတယ်!")
print("Strength: Very Weak")
print("အကြံပြုချက်: အနည်းဆုံး ၆ လုံး သုံးသင့်")
elif length < 8:
print("\n Password တိုသေးတယ်")
print("Strength: Weak")
print("အကြံပြုချက်: ၈ လုံး ထက် ပိုသုံးပါ")
elif length < 12:
# အရှည် အဆင်ပြေပြီ၊ နံပါတ် ပါလား စစ်မယ် (Nested if)
if any(c.isdigit() for c in password):
print("\n Password Ok")
print("Strength: Medium")
else:
print("\n Password အဆင်ပြေတယ်")
print("Strength: Medium")
print("အကြံပြုချက်: နံပါတ် ထည့်ရင် ပိုကောင်းမယ်")
else:
# အရှည် 12 ကျော်ပြီ၊ Special character ပါလား စစ်မယ်
special_chars = "!@#$%^&*"
has_special = any(c in special_chars for c in password)
if has_special:
print("\n Password ကိတ်တယ်!")
print("Strength: Very Strong")
else:
print("\n Password အကိတ်")
print("Strength: Strong")
print("အကြံပြုချက်: Special character (!@#$) ထည့်ရင် ပိုကောင်းမယ်")မှားလေ့မှားထ ဖြစ်တတ်တာ
Mistake 1: Colon (:) မထည့်တာ
# မှားတတ်တာ - Colon မရှိ
# if age > 18 # SyntaxError!
# print("OK")
# အမှန် - Colon ရှိရမယ်
if age > 18:
print("OK")Mistake 2: Indentation မမှန်တာ
# အမှား - Indentation မရှိ
# if age > 18:
# print("OK") # IndentationError!
# နည်းမှန် - Space 4 ချက် ထည့်ရမယ်
if age > 18:
print("OK")Mistake 3: = နဲ့ == ရောတာ
# မှားတဲ့ နည်း - = သုံးထားတယ် (Assignment)
# if age = 18: # SyntaxError!
# မှန်တဲ့ နည်း - == သုံးရမယ် (Comparison)
if age == 18:
print("Age is 18")Mistake 4: Space နဲ့ Tab ရောသုံးတာ
# မှားနေတာ - တစ်ကောင်က Space၊ တစ်ကောင်က Tab
# if age > 18:
# print("First") # Space 4 ချက်
# print("Second") # Tab 1 ခေါက် # TabError!
# မှန်တဲ့ နည်း - တစ်မျိုးတည်း သုံးရမယ်
if age > 18:
print("First") # Space 4 ချက်
print("Second") # Space 4 ချက်Mistake 5: elif နဲ့ else အစီအစဉ် မှားတာ
# မှား - else က elif ရှေ့မှာ
# if score >= 90:
# print("A")
# else:
# print("Other")
# elif score >= 80: # SyntaxError! elif က else ရှေ့မှာ ရှိရမယ်
# မှန်တဲ့ နည်း - if → elif → else အစီအစဉ်
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("Other")အနှစ်ချုပ်
if Statement:
- ပထမဆုံး စစ်ဆေးချက်။
- Condition True ရင် သူ့ တပည့် code ကို Run တယ်။
elif Statement:
- Option တွေ အများကြီးရှိရင် ကြားထဲမှာ သုံးတယ်။
- အပေါ်က if မှားမှ ဒါကို လာစစ်တယ်။
else Statement:
- အပေါ်ကကောင်တွေ အားလုံး မှားမှ နောက်ဆုံး အလုပ်လုပ်မယ့်ကောင်။
- Plan B အနေနဲ့ သုံးတယ်။
အရေးကြီးတဲ့ အချက်တွေ:
- : (Colon) - if, elif, else အားလုံးရဲ့ အဆုံးမှာ မမေ့နဲ့။
- Indentation - ဘယ် code က ဘယ်သူ့ တပည့်လဲဆိုတာ ခွဲပေးတာ။
- အစီအစဉ် - if → elif → else အမြဲတမ်း ဒီအစီအစဉ်။
- == - Comparison မှာ == သုံးရမယ်။ = မသုံးနဲ့။
Quick Reference
# အခြေခံ if
if condition:
# code here
# if-else
if condition:
# True ဆို ဒါ
else:
# False ဆို ဒါ
# if-elif-else
if condition1:
# condition1 True ဆို ဒါ
elif condition2:
# condition2 True ဆို ဒါ
else:
# အားလုံး False ဆို ဒါ
# Multiple conditions
if age > 18 and money > 5000:
# နှစ်ခုလုံး True
if has_ticket or has_pass:
# တစ်ခုခု True
if not is_raining:
# False ဆို True ဖြစ်သွားတယ်
# Nested if
if condition1:
if condition2:
# နှစ်ခုလုံး True
else:
# condition1 True, condition2 False