Show/Hide Toolbars

XSharp

Purpose

Conditionally execute a block of statements.

Syntax

DO CASE
CASE <lCondition>
  <Statements>...
[CASE <lCondition>]
  <Statements>...
[OTHERWISE]
  <Statements>...
END[CASE]

Arguments

<lCondition>If this expression evaluates to TRUE, the statements following it up until the next CASE, OTHERWISE, or ENDCASE are executed. Afterwards, control branches to the statement following the next ENDCASE statement.
OTHERWISEIf all preceding CASE conditions evaluate to FALSE, the statements following the OTHERWISE up until the next ENDCASE are executed. Afterwards, control branches to the statement following the next ENDCASE statement.

Description

DO CASE works by branching to the statement following the first CASE <lCondition> that evaluates to TRUE. If all CASE conditions evaluate to FALSE, it branches to the statement following the OTHERWISE statement (if specified).

Execution proceeds until the next CASE, OTHERWISE, or ENDCASE is encountered, and control then branches to the first statement following the next ENDCASE statement.

Control structures can be nested to any depth. The only requirement is that each control structure be properly nested.

Note: DO CASE...ENDCASE is identical to IF...ELSEIF...ENDIF, with neither syntax having a performance advantage over the other.

Examples

This example uses DO CASE in a menu structure to branch control based on user selection:

FUNCTION ActonChoice(nChoice as LONG) AS VOID
DO CASE
CASE nChoice = 0
  RETURN
CASE nChoice = 1
  ChoiceOne()
CASE nChoice = 2
  ChoiceTwo()
ENDCASE

See Also

BEGIN SEQUENCE, DO WHILE, FOR, FOREACH IF SWITCH