ClassesClasses | | Operators

Page not available for the currently selected syntax (programming language).

Page not available for the currently selected syntax (programming language).

Page not available for the currently selected syntax (programming language).

switchswitchSwitchSwitch (Operator)

Name

switchswitchSwitchSwitch — 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)

Description

switchswitchSwitchSwitchSwitch starts a block that allows to control the program flow via a multiway branch. The parameter ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpression 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 ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpression, 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 ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpression 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

ControlExpressionControlExpressionControlExpressionControlExpressioncontrolExpression (input_control)  integer HTupleHTupleHtuple (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


ClassesClasses | | Operators