IF ... THEN Conditions

Home, Up: Scripts

 

On this page:

IF ... THEN, ALSO:, ELSE:, The SKIP Command.

 

IF ... THEN

The execution of a command or a calculation in a script can be made to depend on whether a condition is met or not.

This condition is a number -- either a variable or an arithmetic expression. Zero means false, any value not zero means true.

 

IF variable or expression THEN command, calculation or comment

 

When IF is followed by an arithmetic expression it will be calculated, but its result will not be displayed, not saved to hy, and $ and $$ will not be updated -- it will only be used to decide whether the part that follows THEN will be executed or ignored.

While technically IF ... THEN conditions can be entered directly, they would make little sense outside of a scipt.

 

If the condition is met (that is, if the value after IF is not zero), everything that follows THEN will be treated exactly as if it were a regular input line, with these exceptions:

- IF ... THEN clauses can not be nested, that is, THEN cannot be followed by another IF.

- The commands RESET, REPEAT and Q, QUIT or EXIT cannot be used.

- A script cannnot call another script (or itself), therefore RUN filename or _filename are not permitted within a script.

In case that THEN is followed by a comment (anything that begins with #) this will be displayed and (if logging mode is ON) written to hy.log.

 

Only the exact value of zero means false, the zero threshold is not applied (by default 1e-16, see page "The Zero Threshold" in chapter "Numbers").

If you want values below the zero threshold to mean false, add ISNOT0 after the variable or expression.

If you want to invert the result, so that zero means true, add IS0.

With IS+ and IS- only positive/negative numbers mean true, with IS0+ and IS0- positive/negative numbers and zero mean true.

IS0, ISNOT0, IS+, IS0+, IS– and IS0– consider absolute values below the zero threshold to be zero. The compare operators treat differences below the zero threshold as equal.

 

ALSO:

If you need more than one command or calculation executed (or comment displayed) when a condition is met, you can add one or more ALSO: lines after the IF ... THEN line:

 

IF variable or expression THEN command, calculation or comment

ALSO: command, calculation or comment

 

Everything that comes after ALSO: in that line (do not omit the colon!) is treated the same way as if it had followed THEN, the same rules and restrictions apply.

Any number of ALSO: lines can follow an IF ... THEN line, even with other lines in between.

At the end of the script the result of the IF condition is lost.

 

ELSE:

A command or calculation can be executed (or comment displayed) when the IF condition has not been met, by adding an ELSE: line:

 

IF variable or calculation THEN command, calculation or comment

ELSE: command, calculation or comment

 

ELSE: inverts the outcome of the IF ... condition.

Any ALSO: line that comes after ELSE: will follow its example -- that is, it will see its condition as met if in the preceding IF ... line it had not been met.

Another ELSE: line would again invert the condition, reverting it back to the original outcome.

Again, ALSO: and ELSE: lines do not need to follow an IF ... THEN line immediately, there can be any number of lines in between.

 

The SKIP Command

SKIP can follow IF ... THEN, ALSO: or ELSE: to skip the rest of the script if a condition is met -- for instance

IF $a IS0 THEN SKIP

would mean that the rest of the script only gets executed when the value of the variable $a is not zero.

 

Do not confuse SKIP with the commands ENDLOOP and ABORT, which end a loop and will be discussed in the chapter "Loops".

 

Home, Up: Scripts, Prev: Introduction to Scripts, Next: Comments