IF ... THEN Conditions

Home, Up: Scripts

 

On this page:

IF ... THEN, ALSO:, ELSE:, The SKIP Command, The NOTHING 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 this 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 ignored (when the result is 0) or executed (when it is not 0).

While technically IF ... THEN conditions can be entered directly, they hardly make sense outside of a script.

 

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

- An IF ... THEN condition cannot be followed by another IF in the same line (conditions can still be nested as we'll see further down on this page).

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

- The loop commands DO, REPEAT and _*filename are not permitted (see chapter "Loops").

- In a script (where conditions will be used) RUN filename or _filename are not permitted.

 

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.

Conditions can be nested in the the form of ALSO: IF ... THEN ...

Note that the IF condition is lost at the end of a script.

 

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 accept its condition as being 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.

Conditions can be nested in the the form of ELSE: IF ... THEN ...

Note that the IF condition is lost at the end of a script.

 

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 as will be discussed in the chapter "Loops".

 

The NOTHING Command

This command does exactly what its name says -- nothing. So, what is it good for?

Actually, it has four possible purposes:

 

It can stand as a placeholder while you develop a script:

IF ... THEN ...

ELSE: NOTHING

...

 

Or it can improve readability when several lines depend on an IF condition:

IF ... THEN NOTHING

ALSO: ...

ALSO: ...

ALSO: ...

 

Or it can help with nested IF ... THEN conditions. IF condition1 THEN IF condition2 THEN ... is not allowed, but instead you can write:

IF condition1 THEN NOTHING

ALSO: IF condition2 THEN ...

With a bit of algebra you could always pack both conditions into one, but it may be easier to do it this way.

Consecutive ALSO: conditions will be true if both conditions have been met, consecutive ELSE: conditions if at least one of them have not.

 

And finally, in a loop it can help resolve a performance issue with IF ... THEN conditions and insert files.

In an IF ... THEN ... line an insert file gets read and inserted into the input line, even if it comes after THEN and the condition is not met.

When, in a loop with a large number of passes, this is a condition that is only rarely met, then considerable time will be wasted on these unnecessary operations. (This also applies to user defined elements after THEN, though these are usually faster.)

After I1:, I*:, ALSO: and ELSE:, though, Hypatia ignores the rest of the line if the condition is not met, and thus does not process any insert files or UDEs that may be part of it.

Therefore, in a loop with many passes, instead of

IF ... THEN ... (myfile) ... @myude ...

it will make the loop faster if you write

IF ... THEN NOTHING

ALSO: ... (myfile) ... @myude ...

 

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