2 Instruction Descriptions
2-30
NJ-series Instructions Reference Manual (W502)
• You can use a range of consecutive values. Place two periods between the numbers to indicate con-
secutive values. The following example executes statement 1 if the value of the integer expression is
between 10 and 15, inclusive.
CASE integer expression 1 OF
10..15:
statement 1;
16:
statement 2;
17:
statement 3;
ELSE
statement m;
END_CASE;
• You can omit ELSE. If you do, none of the statements is executed if none of the values is equal to the
value of the integer expression.
• There are no restrictions on the statements that you can use. You can use the same types of state-
ments for the statements in the CASE construct as you do for the statements outside the CASE con-
struct. For example, you can use function block calls and FOR constructs.
• The following is different in comparison to a C language switch statement. With a C language switch
statement, all statements after a value that equals the integer expression are executed unless a
break statement is used. With the CASE statement, only the statements that correspond directly to
the value that equals the integer expression are executed. For example, in the following example,
statements 1 to 3 are executed for the C language switch statement. Here, only statement 1 is exe-
cuted for the CASE instruction.
• You must always use CASE and END_CASE. They must be paired.
• The data types of the integer expression and values can be different.
• Each value can be given only once.
• You can use a hierarchy that is 15 levels deep, but count all levels of IF, CASE, FOR, WHILE, and
REPEAT constructs.
Precautions for Correct Use
val=1;
switch val
{
case 1:
statement 1;
case 2:
statement 2;
case 3:
statement 3;
}
val:=1;
CASE val OF
1:
statement 1;
2:
statement 2;
3:
statement 3;
END_CASE;
C Language switch Statement
CASE Instruction