[10.10] How can I simulate a switch/case statement in Lingo?

Instead of using huge rafts of nested if/then statements, many languages provide a structure that allows multiple branching based on the value of a variable (eg, "switch" in C or "case" in Pascal). Lingo, unfortunately, does not provide such a structure, but you can simulate it using lists.

Eg:

-- to switch between a series of actions depending on the value of 
-- our variable switchVar. This example assumes switchVar will have a 
-- numeric value, but you can use other values instead

  set actionsList = [ 0: "zeroAction", 1: "oneAction", 2: "twoAction",  ]
  do string(getAProp(actionsList, switchVar)) 

Note the outer "string" call. This is because, if switchVar contains a value that isn't one of the list's properties, getAProp returns . String converts this to "" (an empty string), which can be safely passed to the do command without causing an error.

An analogous structure can be used to do conditional assignments. Rather than use a bunch of separate checks:

  if x = 1 then set y = "one"
  else if x = 2 then set y = "two"
  else if x = 3 then set y = "three"
  else if x = 4 then set y = "four"

Combine them all into a single list assignment:

  set y = getAProp( [ 1: "one", 2: "two", 3: "three", 4: "four" ], x )

For more on the uses of lists, see section 12.