for and while Loops
Loop ဆိုတာ ဘာလဲ?
Loop ဆိုတာ Code တွေကို ထပ်ခါတလဲလဲ အလိုအလျောက် Run စေတဲ့ နည်းစနစ်ပေါ့။ Programming မှာ အရမ်းအသုံးဝင်တာ။
Python မှာ Loop နှစ်မျိုး ရှိတယ်:
- for Loop - Sequence (အစီအစဉ်) ကုန်တဲ့အထိ ပတ်တယ်။
- while Loop - Condition (အခြေနေ) မှန်နေသရွေ့ ပတ်တယ်။
ဒီ Chapter မှာ for နဲ့ while Loop နှစ်ခုလုံးကို အသေးစိတ် လေ့လာရမယ်။
for Loop (The Sequence Loop)
for loop က ပိုမိုက်တယ်။ သူက Infinite Loop (အဆုံးမရှိ loop) မဖြစ်ဘူး။
for loop က while လို Condition ကို စစ်နေတာ မဟုတ်ဘူး။ သူက Sequence (အစီအစဉ်) တစ်ခု ကုန်တဲ့အထိ တစ်ခုချင်း (item by item) ပတ်တာ။
Sequence ဆိုတာ ဘာလဲ?
- String (စာလုံး) - Python ဆို P, y, t, h, o, n
- List (စာရင်း) - [Apple, Banana, Cherry]
- range() - ဂဏန်းတွေ ဥပမာ 0, 1, 2, 3, 4
range() နဲ့ က အသုံးအများဆုံးပဲ။
for variable in sequence:
Indentation (Space ၄ ချက်)။
for Loop with range()
range() ဆိုတာ ဂဏန်းတွေ ထုတ်ပေးတဲ့ function။ for loop နဲ့ အတူတွဲသုံးတာများတယ်။
1. range(stop):
range(5) ဆိုတာ 0 က စ ပြီး 5 မရောက်ခင် (5 မပါဘူး) ဂဏန်းတွေ ထုတ်ပေးတယ်။ (0, 1, 2, 3, 4)
# num နေရာမှာ ဘယ်နာမည်ပေးပေးရတယ် (i, x, number ကြိုက်တာပေး)
# range(5) (0,1,2,3,4) ထဲက ဂဏန်း တစ်ခုချင်း ကို num ထဲ ထည့်ပြီး ပတ်မယ်
for num in range(5):
print("Looping: " + str(num))
print("Loop ပြီးပြီ။")Output:
Looping: 0
Looping: 1
Looping: 2
Looping: 3
Looping: 4
Loop ပြီးပြီ။while loop ထက် အများကြီး ရှင်းတယ်။ Update (+= 1) ရေးစရာ မလိုဘူး။ range() က Auto တိုးပေးသွားတယ်။
2. range(start, stop) - စ နဲ့ ဆုံး သတ်မှတ်တာ:
range(1, 6) ဆိုရင် 1 က စ ပြီး 6 မရောက်ခင် ရပ်တယ်။ (1, 2, 3, 4, 5)
# 1 ကနေ 5 ထိ ရေမယ်
for num in range(1, 6):
print(f"Number: {num}")Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 53. range(start, stop, step) - ခုန်ပြီး ရေတာ:
range(0, 10, 2) ဆိုရင် 0 က စ ပြီး 10 မရောက်ခင် ရပ်မယ်၊ ဒါပေမယ့် တစ်ကြိမ်မှာ 2 ခါခုန်ပြီး ရေတွက်မယ်။ (0, 2, 4, 6, 8)
# 2 ခါခုန်ပြီး ရေမယ် (စုံဂဏန်းတွေ)
for num in range(0, 10, 2):
print(f"Even number: {num}")Output:
Even number: 0
Even number: 2
Even number: 4
Even number: 6
Even number: 84. range() နဲ့ နောက်ပြန်ရေတာ (Counting DOWN):
step ကို အနုတ်ကိန်း သုံးရင် နောက်ပြန်ရေလို့ရတယ်။
# 5 ကနေ 1 ထိ နောက်ပြန်ရေမယ်
for num in range(5, 0, -1):
print(f"Countdown: {num}")
print("Blastoff!")Output:
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Blastoff!- range(5) ဆိုရင် 0, 1, 2, 3, 4 (5 မပါဘူး)
- range(1, 6) ဆိုရင် 1, 2, 3, 4, 5 (6 မပါဘူး)
- stop ကိန်း က အမြဲတမ်း မပါဝင်ဘူး
for Loop with String
String ထဲက စာလုံးတွေကို တစ်လုံးချင်း ပတ်လို့ရတယ်။
name = "PYTHON"
# name ထဲက စာလုံး (char) တစ်ခုချင်း ကို char ထဲ ထည့်ပြီး ပတ်မယ်
for char in name:
print(char)Output:
P
Y
T
H
O
Nfor Loop - Practical Example
Example: Shopping List Iteration
ဈေးဝယ်စာရင်း ထဲက ပစ္စည်းတွေကို တစ်ခုချင်း print ထုတ်မယ်။
print("=== ဈေးဝယ်စာရင်း ===")
shopping_list = ["ထမင်း", "ငါး", "ဟင်းသီးဟင်းရွက်", "သစ်သီး", "နို့"]
# စာရင်းထဲက item တွေကို တစ်ခုချင်း ပတ်မယ်
for item in shopping_list:
print(f"- {item}")
print(f"\nစုစုပေါင်း: {len(shopping_list)} မျိုး")Output:
=== ဈေးဝယ်စာရင်း ===
- ထမင်း
- ငါး
- ဟင်းသီးဟင်းရွက်
- သစ်သီး
- နို့
စုစုပေါင်း: 5 မျိုးExample: Multiplication Table
မြှောက်ဇယားကို for loop နဲ့ ထုတ်မယ်။
number = 5
print(f"=== {number} ရဲ့ မြှောက်ဇယား ===")
for i in range(1, 11):
result = number * i
print(f"{number} × {i} = {result}")Output:
=== 5 ရဲ့ မြှောက်ဇယား ===
5 × 1 = 5
5 × 2 = 10
5 × 3 = 15
5 × 4 = 20
5 × 5 = 25
5 × 6 = 30
5 × 7 = 35
5 × 8 = 40
5 × 9 = 45
5 × 10 = 50while Loop (The Condition Loop)
while loop က bro ခုနက သင်ခဲ့တဲ့ if နဲ့ ခပ်ဆင်ဆင် တူတယ်။
- if က: Condition ကို တစ်ခါ စစ်တယ်။ True (မှန်) ရင် တစ်ခါ run တယ်။ ပြီးပြီ။
- while က: Condition ကို စစ်တယ်။ True (မှန်) ရင် run တယ်။ ပြီးရင် အပြင် ပြန်မထွက်ဘူး။ အပေါ် (while) ဆီ ပြန်တက်တယ်။ ထပ်စစ်တယ်။ True (မှန်) နေသရွေ့ ထပ်ခါထပ်ခါ run နေမှာ။ Condition False (မှား) သွားမှ ရပ် (Stop) တာ။
while [Condition]:
Indentation (Space ၄ ချက်) က အသက်။
ဒီ Condition က True ဖြစ်နေသရွေ့ ဒီ အတွင်းက Code တွေ ပတ်နေမယ်။
while Loop - Basic Example
ဥပမာ - 1 ကနေ 5 ထိ ရေမယ်:
count = 1 # 1. စမယ့်ကောင် (Initializer)
while count <= 5: # 2. Condition (count က 5 ထက် ငယ်/ညီ နေသ၍)
print("Number: " + str(count))
count += 1 # 3. Update (count ကို 1 တိုး)
print("Loop ပြီးပြီ။")Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Loop ပြီးပြီ။ရှင်းလင်းချက်:
count က 1။
Loop 1: 1 <= 5 True လား? True (မှန်) တယ်။ Number: 1 ကို print။ count ကို 1 တိုး (count က 2 ဖြစ်သွား)။ အပေါ်ပြန်တက်။
Loop 2: 2 <= 5 True လား? True။ Number: 2 ကို print။ count ကို 1 တိုး (count က 3 ဖြစ်သွား)။ အပေါ်ပြန်တက်။
Loop 3: 3 <= 5 True လား? True။ print။ count က 4 ဖြစ်။ အပေါ်ပြန်တက်။
Loop 4: 4 <= 5 True လား? True။ print။ count က 5 ဖြစ်။ အပေါ်ပြန်တက်။
Loop 5: 5 <= 5 True လား? True (ညီတယ်လေ)။ Number: 5 ကို print။ count ကို 1 တိုး (count က 6 ဖြစ်သွား)။ အပေါ်ပြန်တက်။
Loop 6: 6 <= 5 True လား? False (မှားပြီ)။
Condition False သွားပြီမို့ while loop အတွင်း လုံးဝ မဝင်တော့ဘူး။ Loop ပြီးသွားပြီ။ အပြင်ကို ခုန်ထွက်ပြီး print(Loop ပြီးပြီ။) ကို Run တယ်။
အန္တရာယ်: Infinite Loop
while loop မှာ အသက် က count += 1 ဆိုတဲ့ Update ပဲ။ bro ဒါကို မေ့ခဲ့ရင်၊ count က 1 ကနေ ဘယ်တော့မှ တက်မလာတော့ဘူး။ 1 <= 5 က အမြဲတမ်း True (မှန်) နေမယ်။
bro Program က Number: 1 ကို ထာဝရ print နေပြီး ရပ်တော့မှာ မဟုတ်ဘူး။ stuck သွားမယ်။ ဒါ သတိထား။
Infinite Loop ဥပမာ:
# ဒီ code က ဘယ်တော့မှ မရပ်ဘူး!
count = 1
while count <= 5:
print("Number: " + str(count))
# count += 1 ကို မေ့သွားတယ်!
# count က 1 အတိုင်း နေမယ်
# 1 <= 5 က အမြဲတမ်း True
# Loop က ထာဝရ ပတ်နေမယ်!- Windows/Linux: Ctrl + C နှိပ်
- Mac: Cmd + C နှိပ်
- တစ်နည်းနည်း နဲ့ Program ကိုရပ်ပစ်ဖို့ပဲ
အမှန်:
count = 1
while count <= 5:
print("Number: " + str(count))
count += 1 # Update ကို မမေ့နဲ့!
print("Loop ပြီးပြီ။")while Loop - Practical Example
Example: Password Retry System
Password မှားရင် ထပ်ရိုက်ခွင့်ပေးမယ့် System။ ဘယ်နှစ်ခါ မှားမလဲ မသိဘူး၊ ဒါကြောင့် while loop သုံးတယ်။
print("=== Password Retry System ===")
correct_password = "python123"
max_attempts = 3
attempts = 0
while attempts < max_attempts:
password = input(f"Password ထည့်ပါ ({attempts + 1}/{max_attempts}): ")
if password == correct_password:
print("\n Login Success!")
print("Welcome!")
break # Password မှန်ရင် Loop ကနေ ထွက်မယ်
else:
attempts += 1 # မှားတဲ့ အကြိမ် တိုးမယ်
remaining = max_attempts - attempts
if remaining > 0:
print(f" Password မှားနေတယ်။ {remaining} ခါ ကျန်ပါတယ်။\n")
else:
print("\n ခွင့်ပြုချက် ကုန်သွားပြီ။")
print("Account လော့ကျပြီ။")Sample Run:
=== Password Retry System ===
Password ထည့်ပါ (1/3): wrong123
Password မှားနေတယ်။ 2 ခါ ကျန်ပါတယ်။
Password ထည့်ပါ (2/3): wrong456
Password မှားနေတယ်။ 1 ခါ ကျန်ပါတယ်။
Password ထည့်ပါ (3/3): python123
Login Success!
Welcome!break Statement
break က Loop ကနေ ချက်ချင်း ထွက်ချင်တဲ့အခါ သုံးတယ်။
break with for Loop:
# ဂဏန်း 7 တွေ့ရင် ရပ်မယ်
for num in range(1, 11):
if num == 7:
print("7 တွေ့ပြီ! ရပ်မယ်။")
break
print(f"Number: {num}")
print("Loop အပြင်မှာ")Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
7 တွေ့ပြီ! ရပ်မယ်။
Loop အပြင်မှာbreak with while Loop:
# User က quit လို့ ရိုက်မှ ရပ်မယ်
print("Game started! ('quit' လို့ရိုက်ရင် ထွက်မယ်)")
while True: # Infinite loop ဖန်တီးထား (break နဲ့ပဲ ထွက်မယ်)
command = input("Command: ")
if command == "quit":
print("Game ပိတ်လိုက်ပြီ။")
break
print(f"bro ရိုက်တာ: {command}")Practical Example - ရှာဖွေတာ:
fruits = ["သစ်တော်သီး", "ငှက်ပျောသီး", "စတော်ဘယ်ရီ", "သခွားသီး", "သရက်သီး"]
search_item = "စတော်ဘယ်ရီ"
print(f"'{search_item}' ကို ရှာနေတယ်...")
for fruit in fruits:
print(f"စစ်နေတယ်: {fruit}")
if fruit == search_item:
print(f" တွေ့ပြီ: {fruit}")
break # တွေ့ပြီဆို ရပ်မယ်
else:
# Loop က break မဖြစ်ဘဲ ပုံမှန် ပြီးရင် ဒီ else run မယ်
print(f" '{search_item}' မတွေ့ဘူး")Output:
'စတော်ဘယ်ရီ' ကို ရှာနေတယ်...
စစ်နေတယ်: သစ်တော်သီး
စစ်နေတယ်: ငှက်ပျောသီး
စစ်နေတယ်: စတော်ဘယ်ရီ
တွေ့ပြီ: စတော်ဘယ်ရီcontinue Statement
continue က လက်ရှိ iteration ကို ကျော်ပြီး နောက် iteration ကို ဆက်လုပ်တာ။ Loop ကနေ ထွက်တာ မဟုတ်ဘူး။
continue with for Loop:
# မဂဏန်းတွေကိုပဲ print မယ် (စုံဂဏန်း ကျော်မယ်)
for num in range(1, 11):
if num % 2 == 0: # စုံဂဏန်း စစ်တယ်
continue # စုံဂဏန်း ဆိုရင် ကျော်မယ်
print(f"Odd number: {num}")Output:
Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9continue with while Loop:
# 1 ကနေ 10 ထိ၊ 5 ကို ကျော်ပြီး print မယ်
count = 0
while count < 10:
count += 1 # Update လုပ်ရမယ်!
if count == 5:
continue # 5 ဆိုရင် print မလုပ်ဘဲ နောက်ပတ်ကို သွားမယ်
print(f"Number: {count}")Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 6
Number: 7
Number: 8
Number: 9
Number: 10Practical Example - Input Validation:
print("=== ဂဏန်း ထည့်ပါ (အနုတ်ကိန်း မထည့်နဲ့) ===")
numbers = []
count = 0
while count < 5:
num = int(input(f"ဂဏန်း {count + 1}: "))
if num < 0:
print("အနုတ်ကိန်း မထည့်နဲ့! ထပ်ထည့်ပါ။")
continue # အနုတ်ကိန်း ဆိုရင် ထပ်မေးမယ် (count မတိုးဘူး)
numbers.append(num)
count += 1
print(f"\nထည့်ထားတဲ့ ဂဏန်းတွေ: {numbers}")
print(f"ပေါင်းလဒ်: {sum(numbers)}")while vs for: ဘယ်ကောင် ဘယ်မှာ သုံးရမလဲ?
while Loop (The Condition Loop)
ဘယ်မှာသုံးလဲ?
- ဘယ်နှစ်ခါ ပတ်ရမလဲ အတိအကျ မသိတဲ့အခါ။
- Condition တစ်ခု True ဖြစ်နေသရွေ့ ပတ်ချင်တဲ့အခါ။
ဥပမာ:
- User က quit လို့ မရိုက်မချင်း game ကို ဆက် run ချင်တယ်။ (သူ ဘယ်တော့ ရိုက်မလဲ မသိနိုင်ဘူး)
- Network မကျနေသရွေ့ data ဆက် download လုပ်နေ။
- Password မမှန်သေးသရွေ့ ထပ်မေးနေ။
အန္တရာယ်: Infinite Loop (update မေ့ရင် သွားပြီ)။
for Loop (The Sequence Loop)
ဘယ်မှာသုံးလဲ?
- ဘယ်နှစ်ခါ ပတ်ရမလဲ အတိအကျ သိတဲ့အခါ။
- Sequence (စာရင်း၊ String၊ range) ကုန်အောင် ပတ်ချင်တဲ့အခါ။
ဥပမာ:
- range(10) (၁၀ ခါ အတိအကျ ပတ်)
- List ထဲက Item အားလုံး (၅ ခုရှိရင် ၅ ခါ အတိအကျ ပတ်)
- String ထဲက စာလုံး အားလုံး (ကုန်တဲ့အထိ အတိအကျ ပတ်)
အန္တရာယ်: မရှိသလောက်ပဲ။ Auto ရပ်တယ်။
- ဘယ်နှစ်ခါ ပတ်မလဲ သိရင် → for Loop သုံး
- ဘယ်နှစ်ခါ ပတ်မလဲ မသိရင် → while Loop သုံး
- Sequence ကို လိုက်ချင်ရင် → for Loop သုံး
- Condition အပေါ် မူတည်ပြီး ပတ်ချင်ရင် → while Loop သုံး
အနှစ်ချုပ်
Loop ဆိုတာ:
- Code တွေကို ထပ်ခါတလဲလဲ Run စေတာ။
- Manual လုပ်စရာ မလို။ Automatic ပတ်တယ်။
while Loop:
- Condition မှန်နေသရွေ့ ပတ်တယ်။
- ဘယ်လောက်ကြာမလဲ မသိ။
- Update ကို မမေ့နဲ့ (Infinite Loop ဖြစ်မယ်)။
for Loop:
- Sequence ကုန်တဲ့အထိ ပတ်တယ်။
- ဘယ်လောက်ကြာမလဲ သိတယ်။
- range() နဲ့ အရမ်း အတွဲညီတယ်။
- Infinite Loop မဖြစ်နိုင်ဘူး။
break Statement:
- Loop ကနေ ချက်ချင်း ထွက်တာ။
- Loop အပြင်ကို ခုန်သွားတယ်။
continue Statement:
- လက်ရှိ iteration ကို ကျော်တာ။
- နောက် iteration ကို ဆက်လုပ်တယ်။
- Loop ကနေ မထွက်ဘူး။
Quick Reference
# for Loop with range()
for i in range(5):
print(i) # 0, 1, 2, 3, 4
# for Loop with range(start, stop)
for i in range(1, 6):
print(i) # 1, 2, 3, 4, 5
# for Loop with range(start, stop, step)
for i in range(0, 10, 2):
print(i) # 0, 2, 4, 6, 8
# for Loop with String
for char in "Python":
print(char)
# for Loop with List
for item in ["a", "b", "c"]:
print(item)
# while Loop
count = 0
while count < 5:
print(count)
count += 1 # Update မမေ့နဲ့!
# break - Loop ကနေ ထွက်တာ
for i in range(10):
if i == 5:
break
print(i)
# continue - iteration ကျော်တာ
for i in range(5):
if i == 2:
continue
print(i) # 0, 1, 3, 4 (2 ကို ကျော်သွားတယ်)