The If
command is a core logic construct in any language. It allows for conditional evaluation and output.
There are four commands that belong to the If
family - If
, ElseIf
, Else
, and EndIf
. Each If
command has to have a corresponding EndIf
command, and in between there can be any number of ElseIf
commands, and optionally one Else
command as well.
The If
and ElseIf
commands consist of the command followed by an expression. The Else
and EndIf
commands do not use expressions, and so will ignore any expression given. If the result of the expression evaluation equates to Boolean true
(see Converting to Boolean), then the information contained after the command, and before the next command in the same control structure, is evaluated by the PTP engine. Otherwise, the next command in the structure is checked. Once one of the expressions has equated to true
, none of the other command expressions in the structure are evaluated. If an Else
is present, it will be deemed true
if the initial If
statement, and none of the preceding ElseIf
statements, have equated to false
.
For example:
{ if 5 == 6 }… Well, that's weird! 5 is equal to 6! { elseif 9 == 9 }… All's well - 9 is equal to 9 { else }… Something seems to have gone wrong... { endif }
All's well - 9 is equal to 9
If
structures can be nested indefinitely within other If
structures, and within other command structures.