Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


4.1.3 SELECT block

The SELECT block has the following form:

  SELECT variable
  CASE expressionA
    statementsA
  CASE expressionB
    statementsB
  DEFAULT
    statementsC
  ENDSELECT

The value of the selection variable (denoted by variable in the SELECT part) is compared with the value of the expression in each of the CASE parts in turn. If there's a match, the statements in the (first) matching CASE part are executed. There can be any number of CASE parts between the SELECT and DEFAULT parts. If there is no match, the statements in the DEFAULT part are executed. There does not need to be a DEFAULT part but if one is present it must be the last part (immediately before the ENDSELECT).

It should be clear that SELECT blocks can be rewritten as IF blocks, with the checks on the IF and ELSEIF parts being equality checks on the selection variable. For example, the following code fragments are equivalent:

  SELECT x
  CASE 22
    WriteF('x is 22\n')
  CASE (y+z)/2
    WriteF('x is (y+x)/2\n')
  DEFAULT
    WriteF('x isn't anything significant\n')
  ENDSELECT

  IF x=22
    WriteF('x is 22\n')
  ELSEIF x=(y+z)/2
    WriteF('x is (y+x)/2\n')
  ELSE
    WriteF('x isn't anything significant\n')
  ENDIF

Notice that the IF and ELSEIF parts come from the CASE parts, the ELSE part comes from the DEFAULT part, and the order of the parts is preserved. The advantage of the SELECT block is that it's much easier to see that the value of x is being tested all the time, and also we don't have to keep writing `x=' in the checks.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.