In order to be useful for writing serious programs a language needs compound statements. The first we am going to consider is the 'If' statement.
Before going on to the If statement it is necessary to explain logical ( sometimes called Boolean ) expressions. A logical expression is one that evaluates to a truth values 'True' or 'False'. There are a number of basic operator that will yield a logical result.
|p > q||True if p greater than q|
|p >= q||True if p greater than or equal to q|
|p < q||True if p less than q|
|p <= q||True if p less than or equal to q|
|p == q||True if p equal to q|
|p <> q||True if p not equal to q|
Where the operator consists of two characters there must be no space between them.
If a value being compared with one of the equality test ( '==' or '<>' ) is Null there is no problem. If, however, one of the other operators is used a runtime error will be generated. If comparing against the literal 'Null' the Null should appear on the right hand side of the operator.
The precedence of the comparison operators is lower than for the arithmetic operators so all the numeric calculation will be evaluated before the comparisons are done. So in the following :-
a + b > c + d
Both the additions will be carried out, then the two sums will be compared.
Structure of the If Statement
The overall form of the statements :-
If logical_expression Then
[[ ElseIf logical_expression Then
The parts in Bold are keywords that must appear exactly as shown. The parts in italics must replaced by valid code. The sections between '[' and ']' are optional and may occur at most once. The sections between '[[' and ']]' are optional and may occur more than once.
When the system encounters an If statement the first condition is evaluated and if True the associated statement block will be executed. If the expession is False then the first ElseIf expression - if there is one - is evaluated and if that is True then its associated statement block is executed. This goes on until there are no more ElseIf tests. If no condition evaluates to True and there is an 'Else' block, that block will be executed.
At most one statement block will be executed. If no expressions are True and there is no 'Else' block then no statements will be executed. Note however that if the condition expressions contain function calls and the function calls change the state of global variables ( see later ) then the state of the program may still change even though no statement blocks are executed.
Shape PlanetSymbol(Number n, Number size) Begin If n == 2 Then VenusSymbol(size); ElseIf n == 3 Then EarthSymbol(size); Else ErrorSymbol(size); Output "Bad planet number", n; EndIf; End;stmt_if_1.grs stmt_if_1.png