Please enable JavaScript to view this site.

A-Shell Reference

Added October 2016

IFELSE(cond, expr1, expr2)

IFELSE$(cond, expr1, expr2)

These functions allow IF/THEN/ELSE logic to be embedded within a larger statement, eliminating the need for a temporary variable or additional/duplicated statements to deal with an either/or bifurcation.

Each takes a conditional expression (cond) and two value expressions (expr1 and expr2). If the conditional expression evaluates to true, then the function returns the value of expr1, else it returns the value of expr2. IFELSE() returns a numeric value and IFELSE$() returns a string value.

Note that although cond is normally provided in the form of a relational (boolean) expression evaluating to true (-1) or false (0), the compiler is more lenient, allowing a string expression, variable or literal value—even though ill-advised.  See History below and Logical vs. Arithmetic Operators for details.

Examples

Consider the case where you want to print a value with one of two masks depending on whether the value is less than 1. You might code it like this:

if VALUE >= 1 then

    PRINT VALUE USING "#####"

else

    PRINT VALUE USING "#.###"

endif

 

With the IFELSE$() function, you can eliminate the IF statement and reduce the two PRINT statements to one:

PRINT VALUE USING IFELSE$(VALUE>=1,"#####","#.###")

Or, in the case of larger statements where you are more likely to introduce temporary variables than to repeat variations of the statement, IFELSE() can eliminate the need for a temporary variable, e.g.

if flag'disable then

    CSTATE = MBST_DISABLE

else

    CSTATE = MBST_ENABLE

endif

xcall AUI, AUI_CONTROL, CTLOP_ADD, CTLID$, CTEXT$, CSTATE, CTYPE$, CMD$, FUNC$, ...

 

The above can be reduced to a single statement using IFELSE:

xcall AUI, AUI_CONTROL, CTLOP_ADD, CTLID$, CTEXT$, ifelse(flag'disable,MBST_DISABLE,MBST_ENABLE), CTYPE$, CMD$, FUNC$, ...

See Also

IFE() and IFE$()

History

2026 May, A-Shell 7.0.1786.2, compiler edit 1083:  String cond expressions are now explicitly converted to numeric values using the VAL() function. Previously, the interpretation of such a string expression as a boolean was ambiguous, depending on the prior state of the numeric expression stack.

2016 September, A-Shell 6.3.1526, compiler edit 773:  Function added to A-Shell.