Programmer Guide > Statement Types > CASE Statement
  

CASE Statement
CASE expression OF
	expression: statement
	...
	...
	expression: statement
	ELSE: statement
ENDCASE
The CASE statement is used to select one, and only one, statement for execution depending upon the value of the expression following the word CASE. This expression is called the case selector expression. Each statement that is part of a CASE statement is preceded by an expression which is compared to the value of the selector expression. If a match is found, the statement is executed and control resumes directly below the CASE statement.
The ELSE clause of the CASE statement is optional. If included, it must be the last clause in the CASE statement. The statement after the ELSE is executed only if none of the preceding statement expressions match. If the ELSE is not included and none of the values match, an error will occur and program execution will stop.
An example of the CASE statement is:
CASE NAME OF 
; Executed if NAME = 'LINDA'
'LINDA': PRINT, 'SISTER'
; Executed if NAME = 'JOHN'
'JOHN': PRINT, 'BROTHER'
'HARRY': PRINT, 'STEP-BROTHER'
; Executed if no matches.
ELSE: PRINT, 'NOT A SIBLING'
ENDCASE
Another example, below, shows the CASE statement with the number 1 as the selector expression of the CASE. 1 is equivalent to true and is matched against each of the conditionals.
CASE 1 OF 
(X GT 0) AND (X LE 50): Y = 12 * X + 5
(X GT 50) AND (X LE 100): Y = 13 * X + 4
(X LE 200): BEGIN
Y = 14 * X - 5
Z = X + Y
END
ELSE: PRINT, 'X has illegal va1ue'
ENDCASE
In the CASE statement, only one clause is selected, and that clause is the first one whose value is equal to the value of the case selector expression. Unlike some other languages, in PV‑WAVE execution does not 'fall through' to subsequent clauses.

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.