Programmer Guide/Command Reference/COND: Difference between revisions

From STX Wiki
Jump to navigationJump to search
No edit summary
 
(27 intermediate revisions by 3 users not shown)
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==
[ <var>target</var> := ] COND [[Programmer_Guide/Introduction#Conditional_Expressions|condition]] ? <var>expr1</var> : <var>expr2</var>
;<var>target</var>: is a normal assignment target, usually the name of an {{STX}} variable, e.g. <code>#var</code>. If no target is specified, a conditional execution is performed.
;<var>condition</var>: is a [[Programmer_Guide/Introduction#Conditional_Expressions|conditional expression]] like used with the <code>[[Programmer_Guide/Command_Reference|IF]]</code> statement and the miscellaneous conditional [[Programmer_Guide/Introduction#Control_Commands|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 <code>$#a == 7</code> and <code>'$#a'=='7'</code> are valid expression, whereas <code>$#a==7</code> is not.
;<var>expr<sub>1</sub></var>, <var>expr<sub>2</sub></var>: Both <code>expr<sub>1</sub></code> and <code>expr<sub>2</sub></code> may be any commands, with the only exception of all control commands. If, at runtime, the conditional expression <var>condition</var> evaluates to truth, the first command <var>expr<sub>1</sub></var> will be executed, otherwise the second <var>expr<sub>2</sub></var>. If <var>target</var> is specified, the return value of the executed command is assigned.


Generally, the conditional assignment takes the following form:
==Notes==
target := COND condition ? expr1 : expr2
 
;<code>target</code>: is a normal assignment target, usually the name of an S_TOOLS-STx variable, e.g. <code>#var</code>.
;<code>condition</code>: is a conditional expression like used with the <code>[[User Guide/Workspace/Parameter Processing|IF]]</code> statement and the miscellaneous loop commands, e.g. the string <code>$#a == 7 || $#a == 42</code> (for more examples, see the <code>[[User Guide/Workspace/Parameter Processing|IF]]</code> statement).
:Note that with conditional S_TOOLS-STx expressions there must always be intervening whitespaces between operators and their arguments (unless the argument is quoted). So, both <code>$#a == 7</code> and <code>'$#a'=='7'</code> are valid expression, whereas <code>$#a==7</code> is not.
;<code>expr1</code> and <code>expr2</code>: Both <code>expr1</code> and <code>expr2</code> may be any expressions that may be normally used in an S_TOOLS-STx <code>:=</code> assignment, with the only exception of a <code>COND</code> expression (meaning that conditional assignments must not be nested for the time being). If, at runtime, the conditional expression <code>condition</code> evaluates to truth, the value determined by the first expression, <code>expr1</code>, will be assigned to <code>target</code>. If <code>condition</code> evaluates to falsehood, it will be the second expression, <code>expr2</code>, that gets assigned to target.
 
You may use any S_TOOLS-STx conditional expression you like for <code>condition</code>. Equally, there is no restriction on <code>expr1</code> and <code>expr2</code>, except that they must not be built up from <code>COND</code> expressions themselves. It is even (and also) possible to use command substitutions (<code>$(...)</code>), nesting them as deeply as one feels inclined to.
 
=== Notes ===
* The <code>COND</code> command is processed by the loader, and is therefore not available in the command line interface.
* The <code>COND</code> command is processed by the loader, and is therefore not available in the command line interface.
* The <code>COND</code> command may not contain a nested <code>COND</code> command.
* The <code>COND</code> command may not contain a nested <code>COND</code> command.
==See also==
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
#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


=== Examples ===
  cond '$#item[?]' == table ? gosub useTable $#item : gosub useOtherItem $#item
#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 <code>conditional_assignment.sts</code> for further working examples.
See the file <code>conditional_assignment.sts</code> for further working examples.
<!-- C.G. 14.3.2011 -->
<!-- C.G. 14.3.2011 -->
<!-- A.N. 27.4.2011 -->

Latest revision as of 15:26, 24 April 2014

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.
expr1, expr2
Both expr1 and expr2 may be any commands, with the only exception of all control commands. If, at runtime, the conditional expression condition evaluates to truth, 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 nested COND 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
cond '$#item[?]' == table ? gosub useTable $#item : gosub useOtherItem $#item

See the file conditional_assignment.sts for further working examples.

Navigation menu

Personal tools