// --- while do: repeat while a condition is true ---

def i as integer(1)
def total as integer(0)

while i <= 5 do (
  total += i
  i += 1
)

assert(total == 15)
print("sum 1..5 = {{total}}")

// --- break(): exit the loop early ---

def n as integer(0)
def found as boolean(false)

while n < 100 do (
  n += 1
  if n == 7 (
    found = true
    break()
  )
)

assert(found)
assert(n == 7)
print("found 7 after {{n}} iterations")

// --- next(): skip the rest of the current iteration ---

def evens as array
def k as integer(0)

while k < 10 do (
  k += 1
  if k % 2 != 0 (
    next()
  )
  evens.push(k)
)

assert(evens.len() == 5)
print("even numbers: {{evens}}")

// --- for loop: iterate over a collection ---

def items as array(10, 20, 30, 40, 50)
def sum as integer(0)

for item in items (
  sum += item
)

assert(sum == 150)
print("array sum = {{sum}}")

// --- break() inside for ---

def first_over_25 as integer(0)

for item in items (
  if item > 25 (
    first_over_25 = item
    break()
  )
)

assert(first_over_25 == 30)
print("first item over 25 = {{first_over_25}}")
