CASE
CASE index variable OF
VALUE case number 1,……:
program instructions
VALUE case number 2,……:
program instructions
:
VALUE case number n,……:
program instructions
ANY :
program instructions
END
Function
Executes the program according to a particular case number.
Parameter
Index variable
Real value variable or expression. Decides which CASE structure to execute according to the
value of this variable.
Program instructions
Executes these program instructions when the value of the index variable equals one of the values
after the VALUE statement.
Explanation
This structure enables the program to select from among several groups of instructions and to
process the selected group. This is a powerful tool in AS language that provides a convenient
method for allowing several alternatives within the program.
The execution procedure is as follows:
- Checks the value of the index variable entered after the CASE statement.
- Checks through the VALUE steps and finds the first step that includes the value equal to the
value of the index variable. - Executes the instructions after that VALUE step.
- Goes on to the instructions after the END statement.
If there is no value that matches the index variable, the program instructions after the ANY
statement are executed. If there is not an ANY statement, none of the steps in the CASE
structure is executed
[NOTE]
ANY statement and its program instructions can be omitted
ANY statement can be used only once in the structure. The statement must be at the
end of the structure as shown in the example below.
The colon “:” after the ANY statement can be omitted. When entering the colon,
always leave a space after ANY. Without a space, ANY: is taken as a label.
Both the ANY and END statements must be entered on their own line
Example
In the program below, if the value of real variable x is negative, the program execution stops after
the message is displayed. If the value is positive, the program is processed according to these 3
cases:
- if the value is an even number between 0 and 10
- if the value is an odd number between 1 and 9
- if the value is a positive number other than the above.
IF x<0 GOTO 10
CASE x OF
VALUE 0,2,4,6,8,10:
PRINT "The number x is EVEN"
VALUE 1,3,5,7,9:
PRINT "The number x is ODD”
ANY :
PRINT "The number x is larger than 10"
END
STOP
10 PRINT "Stopping because of negative value"
STOP