Conditionals Cheat Sheet¶
if
¶
if
is pretty straightforward:
if (test-expression)
// if test-expression is true
elseif (other-test)
// otherwise, if other-test is true
else
// If no tests above were true
end;
case
¶
case
tests a series of conditions and executes the body
of code corresponding to the first condition that is true.
case
player1.money <= 0
=> end-game(player1);
player2.money <= 0
=> end-game(player2);
otherwise
=> move(player1);
move(player2);
end case;
select
¶
select
takes a target object and compares it against
a series of matches and executes the body of code corresponding
to the first match found.
select (as-uppercase(char))
'D' => collect(number-to-string(argument(char, <number>)));
'B' => collect(integer-to-string(argument(char, <integer>), base: 2));
'O' => collect(integer-to-string(argument(char, <integer>), base: 8));
'X' => collect(integer-to-string(argument(char, <integer>), base: 16));
'C' => collect-character(argument(char, <character>));
'S' => print-pretty-name(buffer, argument(char, <object>));
'=' => print-unique-name(buffer, argument(char, <object>));
'%' => collect-character('%');
otherwise =>
error("Invalid format directive '%s' in \"%s\"",
char, format-string);
end;
unless
¶
unless
executes a body of code when the test expression
is false:
unless (test-expression)
// do work, if test-expression is false
end;
when
¶
This is just a shorthand for if
when there’s no elseif
or
else
branches.
when (test-expression)
// if test-expression is true
end;
when
isn’t defined in the DRM. To use it, you must use
the dylan-extensions
module from the dylan
library.