Conditional statements enable the user to execute only part of code depending on some conditions. There are the following conditional statements in Fore:
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.
Sub Main;
Var
i, j: Double;
Begin
i := Math.Rand;
j := Math.Rand;
Debug.WriteLine(i > j ? "i>j" : "i<j");
End Sub Main;
When the example is executed, two random numbers are generated. The result of comparison of the numbers is displayed in the development environment console.
The If statement ensures conditional execution of statements depending on the specified logical expressions. The block of statements starts with the If...Then keywords, between them the checked expression is specified. Then there several optional blocks Elseif...Then may follow, containing other checked expressions. If there is an expression, which value is True, the statements sequence is executed. The Else block can be specified after all the blocks, the block contains the statements that will be executed if none of the expressions returns True. After a group of statements is executed, the statement following the End If keywords takes control.
Sub IfSample;
Var
a, b: Integer;
c: Double;
Begin
//...
//Get values a, b, c
//...
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 IfSample;
The Select Case statement provides the statements sequence selection and execution according to the expression value. After the Select Case keywords you need to enter the expression, which value will be checked. The Case blocks contain the values of the expression that are under check. A single Case block may contain:
One value under check.
Several values separated with commas.
A range of values specified using the To keyword.
After you specify the checked value you need to type in a colon and specify a block of statements executed for this Case block.
If the expression value does not match either of the constants specified in the Case blocks, the platform executes a statements sequence from the Else block, if there is one. The checked expression and values specified in the Case blocks should be of one type. Each of the checked values should be used only in one Case block. Values ranges cannot overlap. You can use the following types of expression and constants: Integer, Decimal, Double, Char, String, Boolean.
Sub SelectCaseSample;
Var
a, c: Integer;
Begin
//...
//Get values a, b, c
//...
Select Case a
Case 1: c := c + 5;
Case 2: c := c + 4;
Case 3: c := c + 3;
Case 4: c := c + 2;
Case 5: c := c + 1;
Case 6: c := c - 2;
Case 7: c := c - 3;
Case 8: c := c - 4;
Case 9: c := c - 5;
Else c := 0;
End Select;
Select Case a
Case 1, 2, 3: c := c + 10;
Case 4, 5, 6: c := c + 1;
Case 7, 8, 9: c := c - 10;
Else c := 0;
End Select;
Select Case a
Case 1 To 3: c := c + 10;
Case 4 To 6: c := c + 1;
Case 7 To 9: c := c - 10;
Else c := 0;
End Select;
//...
End Sub SelectCaseSample;
See also: