_________________________________________________________________________________________________________
If then statements
___________________________________________________________________
The expression between the if and then keywords must have a boolean return type. If the expression evaluates to True then the statement following then is executed.
If the expression evaluates to False, then the statement following else is executed, if it is present.
Be aware of the fact that the boolean expression will be short-cut evaluated. (Meaning that the evaluation will be stopped at the point where the outcome is known with certainty) Also, before the else keyword, no semicolon (;) is allowed, but all statements can be compound statements. In nested If.. then .. else constructs, some ambiguity may araise as to which else statement pairs with which if statement. The rule is that the else keyword matches the first if keyword not already matched by an else keyword. For example:
If exp1 Then If exp2 then Stat1 else stat2; |
If exp1 Then begin If exp2 then Stat1 else stat2 end; |
{ NOT EQUIVALENT } If exp1 Then begin If exp2 then Stat1 end else stat2 |
The following is a valid statement:
If Today in [Monday..Friday] then WriteLn ('Must work harder') else WriteLn ('Take a day off.'); |