SWITCH
Executes one or more blocks of statements.
Syntax
SWITCH SwitchExp CASE LiteralExp1 ... [EXIT] [CASE LiteralExp2] ... [EXIT] [CASE LiteralExpn] ... [EXIT] OTHERWISE ... END [SWITCH]
Arguments
The <LiteralExp> must be a compiled time resolvable numeric or character expression, and may involve operators, as long as such operators involve compile time static value.
CASE <LiteralExp1> .. <LiteralExp> is a constant value of the same data type as <SwitchExp>.
The EXIT optional statement is the equivalent of the C statement break, and if present, execution of the SWITCH structure will end when the EXIT statement is reached, otherwise it will continue with the first statement below the next CASE statement (fall through)
Description
SWITCH is a Harbour construct inspired by the C implementation of switch().
The SWITCH statement compares a constant value against a series of constant values. It is similar to the DO CASE statement but outperforms it to a great extent due to the restrictions introduced with values permitted for comparison. As a general rule, only literal values in form integer or character constants may follow the CASE clauses.
The SWITCH statement evaluates <SwitchExp> and then searches for a first match between the resulting value and <LiteralExp>. When a match is found, the statements following the corresponding CASE clause are executed down to the END statement. To suppress execution of statements of the next CASE clause, the EXIT statement must be explicitely used. This is a major difference to the DO CASE statement where subsequent CASE clauses are skipped once a first match is found.
If no initial match is found with the CASE clauses, the statements following OTHERWISE are executed, if present.
Example
// The example demonstrates the SWITCH control structure // Try with and without EXIT statements PROC Main() LOCAL cSName := "Doe" SWITCH cSName CASE "Abba" ? "Abaneoer" EXIT CASE "Bo" ? "Boeing" EXIT CASE "Doe" ? "John" EXIT OTHERWISE ? "Guest" END ?? '', cSName ? WAIT "EOF SwitchTest.prg" RETU
Seealso
DO CASE, IF…ELSEIF…ENDIF
Pingback: Harbour Statements | Viva Clipper !