Selection Statements

Selection statements change the way a program executes depending on the value of an expression.

selection-statement:

if-statement

switch-statement

The If Statement

The If statement selects a way the program executes based on the value of a Boolean expression.

if-statement:

If   boolean-expression   Then   block   elseif-clausesopt   else-clauseopt   End If

boolean-expression:

expression

elseif-clauses:

elseif-clause

elseif-clauses   elseif-clause

elseif-clause:

Elseif   boolean-expression   Then   block

else-clause:

Else   block

The If statement is executed as follows:

Example

Sub TestIf();
Var
    a, b: Integer;
    c: Double;
Begin
    //...
    //Get the a, b, c values
    //...
    If (a > b) And (b > c) Then
        c := a - b;
    Elseif (a < b) And (b < c) Then
        c := b - a;
    Else
        c := (a + b) / 2
    End If;
    //...
End Sub;

The Select Statement

The Select statement is used to select a list of statements for execution that have corresponding labels by comparing a label to the expression value.

switch-statement:

Select Case   expression   switch-sections   else-clauseopt   End Select

switch-sections:

switch-section

switch-sections   switch-section

switch-section:

Case   switch-labels   :   block

switch-labels:

switch-label

switch-labels   ,   switch-label

switch-label:

constant-expression

The header of the Select statement contains an expression calculation, which determines the executed statement block. The type of the expression in the statement header is named the governing type. The constant expressions in label lists should have values that can be implicitly converted to the governing type of the statement. If two or more label values are the same, a compile error occurs.

The Select statement is executed as follows:

Example

Function TestSelect(a: integer): string;
Begin
    Select Case a
        Case 0Return "A=0";
        Case 1Return "A=1";
        Case 2Return "A=2";
        Else Return "A<>[0,2]";
    End Select;
End Function;

See also:

Statements