Control Statements

Some statements serve to control the process of the program.

All control statements can be grouped into the following categories:

Conditional Statements

These statements serve to branch out the process of the program.

If Statement

On the basis of the Condition value this statement decides whether the Statement should be executed. If the Condition is true, Statement is executed. If it is false, the Statement is ignored and process continues next after the if statement. Statement is either simple statement or a block of statements

  • if (Condition) Statement
    

Unlike the previous version of the if statement (in which the Statementis 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, Statement1 is executed, if it is false, Statement2 is executed. See below:

  • if (Condition) Statement1 else Statement2
    

The Statement2 can even be another if statement and also with else branch:

  • if (Condition1) Statement1 
        else if (Condition2) Statement3
             else Statement4
    
Switch Statement

Sometimes you would have very complicated statement if you created the statement of more branched out if statement. In such a case, much more better is 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.

[Important]Important

Remember that literals must be unique in the Switch 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 correspoding 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 to 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
    }

Iteration Statements

These iteration statements repeat some processes during which some inner Statements are executed cyclically until the Condition that limits the execution cycle becomes false or they are executed for all values of the same data type.

For Loop

First, the Initialization is set up, after that, the Condition is evaluated and if its value is true, the Statement is executed and 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
[Important]Important

Remember that the Initialization part of the For Loop may also contain the declaration of the variable that is used in the loop.

Initialization, Condition, and Iteration are optional.

Do-While Loop

First, the Statement is executed, then the process depends on the value of Condition. If its value is true, the Statement is executed again and then the Condition is evaluated again and the subprocess 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)
While Loop

This process depends on the value of Condition. If its value is true, the Statements is executed and then the Condition is evaluated again and the subprocess 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 start of the loop, if it is false at the beginning of the subprocess, the Statements is not executed at all and the loop is jumped over.

  • while (Condition) Statement
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, a map, or a record. 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.

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 stop some subprocess, you can use the following statement in the program:

  • break;

The subprocess breaks and the process jumps to the higher level or to the next Statements.

Continue Statement

If you want to stop some iteration subprocess, you can use the following statement in the program:

  • continue;

The subprocess breaks and the process jumps to the 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;