If/Then/Else Statements

The if/then statement is a conditional statement that executes its sub-statement, which follows the then keyword, only if the provided condition evaluates to true:

if x < 10 then
  x := x+1;

In the above example, the condition is x < 10, and the statement to execute is x := x+1. As such, the code will increment x by one only if its current value is still less than 10.

else clause

Optionally, an else clause with a second sub-statement can be provided. This second statement will be executed instead of the first one if the condition was false. It is always guaranteed that one of the two statements will execute:

if x < 10 then
  x := x+1
else
  x := x+10;

In the above example, the code will increment x by one if its current value is still less than 10, as before. However, if x is already 10 or larger, it will be incremented by 10 instead.

A Note on Semicolons

Note how the second example above has no semicolon (;) after the first statement, the one that will execute if the condition is true. As you already learned in the Statements topic, in Oxygene semicolons are used to separate statements, not terminate them.

Technically speaking, the semicolon on the first code snippet does not belong to the inner x := x+1 statement. Instead, it separates the entire if/then statement from whatever may come after it. In the second example, the entire if/then/else statement does not end until after the fourth line, so that is the first place a semicolon is valid.

One could argue that it should not be present in the code example at all – however, Oxygene convention is to write the trailing semicolon after each statement, even single ones, and that's why the snippet included it.

if/then Statements and begin/end blocks.

On its own, the if/then statement, as well as its optional else clause, only takes a single statement to be executed for each of the two cases. To execute more than one statement, multiple statements can be grouped using a begin/end Block Statement:

if x < 10 then begin
  x := x+1;
  writeLn('increased by 1.');
end
else begin
  x := x+10;
  writeLn('increased by 10.');
end;

Optionally, a begin/end statement block can be used, even if only a single statement is provided. This is common practice to keep code clean and readable, and to avoid the common mistake of forgetting to add begin/end when later expanding a single-statement if/then statement.

It is also common practice and highly recommended to either consistently use or not use a begin/end pair for both the then and the else statement, even if not necessary. It helps to keep code balanced.

if x < 10 then begin
  x := x+1;
  y := y+1;
end
else
  x := x+10; // feels unsymmetrical with the 'then' block above

if x < 10 then begin
  x := x+1;
  y := y+1;
end
else begin
  x := x+10; // balances nicely, even if the 'begin/end' is unnecessary here.
end;

Nullable Conditions

The condition expression for the if/then statements must be of Boolean or Nullable Boolean type.

If the condition is a simple boolean, the if/then statement will execute the then clause if the condition is true, and the (optional) else clause if the condition is false.

If the condition is a Nullable Boolean type, then the additional case of the condition evaluating to nil needs to be considered. While a nil nullable boolean strictly speaking is not equivalent to false, the if/then statement treats them the same, and will execute the else clause, if provided, in this case.

This behavior symmetrically extends to while/do loops, which also treat a nil condition as false and will exit the loop, while repeat/until loops will treat a nil condition as false and keep running the loop.

See also