<< Click to Display Table of Contents >> Manual > Script Guide > Script Ver2 (SC2 syntax) > Conditional/Repetitive |
Conditional/repetition statements
Describes the control statements that can be used in scripts.
The syntax of each statement closely follows that of the C language. |
Conditional statement (if-else)
▪If Statement
The
if statement allows you to decide whether or not to execute a script depending on the conditional expression. The conditional expression is judged as a Boolean value, and when the expression is TRUE, the script within the scope enclosed in the brackets is executed. Note that if the script is only one line, the brackets can be omitted.
if ( {
} |
▪if-else statement
The
if-else statement can branch the process flow so that either script 1 or script 2 is executed depending on the condition expression. Script 1 is executed when the result is TRUE, and script 2 is executed when the result is FALSE. Note that if each script is only one line, the brackets can be omitted.
if ( {
} else {
} |
▪if- else if - else if - else statement
if- else if - else if - ... - else statements consist of multiple else if statements and a final else statement. The final else statement is optional and can be omitted if not required. Condition expressions are evaluated from top to bottom, and the script in the scope where the condition is TRUE is executed. After evaluating all conditions, if none of the conditions are TRUE, the script written in the final else is executed. Note that if each script is only one line, the brackets can be omitted.
if ( {
} else if ( {
} else {
} |
An example of using an if statement is as follows:
Example 1) Example of including expressions and methods in the conditional expression
if (a == 1 || (b > d * 123) && ::GetVal("TAG01")) { ::WriteVal("TAG01", 1); } else { ::WriteVal("TAG01", 2); }
Example 2) If the script is a single line, you can omit the brackets.
if (a == 1) ::WriteVal("TAG01", 1);
Example 3) You can omit the blankets even if each statement is on a single line.
if (a == 1) ::WriteVal("TAG01", 1); else if (a == 2) ::WriteVal("TAG01", 2); else if (a == 3) ::WriteVal("TAG01", 3); }
Conditional statements (switch-case)
Like the if statement, the switch statement allows you to branch processing based on a conditional expression.
Multi-branch processing with many conditional expressions can be written simply. The switch statement executes a script when the
In addition, you can write a
switch ( { case
break; case
break; ..... case
break; default: } |
example)
switch (a) { case 1: b = ::GetVal("TAG01"); break; case 2: b = ::GetVal("TAG02"); break; case 3: b = ::GetVal("TAG03"); break; }
Loop statements (while)
The while statement runs a script repeatedly while a condition is TRUE. If the script is only one line, you can omit the blankets.
while ( {
} |
▪break Keywords
When you want to exit a loop in the middle of a script while a repetitive process is being executed, use the break keyword. The break keyword can be used anywhere. You can also write multiple break statements. It is usually written in combination with an if statement, so that break is called when the if condition becomes TRUE.
while ( {
if (
if (
} |
▪continue Keyword
If you want to return to the beginning of a loop midway through a repeated process, use the continue keyword. The continue keyword can be used anywhere. You can also write continue multiple times. It is usually written in combination with if, so that continue is called when the if condition becomes TRUE. When continue is called, the while condition is evaluated, and if it is TRUE, the process returns to the beginning of the script in the blanket and execution continues.
while ( {
if (
if (
} |
Furthermore, the break and continue keywords can be used together.
Example 1) The script in the blanket will be executed 100 times.
var a = 0; while (a < 100) { a = a + 1; }
Example 2) Break the loop when the condition is met.
var a = 0; while (1) { a = a + 1; if (a == 100) break; }
Repetition statements (for)
The for statement runs a script repeatedly while a condition expression is TRUE, just like the while statement.
The difference from the while statement is the
Note that you can omit
for ( {
} |
The processing order for for statements is as follows:
1)
2)If the
3)
4)Repeat steps 2 and 3 until the
Example: Execute a script 10 times
for (var i = 0; i < 10; i = i + 1) // i changes to 0, 1, 2.... { //
For example, the script will be executed 10 times.
for (var i = 0; i < 20; i = i + 2) // i changes to 0, 2, 4.... { //