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 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.

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 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.

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.

 

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".

 

The NOTHING Command

This command does exactly what its name says -- nothing. And admittedly, you'll probably never need it. 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 of 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 IF ... THEN ... lines insert files get read and inserted into the input line, even if they come after THEN and the condition is not met.

If, in a loop with a large number of passes, this is a condition that is only rarely met, then considerable time may 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 ...

 

You are right, you could use $ instead of NOTHING, as in a script the command $ also does nothing. I just thought it would be nicer to have a command especially for this purpose.

 

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