while and each are control flow blocks that make it possible to repetitively loop through execution of the same code.
while offers a simple mechanism: it executes a block's statements over and over as long as its condition remains true. The condition is checked right away and every time after the block's statements have been performed.
For example, this code logs the same message 5 times:
mut x = 1 while x<=5 $.log "DANGER, DANGER!! Will Robinson" x+=1
Like if, while may be used as a control suffix following a statement:
$$.show(list.extract) while !list.isEmpty
is equivalent to:
while !list.isEmpty $$.show(list.extract)
break and continue
Within a while block, break or continue statements may be used to modify the loop's control flow behavior.
The break statement, placed anywhere within a while block, provides a way to stop performance of the 'while' block. break is typically accompanied with if:
while true // without the break, this loop would run forever actor.walkstep // will happen at least once break if keypress==0x20 // stops whenever the space bar is pressed
By contrast, the continue statement ignores all subsequent statements in the 'while' block, returning control back to the top of the while block where the conditional expression is re-evaluated.