Programmer Guide/Command Reference/COND: Difference between revisions
From STX Wiki
Jump to navigationJump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
{{DISPLAYTITLE:{{SUBPAGENAME}}}} | {{DISPLAYTITLE:{{SUBPAGENAME}}}}__NOTOC__ | ||
The <code>COND</code> command performs a conditional assignment. | The <code>COND</code> command performs a conditional assignment. | ||
==Usage== | ==Usage== | ||
Line 12: | Line 12: | ||
==See also== | ==See also== | ||
conditonal assignment with [[../EVAL|EVAL]], [[Programmer_Guide/Introduction#Control_Commands|control commands]], [[../IF|IF]] | conditonal assignment with [[../EVAL|EVAL]], [[Programmer_Guide/Introduction#Control_Commands|control commands]], [[../IF|IF]] | ||
==Examples== | |||
#min := cond $#a < $#b ? $#a : $#b // calculate minimum of #a and #b | #min := cond $#a < $#b ? $#a : $#b // calculate minimum of #a and #b | ||
#abs := cond $#a < 0 ? num -$#a : $#a // calculate absolute value of #a | #abs := cond $#a < 0 ? num -$#a : $#a // calculate absolute value of #a |
Revision as of 14:45, 27 April 2011
The COND
command performs a conditional assignment.
Usage
[ target := ] COND condition ? expr1 : expr2
- target
- is a normal assignment target, usually the name of an STx variable, e.g.
#var
. If no target is specified, a conditional execution is performed. - condition
- is a conditional expression like used with the
IF
statement and the miscellaneous conditional control commands.- Note that with conditional STx expressions there must always be intervening whitespaces between operators and their arguments (unless the argument is quoted). So, both
$#a == 7
and'$#a'=='7'
are valid expression, whereas$#a==7
is not.
- Note that with conditional STx expressions there must always be intervening whitespaces between operators and their arguments (unless the argument is quoted). So, both
- expr1 and expr2
- Both
expr1
andexpr2
may be any commands, with the only exception of all control commands. If, at runtime, the conditional expression condition evaluates to truth, the value the first command expr1 will be executed, otherwise the second expr2. If target is specified, the return value of the executed command is assigned.
Notes
- The
COND
command is processed by the loader, and is therefore not available in the command line interface. - The
COND
command may not contain a nestedCOND
command.
See also
conditonal assignment with EVAL, control commands, IF
Examples
#min := cond $#a < $#b ? $#a : $#b // calculate minimum of #a and #b #abs := cond $#a < 0 ? num -$#a : $#a // calculate absolute value of #a #abs := cond $#a < 0 ? eval $#a*(-1) : $#a // an alternative to the above #absdiff := cond $#a > $#b ? eval $#a-$#b : eval $#b-$#a // absolute difference #len := cond $(length $#a) > 0 ? length $#a : length $#b // length of #a or, if empty, of #b
See the file conditional_assignment.sts
for further working examples.