switch

Syntax

SWITCH ( expression ) {

 CASE expression: {

   code

   BREAK

 }

 ...

 DEFAULT: ...

}

 

Arguments

expression can contain any valid expression. If you provide a mathematical expression for a particular CASE, then place that expression between brackets (). If you explicitly want to provide a string for a particular CASE, then place single quotes around that string (for example when you don't want ADAPL to try to calculate (x+y), but want to have it treated as text.) If you provide the name of a variable without quotes around it, for a particular CASE, first the value in it is retrieved. In the end all expressions (calculated sums, retrieved values, and strings) are compared as strings.
In code you can place any ADAPL code you want, except other SWITCH statements (nesting can be achieved by putting the other SWITCH statements in separate subroutines, to which you can jump from the main SWITCH CASEs). You can separate commands and functions by a space, or by a semicolon and a space, or by the end of a line. A code line or block is always placed between curly brackets {}.

Meaning

A switch/case-construction makes it possible to program nested if/then/else-constructions more elegantly. Especially when a variable can assume many values and you want to execute a different piece of code for each possible value, then SWITCH is ideal.
SWITCH calculates the value of the first expression and converts it to a string. Subsequently the program jumps to the first CASE statement and compares the (string) value of that expression with the first. If they are equal, the code line or block following that first CASE is executed. If not, then the next CASE will be tested, and so on.

Just like the entire SWITCH statement, the code is being run through from top to bottom, including the DEFAULT, and the BREAK command ensures that the SWITCH structure is not being run through any further, and execution jumps to the line following the last SWITCH curly bracket. (BREAK is also needed in the last CASE block if you don't want the DEFAULT to be executed if this CASE is true.) If you don't use BREAK, every next CASE is being tested and/or run through until finally DEFAULT is being executed. (For DEFAULT you have to provide some code. If you don't really need any default code, just use DEFAULT: BREAK, to end the SWITCH construction.) The DEFAULT will always be executed if not a single CASE test is positive, and this is what DEFAULT is really meant for. The DEFAULT is placed below the CASE statements.

The opening curly bracket must always be placed on the same line as the SWITCH, CASE or DEFAULT statement. The closing curly bracket can be on a line of its own.

Note that in between the lines that hold the SWITCH and CASE statements, you cannot place an empty line or any comments: placing an empty line or comment line here will make it impossible to compile this adapl.

Example

switch (myvar) {
  case '2': { errorm 'myvar is'; errorm 'two'; break;}
  case (2 + 1): {
    errorm 'three'
    break
  }
  case 4: {errorm 'four' break}
  default: errorm 'myvar is greater than four'

}

Result

When myvar contains anything different from ‘2’, ‘3’ or ‘4’ than myvar is greater than four is being displayed.

When myvar contains ‘2’ than myvar is two is being displayed, and ADAPL leaves the switch construction.

When myvar contains ‘3’ than three is being displayed, and ADAPL leaves the switch construction.

When myvar contains ‘4’ than four is being displayed, and ADAPL leaves the switch construction.