12.1.6 Statements

The following table is a set of statements and keywords that can be used in the script.

Type

Description

IF-ELSE Statement

Evaluates the expression after “If“. If the expression is true, then the code immediately following “If“ will be executed. Otherwise, the contents immediately following “Else“ will be executed.

WHILE/DO-WHILE Statement

Statements for loop processes.

FOR Statement

A statement for more complex loop processes.

SWITCH-CASE Statement

Statement for executing different processes based on different conditions.

GOTO Statement

A statement that forces the flow of the program to go to a specific location in the script.

CONTINUE Keyword

A keyword used in loop processes.

RETURN Keyword

A keyword that designates the value that will be returned to the caller.

RUNSCRIPT Keyword

A keyword that calls an external program.

(1) IF-ELSE Statement

The IF-ELSE statement is one of the most frequently used logical decision-making statements. It supports nested IF statements such as: If (condition 1) { If (condition 2) { … } }. There is no limit on the level of nested IF. The Else statement may be omitted. The ‘Else If’ statement is used for additional conditions following an If statement. For a given If-Else If-Else statement, only one branch will be executed. Once one condition is met, none of the following “Else If“ or “Else“ conditions are evaluated.

If (A == 1)

{

    // A program for when A is 1

}

Else If (A == 2)

{

    // A program for when A is 2

}

Else

{

    // A program for when A is neither 1 nor 2

}

 (2) WHILE/DO-WHILE Statement

The WHILE/DO-WHILE is a statement for a loop process. The statements below the WHILE keyword will be repeated as long as the condition followed by the WHILE keyword is True.

A = 0;

While (A<10)

{

// Write the program that will be repeated.  

// while the WHILE keyword’s condition is True.

// This example will be repeated 10 times.

A = A +1;

}

The subordinate statements will never be executed if the conditions for the WHILE statement are not true. If you use a DO-WHILE statement, the loop statement will be executed at least once. After the first execution, the system will check the condition within the WHILE statement.

Do {

// Write the loop statement when the condition is True.

// The loop statement will be executed once at least.

} While (A<10); // mark the end of the statement with ‘ ; ’.

(3) FOR Statement

In this statement, all expressions for the initialization, the decision-making, and the post-loop process are described in a single statement line.

The following example displays the WHILE statement example shown above but using the FOR statement instead.

For (A=0; A<10; A=A+1)

{

// Write the loop statement.

}

The FOR statement is followed by the initialization statement, the decision-making statement, and the post-processing statement.

FOR (Initialization statement; Decision-Making statement; Post-Processing statement)

In the example above, the loop statement will be executed 10 times. On the first execution, A will have a value of 0. After this, the statement “A=A+1“ will execute, and the loop will run again. The loop will continue to run until A is equal to 10 or greater.

(4) SWITCH-CASE Statement

This statement is used when different processes are needed for different cases. Each case can be distinguished by an unlimited number of CASE statements.

The CASE keyword must be followed by a constant value and terminated with a colon “ : ”. Tags, variables, or expressions cannot be used in a CASE statement. Only one DEFAULT statement can be used in the SWITCH statement.

The DEFAULT statement does not need a constant. This statement is executed if none of the other cases are to be executed. The DEFAULT statement can be omitted if it is not needed.

The BREAK keyword plays the role of terminating the SWITCH statement. If there is no BREAK keyword after a CASE statement, then corresponding CASE/DEFAULT statements will also be executed. This means that multiple CASEs can execute the same result, as shown in the example below.

Switch (A)

{

Case 1:

// Write the program when A is 1.

Break;

Case 5:

Case 7:

// Write the program when A is 5 or 7.

Break;

Default:

// Write the default program. This section can be omitted.

Break;

}

(5) GOTO Statement

The GOTO statement controls the flow of the program directly. This statement uses an index (unique name starting with “@”) to point to the new location.

The index name must not be duplicated with variables, tags, or program names.

VAR A;

A = 0;

@ComeHere            // Assign an index.

If (A < 10)

{

    A = A + 1

    Goto ComeHere;   // Move to the assigned index.

}

(6) CONTINUE Keyword

The CONTINUE keyword is used in loop statements. When the system meets the CONTINUE keyword, the flow skips the remainder of the loop and moves to the first part of the current loop. This keyword is often used in the WHILE, DO-WHILE, and FOR statements. It is especially useful when there are multiple decision-making statements (IF-ELSE) in the loop.

The following examples operate identically.

While (A<10)

{

A = A +1;

If (A < 5)

{

// Process when A is less than 5

}

Else

{

  // Process when A is greater than or equal to 5

}

}

 

While (A<10)

{

A = A +1;

If (A < 5)

{

// Process when A is less than 5

   Continue;

}

  // Process when A is greater than or equal to 5

}

(7) RETURN Keyword

Every program returns a result value to the caller. The RETURN keyword is used for designating a return value and termination of the program.

The following example processes three input parameters and returns the sum of them to the caller.

The name of the program shown below is MyPgm.

PARAM p1, p2, p3;

Return p1+p2+p3;

The program MyPgm will be called in another program as shown below. Then the variable ‘RtnValue’ will receive the value ‘6’.

RtnValue = MyPgm(1, 2, 3);

 (8) RUNSCRIPT Keyword

When you call an external program directly, the caller will be in standby mode until the called program (the external program) ends its operation.

However, if you use the RunScript keyword to call an external program, the caller will not wait until the end of the called program. Both the caller and called program will be executed in parallel.

If (A == 0)

RunScript NewThread(); // Executes an external program (NewThread)

A = A + 1;              // Continues the rest of the program without stalling.