CoCoA:Cond

From ApCoCoAWiki

Cond

conditional expression

Description

If <formula>B_n</formula> is the first in the sequence of <formula>B_i</formula>'s to evaluate to TRUE,

then <formula>E_n</formula> is returned. If none of the <formula>B_i</formula>'s evaluates to TRUE, then

Null is returned. The construct, Elsif B Then E can be repeated any number of times. Note: be careful not to type Elseif by mistake (it has an extraneous e).

The difference between Cond and If is that Cond is an expression

which may be assigned to a variable; each of the <formula>E_i</formula>'s is an

expression, not a general sequence of commands (as their analogues in If might be).

Example

  Define Sign(A)
    Return Cond A&gt;0 Then 1 Elsif A=0 Then 0 Else -1 EndCond;
  EndDefine;
  Sign(3);
1
-------------------------------
  Define PrintSign(A)
    Return Cond(A&gt;0,<quotes>positive</quotes>,A=0,<quotes>zero</quotes>,<quotes>negative</quotes>);
  EndDefine;
  PrintSign(3);
positive
-------------------------------

Syntax

Cond B_1 Then E_1 EndCond
Cond B_1 Then E_1 Elsif B_2 Then E_2 Elsif ... EndCond
Cond B_1 Then E_1 Elsif B_2 Then E_2 Elsif ... Else E_r EndCond
Cond(B_1,E_1,B_2,E_2,...,E_r)

where the B_i's are boolean expressions and the <formula>E_i</formula>'s are expressions.

If

   <type>programming</type>
   <type>branching</type>