Operator Reference

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)

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

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

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

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

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

Parameters

ControlExpressionControlExpressionControlExpressioncontrolExpressioncontrol_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, switchswitchSwitchSwitchswitch (as an operator) returns 2 ( H_MSG_TRUE) . Otherwise, an exception is raised and an error code is returned.

Alternatives

ififIfIfif, elseifelseifElseifElseifelseif, elseelseElseElseelse

See also

casecaseCaseCasecase, defaultdefaultDefaultDefaultdefault, endswitchendswitchEndswitchEndswitchendswitch, ififIfIfif

Module

Foundation