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

Loops

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

Loop သည် code တစ်ပိုင်းကို ထပ်ခါထပ်ခါ run လုပ်ရန်သုံးပါတယ်။ List ထဲက data တွေပြရန်၊ total တွက်ရန်၊ search/filter လုပ်ရန် အရေးကြီးပါတယ်။

dart
void main() {
  List<int> prices = [3000, 5000, 7000];
  int total = 0;

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

  print('Total: $total');

  int count = 1;
  while (count <= 3) {
    print('Attempt $count');
    count++;
  }
}

for-in loop က List ထဲက price တစ်ခုချင်းစီကိုယူပြီး total ထဲပေါင်းထည့်ပါတယ်။ while loop က condition မှန်နေသရွေ့ run လုပ်ပြီး count++ က count ကို 1 တိုးပါတယ်။

You should see
Total: 15000 Attempt 1 Attempt 2 Attempt 3

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

  • while loop မှာ count တိုးဖို့မေ့ရင် infinite loop ဖြစ်နိုင်ပါတယ်။ Loop က computer ကို treadmill ပေါ်တင်ပြီး off button မပေးသလိုဖြစ်သွားမယ်—မလုပ်ပါနဲ့။
Loops | Thuta Learning