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

Loops (For, While)

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

Loop ဆိုတာ တူညီတဲ့အလုပ်ကို ထပ်ခါထပ်ခါလုပ်စေချင်တဲ့အခါ သုံးတဲ့ control structure ပါ။ Kotlin မှာ for loop ကို range သို့မဟုတ် collection တွေကိုသွားဖို့သုံးပြီး while loop ကို condition မှန်နေသရွေ့ run ဖို့သုံးပါတယ်။

kotlin
fun main() {
    for (number in 1..4) {
        println("Number: $number")
    }

    var countdown = 3
    while (countdown > 0) {
        println("Countdown: $countdown")
        countdown--
    }
}
You should see
Number: 1 Number: 2 Number: 3 Number: 4 Countdown: 3 Countdown: 2 Countdown: 1

အနှစ်ချုပ်

ထပ်လုပ်ရမယ့်အလုပ်ရှိရင် loop သုံးပါ။ Range/collection အတွက် for, condition အတွက် while ကိုရွေးပါ။

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

  • while ထဲမှာ counter ကိုမပြောင်းမိရင် program မပြီးဘဲ ဆက်ပြေးနေတတ်ပါတယ်။

Practical example — total price

Practical example — total price

kotlin
fun main() {
    val prices = listOf(1000, 2500, 1500)
    var total = 0

    for (price in prices) {
        total += price
    }

    println("Total: $total MMK")
}

You'll know it worked when: Total: 5000 MMK

Loops (For, While) | Thuta Learning