Thuta Learning
Ruby
IntermediateProgrammingbeginner

Loops (While, Until)

Loop ဆိုတာ code တစ်ပိုင်းကို ထပ်ခါထပ်ခါ run စေတဲ့နည်းလမ်းပါ။ Ruby မှာ while, until ရှိပေမယ့် collection တွေအတွက် each ကို ပိုသုံးလေ့ရှိပါတယ်။

ruby
count = 1

while count <= 3
  puts "While count: #{count}"
  count += 1
end

counter = 1
until counter > 3
  puts "Until counter: #{counter}"
  counter += 1
end

while က condition မှန်နေသရွေ့ run ပါတယ်။ until က condition မမှန်သရွေ့ run ပါတယ်။ Loop တိုင်းမှာ counter ကို update မလုပ်ရင် infinite loop ဖြစ်နိုင်ပါတယ်။

You should see
While count: 1 While count: 2 While count: 3 Until counter: 1 Until counter: 2 Until counter: 3

Info

count += 1 သည် count = count + 1 နဲ့တူပါတယ်။

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

  • Counter update မလုပ်မိရင် loop က မဆုံးတော့ပါ။ Terminal မှာ stuck ဖြစ်ရင် Ctrl + C နဲ့ရပ်နိုင်ပါတယ်။
Loops (While, Until) | Thuta Learning