Selection statements change the way a program executes depending on the value of an expression.
selection-statement:
if-statement
switch-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:
Values of logical expressions are consecutively evaluated in the If and Elseif conditions.
If the value of the calculated expression for a condition is True, the block corresponding to this condition is executed. After executing the block, control is transferred to the end point of the statement.
If no condition is satisfied (values of all expressions are False) and the Else block is present, control is transferred to this block and, after it is finished, control is transferred to the end point of the statement.
If no condition is satisfied and the Else block is absent, control is transferred to the end point of the statement.
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 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:
The expression in the header is calculated and converted to the governing type.
If the value of a constant specified in the Case label is equal to the expression value, the block corresponding to this label is executed. After executing the block, control is transferred to the end point of the statement.
If none of the label values match the governing expression value and the Else block is present, the control is transferred to this block and after its execution the control is transferred to the end point of the statement.
If none of the label values match the governing expression value and the Else block is absent, the control is directly transferred to the end point of the statement.
Function TestSelect(a: integer): string;
Begin
Select Case a
Case 0: Return "A=0";
Case 1: Return "A=1";
Case 2: Return "A=2";
Else Return "A<>[0,2]";
End Select;
End Function;
See also: