CASE

Top  Previous  Next

CASE statements are used for conditional execution of code in VPL programs.

 

CASE <expression> OF
<constant 1>,<constant 2>,<constant 3>:
  <statements>
<constant 1>..<constant 2>:
  <statements>
<constant 1>..<constant 2>,<constant 3>:
  <statements>
ELSE
  <statements>
END_CASE;

 

<expr> is an expression that evaluates to a number. If one of the <num> values has the same value as <expr>, that section of code will be executed.

If none of the <num> values match the <expr> value, the statements after the ELSE word are executed, and if no ELSE statement is found, the code after the END_CASE word will be executed.

The <expression> is limited to variables of SINT and INT types.

 

 

Example:

INCLUDE rtcu.inc
 
VAR
  a   : INT;
  str : STRING;
END_VAR;
 
PROGRAM test;
 
BEGIN
 
  CASE a OF
    -1   : str := "a is minus one";
    1     : str := "a is one";
    2..6 : str := "a is between two and six";
    7,9 : str := "a is either seven or nine";
  ELSE
     str := "a is something else...";
  END_CASE;
 
END;
 
END_PROGRAM;