Skip to content

switchπŸ”—

Only available for HDevelop

Short descriptionπŸ”—

switch β€” Starts a multiway branch block.

SignatureπŸ”—

switch( : : ControlExpression : )

DescriptionπŸ”—

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

As in the programming languages C, C++, and C#, the case statement is a jump label andβ€”in contrast to elseifβ€”not the begin of an enclosed block that is automatically left at the next case or default statement.

In order to leave the switch block after the execution of the code lines of a case branch, as in C or C++ a break statement must be inserted at the end of the case branch. break statements can be used anywhere within a switch block. This causes the program execution to continue after the closing endswitch statement. Without a break statement at the end of a branch the program execution β€œfalls through” to the statements of the following case or default branch.

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

ParametersπŸ”—

ControlExpression (input_control) integer β†’ (integer)

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

Combinations with other operatorsπŸ”—

Combinations

Alternatives

if, elseif, else

See also

case, default, endswitch, if

ModuleπŸ”—

Foundation