Selection statements are used to change how a program executes depending on the value of an expression. There are the following conditional statements in Fore.NET:
The ?: conditional statement returns one of two values depending on the value of passed logical expression.
If an expression returns True, the expression followed by the ? character is calculated and becomes the result. If an expression returns True, the expression followed by the : character is calculated and becomes the result. In any case, only one of two expressions is returned.
Imports System;
Imports System.Diagnostics;
Sub TestChoice();
Var
RndObj: Random = New Random();
i, j: Double;
Begin
i := RndObj.NextDouble();
j := RndObj.NextDouble();
Debug.WriteLine(i > j ? "i>j" : "i<j");
End Sub;
The If statement is used to select how a program executes based on the value of a logical expression.
The statement is executed as follows:
Values of logical expressions are consecutively calculated 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 passed 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 passed to this block and, after it is finished, control is passed to the end point of the statement.
If no condition is satisfied and the Else block is absent, control is passed 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.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 compilation 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 passed 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 passed to this block and after its execution the control is passed 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 passed 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: