Link Search Menu Expand Document

3.14 Control Structure

CRS control structures are defined with almost the same syntax as JavaScript.

if… else statement

It is a conditional branch.
    if ( conditional expression ) statement 1 [other statement 2 ]

Statement 1 is executed when the conditional expression is true. If the option else is specified, statement 2 is executed when the conditional expression is false.

Example

if (item1 == "1") {
    print ("item1 is 1 ") ;
} else {
    print ("item1 is not 1 ");
}

for statement

It is a for loop.
    for ( initialization expression ; end conditional expression ; increment expression ) statement

After executing the initialization expression, execute the statement while the termination conditional expression is true. Execute the increment expression before checking the next termination condition.

Executing a break statement within a statement immediately ends the loop. Also, executing a continue statement within a statement enters the next loop without executing subsequent statements.

Example

for (var n = 0; n <10; n ++) {
    print ("n is ", n, " ") ;
}
 

for (var n = 0; n <20; n ++) {
    if (n == 5) continue;
    if (n == 9) break;
    print ("n is ", n, " ") ;
}

for in statement

A loop that corresponds to elements such as arrays.
    for ( loop variable in target variable ) statement

Execute the statement while assigning a value to the loop variable to enumerate the contents of the target variable. The target variable can be an array object, an Array object, and some objects that support iterators. Executing a break statement within a statement immediately ends the loop. Also, executing a continue statement within a statement enters the next loop without executing subsequent statements.

Example

var a = new Array ("aa", "bb", "cc");
var n
for (n in a) {
    print (a [n], "\ n");
}

while statement

It is a conditional loop.
    while (conditional expression) statement

Execute the statement while the conditional expression is true. The conditional expression is evaluated before the statement is executed. Executing a break statement within a statement immediately ends the loop. Also, executing a continue statement within a statement enters the next loop without executing subsequent statements.

Example

var a = 0;
while (a <10) {
    print ("a is ", a, "\ n");
    a ++;
}

do… while statement

It is a conditional loop.
    do statement while ( conditional expression )

Execute the statement while the conditional expression is true. The conditional expression is evaluated after the statement is executed. Executing a break statement within a statement immediately ends the loop. Also, executing a continue statement within a statement enters the next loop without executing subsequent statements.

Example

var a = 0;
do {
    print ("a is ", a, "\ n");
    a ++;
} while (a <10);

switch case statement

Branch depending on the condition

switch ( evaluation formula ) {
case result 1 :
    Execution statement
case result 2 :
    Execution statement
 
    :
case result n:
    Execution statement
default:
    Execution statement
}

Evaluate the evaluation expression and jump to the case label that matches the result . If the default label is defined, it will jump if the evaluation expression does not match any other case label.

Executes the statement after the jumped label, but continues executing the statement that continues even when the next case label is reached. Place a break statement to interrupt the execution of the statement and jump to the end of the switch block .

Example

var data = 1;
switch (data) {
case 0:
    print ("data is 0 \ n") ;
case 1:
    print ("data is less than or equal to 1 \ n");
case 2:
    print ("data is less than or equal to 2 \ n");
case 3:
    print ("data is 3 or less \ n");
}

For this example, the result is:
    data is less than or equal to 1
    data is 2 or less
    data is 3 or less

Example of using break statement

var data = 1;
switch (data) {
case 0:
    print ("data is 0 \ n") ;
    break;
case 1:
    print ("data is 1 \ n") ;
    break;
case 2:
    print ("data is 2 \ n") ;
    break;
case 3:
    print ("data is 3 \ n") ;
    break;
default: default:
    print ("data is 3 \ n") ;
    break;
}

Execution result
    data is 1