IF, ELSE, ELSEIF - conditional execution

SYNOPSIS

<IF condition1>
  ... statements if condition1 true ...
[<ELSE[ ]IF condition2>
  ... statements if condition2 true ...]
[<ELSE>
  ... statements if both are false ...]
</IF>


DESCRIPTION
The IF statement evaluates the given conditional expression. If the result is true (i.e. nonzero), then the statements following the <IF> tag are executed. If the result is false, those statements are skipped and control falls to the next statement after the closing </IF> tag.

An optional ELSE clause may be present, in which case the statements following the <ELSE> tag are executed instead if the result is false. If the ELSE clause is another IF statement, it can be made part of the <ELSE> tag by using <ELSEIF>. Thus, the left and right statements below are equivalent:

<IF $x eq 5>                     <IF $x eq 5>
  X is five.                       X is five.
<ELSE>                           <ELSEIF $y gt 10>
  <IF $y gt 10>                    Y is greater than 10.
    Y is greater than 10.        <ELSE>
  <ELSE>                           X is not five and Y is <= 10.
    X is not 5 and Y is <= 10.   </IF>
  </IF>
</IF>

If many ELSEIF clauses are used on the same value, a SWITCH statement (here) may be a clearer alternative.

The condition of an IF statement can be any Texis SQL expression that is valid as a WHERE clause. Thus SQL operators such as LIKE may be used, as well as SQL functions called.

For ease of compliance with the HTML-like syntax of Vortex, quote and operator translation will occur before the expression is parsed by SQL: Double quotes that denote strings will be mapped to single quotes, and single quotes in strings will be escaped to two single quotes. And the following operators will be mapped:

  • eq to =

  • ne or neq to !=

  • gt to >

  • ge or gte to >=

  • lt to <

  • le or lte to <=

In addition, if the expression is $operand$ nmod $operand$, it will be mapped to ($operand$ % $operand$) = 0, for legacy support of the nmod operator, which is deprecated (use SQL modulo operator %).


EXAMPLE

<IF $limit lt 5 and ($ans eq "correct" or $body like $query)>
  You're under the limit, and either
  have the correct answer or the text matches your query.
</IF>


CAVEATS
Variables should not be embedded inside a literal string, e.g. "this is a $test". Literal string values should always be quoted, or they may be misconstrued as non-existent SQL columns.

The results of some operators can depend on the original type of the values, e.g. comparison of integer vs. string values (see discussion under Variable Types, here).

See the syntaxversion pragma (here) to enable deprecated legacy version 7 parsing behavior for older scripts, such as support for simple Vortex expressions.

arrayconvert (here) applies to all variables in IF expressions in version 8 and later, since all IF expressions are now handled by SQL. An empty (no values) variable is considered an empty string.


SEE ALSO
SWITCH


Copyright © Thunderstone Software     Last updated: Jul 17 2021
Copyright © 2021 Thunderstone Software LLC. All rights reserved.