Program structures

All the statements we have written so far are processed sequentially, i.e. line for line from top to bottom. Each statement is executed exactly once, and it is followed the next statement.

In this section, we shall look at creating programs in which statements may be executed several times or not at all on the basis of a condition. For this, we will use loops and alternatives.

A condition is a Boolean value which is tested in a loop or an alternative. However, since a condition can depend on several states (i.e. Boolean values) at the same time, we have to first look at how these states can be joined.

Logical conjunctions

The data type boolean was already described in chapter "Boolean values". We will now look at the operations that can be applied to this type.

Boolean values can be joined with each other. Possible conjunctions are the logical AND (&&) and the logical OR (||). The following rules apply in this respect:

TRUE && TRUE: TRUE

TRUE && FALSE: FALSE

FALSE && FALSE: FALSE

TRUE || TRUE: TRUE

TRUE || FALSE: TRUE

FALSE || FALSE: FALSE

These operations are exclusive. This means that the operand to the right of the operator will not be evaluated, if this can not result in any change to the final result. Let us consider the following conjunction as an example:

1 == 0 && 0 == 0

The expression 1 == 0 is evaluated first. This returns FALSE.

In this case, even if 0 == 0 returns TRUE (which is the case), the final result will still be FALSE, because TRUE && FALSE returns FALSE. It is therefore not necessary to evaluate the expression 0 == 0. It will be ignored.

The following example presents a different situation:

1 == 0 || 0 == 0

Once again, the expression 1 == 0 is evaluated first. This returns FALSE. However, the end result can still change by evaluating the right hand expression, because FALSE || TRUE returns TRUE. This means that the second expression has to be evaluated as well.

Finally, consider the following example:

0 == 0 || 1 == 0

The expression 0 == 0 is TRUE. Regardless of the result that the expression on the right returns, the end result will still be TRUE, because TRUE || FALSE returns TRUE, in the same way that TRUE || TRUE does. It is therefore unnecessary to evaluate the expression 1 == 0. Once again, it will be ignored.

Conditionals

Conditionals are also referred to as branches. A condition is evaluated and if this returns TRUE, then a branch statement is executed. If the condition is not met, the branch is not executed. Alternatively, a different branch can be executed instead.

Expressed in pseudo-code, a simple condition looks like this:

if Condition == TRUE, then execute Statement.

In TDM Studio, this looks as follows:

1 A) if condition then

2 // Any statements

3 B) endif

Notes:

A) The condition to be evaluated must appear between the keywords if and then.

B) A conditional in TDM Studio must be terminated with the keyword endif. In between then and endif, any number of statements can be placed.

It is not necessary to place a semicolon after endif. However, no error will be generated, if you do place one.

Let us assume that we want to check if the value of the variable number is smaller than 0. If this is the case, we want to issue a warning:

1 integer number = -1;

2

3 if number < 0 then

4 Trace(TRACELEVEL_WARNING, """number"" is a negative value.");

5 endif

Since the condition is met in this case, this will result in the following output:

WARNING: "number" is a negative value.

If you set number to a value greater than or equal to zero, then nothing happens, because the statement in Line 4 is not executed.

So far we haven't had any real conditional, but these can easily be created. We will stay with the example above, but now we want to issue a different message, if the content of number is greater than or equal to 0:

1 integer number = 0;

2

3 if number < 0 then

4 Trace(TRACELEVEL_WARNING, """number"" is a negative value.");

5 else

6 Trace(TRACELEVEL_INFO, "Everything OK.");

7 endif

The secret is the keyword else in Line 5. It means that if the condition in Line 3 is not met, then the statement in Line 6 is executed instead of the statement in Line 4.

The condition that you specify can be as complex as required. For instance, you could check if the value lies between 0 and 10 (i.e. in the range 1 to 9):

1 if 0 < number && number < 10 then

2 Trace(TRACELEVEL_INFO, "Everything OK.");

3 else

4 Trace(TRACELEVEL_WARNING,

"""number"" is too small or too large.");

5 endif

The processing priority of the operators means that the comparison is run first and then the conjunction. You can also use brackets to improve the readability:

if (0 < number) && (number < 10) then ...

Finally, there is the keyword elseif. This allows you to join together a number of conditionals:

1 integer number = 12;

2

3 if number &lt; 0 then

4 Trace(TRACELEVEL_WARNING, """number"" is a negative value.");

5 elseif number &lt; 10 then

6 Trace(TRACELEVEL_WARNING, """number"" is smaller than 10.");

7 elseif number &lt; 20 then

8 Trace(TRACELEVEL_WARNING, """number"" is smaller than 20.");

9 else

10 Trace(TRACELEVEL_INFO, """number"" is greater than 19.");

11 endif

If the number is smaller than 0, the statement in Line 4 is executed. After this, you exit the entire conditional, so that no further evaluation takes place.

If, on the other hand, the number is greater than or equal to 0, then the condition in Line 5 is evaluated. Since, in the current example, the number is not smaller than 10, the statement in Line 6 is not executed but instead the condition in Line 7 is checked.

Because this condition is met, the statement in Line 8 is executed and you exit the conditional. If the number had a value of greater than or equal to 20, then the statement in Line 10 would be executed.

Regardless of how many branches a conditional contains, the following rules always apply: Only one branch can ever be executed, not more than one. This is also the case if several conditions are met. In this case, the branch with the first condition that is met will be executed and all others are skipped.