Thuta Learning
ရှာဖွေရန်
BasicProgrammingbeginner

Python Loops: for, while, break and continue

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Iterable ကို for ဖြင့်လည်ပတ်ရန်
  • while condition ရေးရန်
  • break နှင့် continue ကိုခွဲခြားရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Python for loop သည် sequence သို့မဟုတ် iterable ၏ item များကိုအစဉ်လိုက်လည်ပတ်သည်။ အကြိမ်ရေသတ်မှတ်လိုပါက range() သုံးပြီး condition မှန်နေသရွေ့လုပ်လိုပါက while သုံးသည်။ break က loop ကိုရပ်ပြီး continue က လက်ရှိအကြိမ်ကိုကျော်သွားသည်။

python
scores = [68, 42, 91, 77]

for index, score in enumerate(scores, start=1):
    if score < 50:
        continue
    print(f"{index}: {score}")

attempts = 3
while attempts > 0:
    print(f"Attempts left: {attempts}")
    attempts -= 1
You should see
1: 68
3: 91
4: 77
Attempts left: 3
Attempts left: 2
Attempts left: 1

လက်တွေ့စမ်းကြည့်ရန်

1 မှ 30 အတွင်း 3 ဖြင့်စားပြတ်သော် Fizz၊ 5 ဖြင့်စားပြတ်သော် Buzz ထုတ်သော loop ရေးပါ။

Python Control FlowPython Software Foundation

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • while loop condition မပြောင်းသဖြင့် infinite loop ဖြစ်ခြင်း
  • List index လိုသည့်အခါ range(len(...)) ကိုမလိုအပ်ဘဲသုံးခြင်း

လေ့ကျင့်ခန်း

1 မှ 30 အတွင်း 3 ဖြင့်စားပြတ်သော် Fizz၊ 5 ဖြင့်စားပြတ်သော် Buzz ထုတ်သော loop ရေးပါ။

You'll know it worked when: 1: 68 3: 91 4: 77 Attempts left: 3 Attempts left: 2 Attempts left: 1

Python Loops: for, while, break and continue | Thuta Learning