Skip to content

Perulangan While

Perulangan while juga memiliki empat variasi:

yuescript
i = 10
while i > 0
  print i
  i -= 1

while running == true do my_function!
yue
i = 10
while i > 0
  print i
  i -= 1

while running == true do my_function!
yuescript
i = 10
until i == 0
  print i
  i -= 1

until running == false do my_function!
yue
i = 10
until i == 0
  print i
  i -= 1
until running == false do my_function!

Seperti loop for, loop while juga bisa digunakan sebagai ekspresi. Ekspresi while dan until mendukung break dengan banyak nilai.

yuescript
value, doubled = while true
  n = get_next!
  break n, n * 2 if n > 10
yue
value, doubled = while true
  n = get_next!
  break n, n * 2 if n > 10

Selain itu, agar sebuah fungsi mengembalikan nilai akumulasi dari loop while, pernyataannya harus di-return secara eksplisit.

Repeat Loop

Loop repeat berasal dari Lua:

yuescript
i = 10
repeat
  print i
  i -= 1
until i == 0
yue
i = 10
repeat
  print i
  i -= 1
until i == 0

Ekspresi repeat juga mendukung break dengan banyak nilai:

yuescript
i = 1
value, scaled = repeat
  break i, i * 100 if i > 3
  i += 1
until false
yue
i = 1
value, scaled = repeat
  break i, i * 100 if i > 3
  i += 1
until false