5. Control flow
if
if guess < secret
show_msg("Too low")
elif guess > secret
show_msg("Too high")
else
show_msg("Correct!")
end
- No
:afterif,eliforelse(D9), oneendfor the wholeif(D20). elifis short for "else if" (D10, D40). Unio writeselse if, and Wax'selifis a deliberate difference.- A condition must be a
Bool:if livesis refused, withif lives not_eq 0as the fix (D16, Unio D187).
Comparisons and logic
eq, not_eq, <, >, at_least and at_most compare, and and, or and not combine (D22, D158, Unio D17,
D34, D190). Comparisons on Floats are exact (Unio D184).
Unio's standard, chapter 5, has the rules in full.
while and break
mut tries = 0
while tries < 7 and not won
tries = tries + 1
end
while runs its body as long as its condition holds, and break leaves the loop (D22, Unio D19, D32). eq and
not_eq between two Floats are refused in a while condition (Unio D184).
for_each
for_each is one keyword, its two words joined as one_of's and at_least's are (D146).
for_each name at pos i in names
show_msg("{i}. {name}")
end
As in Unio (D22, Unio D171 to D174):
- It goes through a
List, anArrayor arange[…]kept in a variable, in order, or aDict's values, withat key namegiving each key (D33);at posandat offseton aDictare refused, withat keyas the fix (D105). - The item's name is new for each pass, made by the
for_eachline, and ends with the loop. at pos iorat offset igives the position too, counting from 1 or from 0.- What it goes through can't change inside it, nor through a call that can change it, and a loop going through a top-level name can't pause; copying it into a name of its own first is the fix (D69).
breakleaves it.
range is in chapter 8. Unio's standard, chapter 5, has the rules in full.
for_each goes through a text by graphemes, what a person sees as one character (D65, D66).
for
for counts, with range's words, from, to or before, and step_by, written without brackets or colons
(D172):
for round from 1 to 3
show_msg("Round {round}!")
end
for i from 0 before size
show_msg("{i}")
end
for n from 10 to 0 step_by -2
show_msg("{n}")
end
toincludes the last number,beforestops short of it, andstep_byis optional, counting by 1, negative to count down, asrange's labels are (Unio D170, D172). A step of 0, or one pointing away from the end, is refused where Wax can see it and stops with a failed check otherwise, as forrange.- The counter is a new immutable variable for each pass, ending with the loop;
breakleaves it. for_eachgoes through items;forcounts. Arange[…]written straight into afor_each, asfor_each i in range[from: 1, to: 3], is refused, withfor i from 1 to 3as the fix; a range kept in a variable is a list, gone through withfor_each(D99).forwithin, as Python writesfor x in xs, is refused, withfor_each x in xsas the fix.
Not decided: leaving an outer loop; other loops.