စံကား

06Control flow

Break and continue

Leave a loop at once, or skip the rest of one round.

Two statements change how a loop runs from inside the loop itself:

ZakaReads asDoes
ကွင်းဆက် ကို ရပ်ပါ။"please stop the loop"leaves the loop at once (break)
နောက်တစ်ကြိမ်ဆီသို့ ကျော်ပါ။"please skip to the next round"skips the rest of this round (continue)
စံကား

Expected output

၁
၂
၄
၅
၇
၈
၁၀

Searching with break

A loop that runs "forever" with မှန် (true) as its condition, plus a break when the answer is found, is a common search pattern:

စံကား

Expected output

၁၂၈

Continue in a generic loop

In a generic loop, continue still runs the ပြီးလျှင် update, so the counter keeps moving:

စံကား

Expected output

၀
၂
၄
၆
၈

Rules

  • Both statements act on the innermost loop around them.
  • Both must be inside a loop:
စံကားZK0309

Expected error

error[ZK0309]: "ကွင်းဆက် ကို ရပ်ပါ" stands outside a loop

Hands on

  • Print the first number above 1 that divides 91 with no remainder.
  • In the times table from Counted loops, add a break to the inner loop. Which loop does it stop?

Next: Functions.