You are here

Conditional expressions

Sometimes, the usual mathematical expressions aren’t enough. For example, although it’s easy to say that one variable is proportional to another, by multiplying it by a constant, it’s much harder to say that the value of a variable depends on whether another variable is above or below a threshold value. Simile’s equation language provides if...then statements as a way of handling these either / or situations. Using these conditional statements, alternative expressions are used depending on the result of evaluating a condition. For example, the overflow of water from a tank will be high if the tank is full, and zero otherwise.

Conditional statements are made up of several parts, some of which (the middle elseif parts) are optional. The general form is:

if C1 then E1 elseif C2 then E2 ..... else En

where:

  • C1, C2,... are some conditions
  • E1 is the expression used if the first condition is true
  • E2 is the expression used if the second condition is true and the first is not
  • En is the expression used if all the conditions are false

For example, if the maximum volume of the tank is stored in a variable called “Maximum”, and the amount of water in the tank is stored in a variable called “Amount” then the following expression could be used to calculate the overflow rate: “if Amount>=Maximum then 100 else 0”. In this case “Amount>=Maximum” is the condition, and 100 is the expression when the condition is true (the tank is full) and 0 is the expression when the condition is false (the tank is less than full).

Notes:

  • The elseif part is optional, and you can have as many of them as you like.
  • You must have the final else part.
  • Each condition has three parts: any expression, a comparison operator (<, >, =), and any expression.