Control Statements
Some statements serve to control the processing flow.
All control statements can be grouped into the following categories:
Conditional Statements
These statements serve to perform different set of statements depending on condition value.
If Statement
On the basis of the Condition
value, this statement decides whether the Statement
should be executed. If the Condition
is true, the Statement
is executed. If it is false, the Statement
is ignored and the process continues next after the if
statement. The Statement
is either a simple statement or a block of statements:
if (Condition) Statement
Unlike the previous version of the if
statement (in which the Statement
is executed only if the Condition
is true), other Statements
that should be executed even if the Condition
value is false can be added to the if
statement. Thus, if the Condition
is true, the Statement1
is executed, if it is false, the Statement2
is executed. See below:
if (Condition) Statement1 else Statement2
The Statement2
can even be another if
statement, and also with an else
branch:
if (Condition1) Statement1
else if (Condition2) Statement3
else Statement4
Example 72. If statement
integer a = 123;
if ( a < 0 ) {
a = -a;
}
Switch Statement
Sometimes you would have very complicated statement if you created the statement of more branched out if
statement. In this case, it is much more convenient to use the switch
statement.
Now, instead of the Condition
as in the if
statement with only two values (true or false), an Expression
is evaluated and its value is compared with the Constants
specified in the switch
statement.
Only the Constant
that equals to the value of the Expression
decides which of the Statements
is executed.
If the Expression value is Constant1
, the Statement1
will be executed, etc.
Remember that literals must be unique in theSwitch statement
.
switch(Expression) {
case Constant1 : Statement1 StatementA [break;]
case Constant2 : Statement2 StatementB [break;]
...
case ConstantN : StatementN StatementW [break;]
}
The optional break;
statements ensure that only the statements corresponding to a constant will be executed. Otherwise, all below them would be executed as well.
In the following case, even if the value of the Expression
does not equal the values of the Constant1,…,ConstantN
, the default statement (StatementN+1
) is executed.
switch (Expression) {
case Constant1 : Statement1 StatementA [break;]
case Constant2 : Statement2 StatementB [break;]
...
case ConstantN : StatementN StatementW [break;]
default : StatementN+1 StatementZ
}
Example 73. Switch statement
integer ok = 0;
switch ( response ) {
case "yes":
case "ok":
a = 1;
break;
case "no":
a = 0;
break;
default:
a = -1;
}
Iteration Statements
Iteration statements repeat some processes during which some inner Statements
are executed repeatedly until the Condition
that limits the execution cycle becomes false or they are executed for all values of the same data type.
For Loop
Firstly, the Initialization is set up. Secondly, the Condition
is evaluated and if its value is true, the Statement
is executed. Finally, the Iteration
is made.
During the next cycle of the loop, the Condition
is evaluated again and if it is true, Statement
is executed and Iteration
is made. This way the process repeats until the Condition
becomes false. Then the loop is terminated and the process continues with the other part of the program.
If the Condition
is false at the beginning, the process jumps over the Statement
out of the loop.
for (Initialization;Condition;Iteration)
Statement
Remember that theInitialization
part of theFor Loop
may also contain the declaration of the variable that is used in the loop.
Initialization
,Condition
andIteration
are optional.
Example 74. For loop
integer result = 1;
integer limit = 5;
for(integer i = 1; i <= limit; ++i) {
result = result * i;
}
Do-While Loop
Firstly, the Statement
is executed. Secondly, the value of the Condition
is evaluated. If its value is true, the Statement
is executed again and then the Condition
is evaluated again and the loop either continues (if it is true again) or stops and jumps to the next or higher level subprocesses (if it is false).
Since the Condition
is at the end of the loop, even if it is false at the beginning of the subprocess, the Statement
is executed at least once.
do Statement while (Condition)
integer a = 5;
integer sum = 0;
do {
sum = sum + a;
a--;
} while (a > 3);
While Loop
The processing depends on the value of the Condition
. If its value is true, the Statements
is executed and then the Condition
is evaluated again and the processing either continues (if it is true again) or stops and jumps to the statement following the cycle (if it is false).
Since the Condition
is at the beginning of the loop, if it is false before entrance to the loop, the Statements
is not executed at all and the loop is jumped over.
while (Condition) Statement
integer a = 5;
integer sum = 0;
while ( a > 3 ) {
sum = sum + a;
a--;
For-Each Loop
The foreach
statement is executed on all fields of the same data type within a container. Its syntax is as follows:
foreach (<data type> myVariable : iterableVariable) Statement
All elements of the same data type (data type is declared in this statement) are searched in the iterableVariable
container. The iterableVariable
can be a list, map, record or variant. For each variable of the same data type, specified Statement
is executed. It can be either a simple statement or a block of statements.
Thus, for example, the same Statement
can be executed for all string
fields of a record, etc.
It is possible to iterate over values of a map (i.e. not whole <entries>
). The type of the loop variable has to match the type of map’s values:
map[string, integer] myMap = {'first' -> 1, 'second' -> 2};
foreach(integer value: myMap) {
printErr(value); // prints 1 and 2
}
To obtain map’s keys as a list[]
, use the getKeys() function.
When iterating over a variant (if it contains a list, map or a record), use variant as the loop control variable type: foreach (variant v: …)
variant myVariant = [1, 'hello', true, today()];
foreach(variant value: myVariant ) {
printErr(value); // 1, 'hello', true and actual date
}
Jump Statements
Sometimes you need to control the process in a different way than by decision based on the Condition
value. To do that, you have the following options:
Break Statement
If you want to jump out of a loop or of a switch, you can use the following statement in the program:
break;
The processing of a loop (or switch) is relinquished and it continues with Statements
following the loop or switch.
Continue Statement
If you want to stop processing of some iteration and go to next one, you can use the following statement in the program:
continue;
The processing jumps to the end of a loop, iteration is performed (in for loop) and the processing continues with next iteration step.
Return Statement
In the functions, you can use the return word either alone or along with an expression
. (See the following two options below.)
The return statement can be in any place within the function. There may also be multiple return
statements among which a specific one is executed depending on a condition, etc.
return;
return expression;
Updated 12 months ago