IF ... THEN Conditions

Home, Up: Scripts

 

On this page:

IF ... THEN, IFNOT ... THEN, ALSO:, ELSE:, The SKIP Command, The GOTO 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 (which is 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 a value of zero meets the condition, use IFNOT ... instead of IF ... (see below), or add IS0 after the variable or expression.

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.

 

IFNOT ... THEN

This is the same as IF ... THEN, only that the condition is met when the variable or expression after IFNOT is exactly zero.

Other than this there is no difference between IFNOT and IF, they are used in exactly the same way, everything below about IF also applies to IFNOT.

 

IFNOT ... THEN is a simpler way to say IF ... IS0 THEN, but there is a subtle difference:

With the IS0 operator performing the negation, values below the zero threshold become 0 and thus false. IFNOT, though, considers all values unequal zero to be true, and thus not meeting the condition.

For instance, if (with the default zero threshold of 1e-16) $a has the value 1e-18, then the IF $a IS0 condition is met -- $a is considered to be zero -- while the IFNOT $a condition would not be met -- $a is not excatly zero.

While this may be a bit confusing, IFNOT and IF behave exactly alike -- they both ignore the zero threshold and, if necessary, must be made to respect it with IS0 or ISNOT0 (for which you can also write IS+-).

 

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, with one exception:

ALSO: IF ... THEN ... and IFNOT ... THEN ... are allowed, this way conditions can be nested.

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

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 ELSE: is true, that is, if in the preceding IF ... line the condition 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 (outside of a loop ABORT does the same as SKIP, though).

 

The GOTO Command

This will be discussed in more detail on the next page, but instead of skipping the rest of the script, GOTO lets you skip only a part of it:

 

IF ... THEN GOTO mylabel

...

LABEL: mylabel

...

 

As said, more about this on the next page.

 

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 can be easier to do it this way.

 

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: GOTO and Labels