Simplifying ELSE…IF

Properly structured IF statements can get quite deeply nested quite quickly if one is indenting program code for legibility.

IF A% = 1 THEN
 …
ELSE
 IF A% = 2 THEN
  …
 ELSE
  IF A% = 3 THEN
   …
  ELSE
   …
  END IF
 END IF
END IF

This dialect of BASIC provides a control structure from BBC BASIC to help with this.

CASE A% OF
 WHEN 1: …
 WHEN 2: …
 WHEN 3: …
 OTHERWISE: …
END CASE

This also helps with cases where the IF statements are not even using the same variable, let alone are doing simple comparisons.

IF LEN(ENV$("TERM")) = 0 THEN
 ERROR 0,"The TERM environment variable must be set."
ELSE
 IF LEN(ENV$("COLORTERM")) <> 0 THEN
  REM Not a kernel virtual terminal
 ELSE
  IF ENV$("TERM") = "linux" OR ENV$("TERM") = "teken" OR ENV$("TERM") = "netbsd6" THEN
   PRINT "This probably will have strange effects on a kernel virtual terminal."
  ELSE
   …
  END IF
 END IF
END IF

Because the controlling expression for the CASE does not have to be a variable, and the controlling expressions for the WHENs do not have to be simple constants.

CASE TRUE OF
 WHEN LEN(ENV$("TERM")) = 0
  ERROR 0,"The TERM environment variable must be set."
 WHEN LEN(ENV$("COLORTERM")) <> 0
  REM Not a kernel virtual terminal
 WHEN ENV$("TERM") = "linux" OR ENV$("TERM") = "teken" OR ENV$("TERM") = "netbsd6"
  PRINT "This probably will have strange effects on a kernel virtual terminal."
 OTHERWISE:
  …
END CASE