nenu-lang / loops
Stable

Loops

  • Use loop collection as value for one binding or loop collection as key, value for two.
  • continue skips the rest of the current iteration; break exits the nearest loop.
  • Do not structurally mutate the collection being iterated.

Looping An Array

code
1number total = 023loop expenses as expense {4  total = total + expense.amount5}

The one-binding form receives each element. The two-binding form receives the numeric index first and the element second. Both bindings are local to the loop body.

Looping A Map

code
1number room_total = 023loop balances.entries() as member_id, amount {4  room_total = room_total + amount5}

For maps, the first binding is the string key and the second is the typed value. entries() keeps the same map value while making the key-and-value intent explicit.

Continue And Break

code
1number open_total = 023loop expenses as expense {4  if expense.status == ExpenseStatus.Settled {5    continue6  }78  if open_total >= 500 {9    break10  }1112  open_total = open_total + expense.amount13}
  • continue skips statements below it and begins the next iteration.
  • break exits the nearest enclosing loop.
  • Both keywords are only valid inside a loop.

Choose A Loop Or Collection Method

GoalPrefer
Produce a filtered arrayfilter
Project every item to another valuecollect
Check any or all itemsany or all
Accumulate a total or perform several statements per itemloop
Need early exitloop with break
Need to skip selected items while doing other workloop with continue

Mutation Safety

code
1Expense[] open_expenses = []23loop expenses as expense {4  if expense.status != ExpenseStatus.Settled {5    open_expenses.push(expense)6  }7}

Common Mistakes

WrongCorrect
for expense in expensesloop expenses as expense
foreach expensesloop expenses as expense
while conditionUse a collection loop or restructure the workflow.
break outside a loopReturn, throw, or use a conditional as appropriate.

Continue Learning