Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

Use the tabs on the upper right to switch to a different programming language.

switchswitchSwitchSwitchswitch (Operator)

Name

switchswitchSwitchSwitchswitch — Starts a multiway branch block.

Signature

switch( : : ControlExpression : )

Herror switch(const Hlong ControlExpression)

Herror T_switch(const Htuple ControlExpression)

void Switch(const HTuple& ControlExpression)

static void HOperatorSet.Switch(HTuple controlExpression)

def switch(control_expression: int) -> None

Description

switchswitchSwitchSwitchSwitchswitch starts a block that allows to control the program flow via a multiway branch. The parameter ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpressioncontrol_expression must result in an integer value. This value determines to what casecaseCaseCaseCasecase label the execution jumps. Every casecaseCaseCaseCasecase statement includes one integer constant. If the integer constant of a casecaseCaseCaseCasecase statement is equal to the calculated value of the parameter ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpressioncontrol_expression, the program execution continues there. In addition, an optional defaultdefaultDefaultDefaultDefaultdefault statement can be defined as the last jump label within a switchswitchSwitchSwitchSwitchswitch block. The program execution jumps to this defaultdefaultDefaultDefaultDefaultdefault label, if no casecaseCaseCaseCasecase constant matches the calculated ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpressioncontrol_expression value.

As in the programming languages C, C++, and C#, the casecaseCaseCaseCasecase statement is a jump label and---in contrast to elseifelseifElseifElseifElseifelseif---not the begin of an enclosed block that is automatically left at the next casecaseCaseCaseCasecase or defaultdefaultDefaultDefaultDefaultdefault statement.

In order to leave the switchswitchSwitchSwitchSwitchswitch block after the execution of the code lines of a casecaseCaseCaseCasecase branch, as in C or C++ a breakbreakBreakBreakBreakbreak statement must be inserted at the end of the casecaseCaseCaseCasecase branch. breakbreakBreakBreakBreakbreak statements can be used anywhere within a switchswitchSwitchSwitchSwitchswitch block. This causes the program execution to continue after the closing endswitchendswitchEndswitchEndswitchEndswitchendswitch statement. Without a breakbreakBreakBreakBreakbreak statement at the end of a branch the program execution “falls through” to the statements of the following casecaseCaseCaseCasecase or defaultdefaultDefaultDefaultDefaultdefault branch.

If the same statements have to be executed in different cases, i.e., for multiple control values, several casecaseCaseCaseCasecase statements with different constant expressions can be listed one below the other.

Parameters

ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpressioncontrol_expression (input_control)  integer HTupleintHTupleHtuple (integer) (int / long) (Hlong) (Hlong)

Integer expression that determines at which case label the program execution is continued.

Example (HDevelop)

TestStr := ''
for Index := 1 to 8 by 1
  TestStr := TestStr + '<'
  switch (Index)
  case 1:
    TestStr := TestStr + '1'
    break
  case 2:
    TestStr := TestStr + '2'
    * intentionally fall through to 3
  case 3:
    TestStr := TestStr + '3'
    * intentionally fall through to 4
  case 4:
    TestStr := TestStr + '4'
    break
  case 5:
  case 6:
    * common case branch for 5 and 5
    TestStr := TestStr + '56'
    break
  case 7:
    * continue for loop
    TestStr := TestStr + '7'
    continue
  default:
    TestStr := TestStr + 'd'
    break
  endswitch
  TestStr := TestStr + '>'
endfor

Result

If the condition is correct, switchswitchSwitchSwitchSwitchswitch (as an operator) returns 2 (H_MSG_TRUE). Otherwise, an exception is raised and an error code is returned.

Alternatives

ififIfIfIfif, elseifelseifElseifElseifElseifelseif, elseelseElseElseElseelse

See also

casecaseCaseCaseCasecase, defaultdefaultDefaultDefaultDefaultdefault, endswitchendswitchEndswitchEndswitchEndswitchendswitch, ififIfIfIfif

Module

Foundation