control flow else elsif
Documentation for control flow
else elsif assembled from the following types:
A compound conditional may be produced by following an
if conditional with
else to provide an alternative block to run when the conditional expression is false:
if 0 else ; # says "yes"if 0 else ; # says "yes", space is not required
else cannot be separated from the conditional statement by a semicolon, but as a special case, it is OK to have a newline.
if 0 ; else ; # syntax error
if 0else ; # says "yes"
Additional conditions may be sandwiched between the
if and the
elsif. An extra condition will only be evaluated if all the conditions before it were false, and only the block next to the first true condition will be run. You can end with an
elsif instead of an
else if you want.
if 0 elsif False else # says "yes"if 0 elsif True else # says "YES"if 0 elsif False # does not say anythingsub rightsub wrongif wrong() elsif right() else# The above says "Wrong!" then says "Right!" then says "yes"
You cannot use the statement modifier form with
42.say if 0 else # syntax error
All the same rules for semicolons and newlines apply, consistently
if 0 ; elsif 1 else ; # syntax errorif 0 elsif 1 ; else ; # syntax error
if 0 elsif 1 else ; # says "1"
if 0 elsif 1else ; # says "1"if 0elsif 1 else ; # says "1"if 0elsif Falseelse ; # says "yes"
The whole thing either slips us an empty list (if no blocks were run) or returns the value produced by the block that did run:
my = 0; say (1,(if 0 elsif False ),3, ); # says "(1 3 0)"my = 0; say (1,(if 0 else ),3, ); # says "(1 2 3 43)"
It's possible to obtain the value of the previous expression inside an
else, which could be from
if or the last
elsif if any are present:
= 1; if 0 else -> ; # says "1 0"= 1; if False else -> ; # says "1 False"if False elsif 0 else -> ; # says "0"