##### 37.6.4.2 Using conditional expressions

A conditional expression starts with:

<$_if (expr)>

or:

<$_if not (expr)>

and continues with:

<$_elseif (expr)> (as many as you please)
<$_elseif not (expr)> (as many as you please)
<$_else> (evaluated when no expr is true)
<$_endif> (optional if at the end of a macro)

(As an alternative to a long list of <$_elseif> clauses, you could use an indexed array for the (expr) values; see §37.4.6 Using a list instead of a conditional expression.)

Result of testing a string value

If (expr) has a string value, that value is seen as a non-number, and (expr) would evaluate to zero; that is, false. The relational operators always return “0” (false) or “1” (true), so if you had a variable $$myword with yes/no values, you would have to test the value like this:

<$_if ($$myword is yes)>

or like this

<$_if ($$myword is "yes")>

because, by itself:

<$_if ($$myword)>

would never be true.

How to nest conditionals

You cannot nest <$_if>s (and <$_if not>s) in the same macro; instead, call a second macro from within the first, and include the subordinate <$_if> (or <$_if not>) in the second macro. You specify a limit to such macro nesting with the following setting (see §37.1.3 Nesting macros):

[Macros]
MacroNestMax=128

Conditionals within expressions

You can also use C-style ternary operators “?” and “:”, for a shorthand version of a conditional expression. For example:

<$($$myvar ? "yes" : "no")>

instead of:

<$_if ($$myvar)>yes<$_else>no<$_endif>

The ternary operators give you a natural way to use conditionals within an expression, which is otherwise impossible.

Previous Topic: 37.6.4.1 Understanding control-structure elements

Next Topic: 37.6.4.3 Using loop structures

Parent Topic: 37.6.4 Using control structures in expressions

Sibling Topics:

37.6.4.1 Understanding control-structure elements

37.6.4.3 Using loop structures

Table of Contents • Index