Control Structures – If…else if…else
If
The if construct is one of the most important features of many languages, ChaosPro’s own language included. It allows for conditional execution of code fragments. ChaosPro features an if structure that is similar to that of C:
if (expr) { statement }
As described in the section about expressions, expr is evaluated to its Boolean value. If expr evaluates to TRUE, ChaosPro will execute statement, and if it evaluates to FALSE – it’ll ignore it. If expr does not evaluate to a boolean value, then ChaosPro will display an error and won’t compile. ChaosPro won’t automatically convert the expression to a boolean. The reason for this is: speed! In order to perform certain optimizations, ChaosPro must have a boolean expression. Well, it could automatically add a “!=0″ to the expression, if it’s not boolean, just to make it boolean. But instead I think that the formula author should do that to write better code and to be aware that there’s a non boolean expression which should be boolean. Otherwise the formula writer would produce slow code not knowing why it is so slow…
ChaosPro always wants to have the curly brackets after the if statement, even if there’s only a single statement.
If-statements can be nested indefinitely within other if-statements, which provides you with complete flexibility for conditional execution of the various parts of your formulas.
Else
Often you’d want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. For example, the following code would set z to -1 if a is negativ, and set z to 1 if not (i.e. a>=0).
if (a<0) { z=-1; } else // in case a<0 is FALSE, i.e. a>=0 { z=1; }
The else statement is only executed if the if-expression evaluated to FALSE, and if there were any else-if expressions – only if they evaluated to FALSE as well (see else-if).
Else If
else if, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the else-if-conditional expression evaluates to TRUE. For example, the following code would set z to 1, if a is bigger than b, z to 0, if a is equal to b and z to -1 if a is smaller than b:
if (a > b) { z=1; } else if (a == b) // if a is not bigger than b, evaluate expression (a==b) to see whether it is true { z=0; } else // Ok, no conditional expression in the if-else if-statement evaluated to TRUE, so execute the following block. { z=-1; }
There may be several else-ifs within the same if statement. The first else-if expression (if any) that evaluates to TRUE would be executed.
The else-if-statement is only executed if the preceding if expression and any preceding else-if-expressions evaluated to FALSE, and the current else-if expression evaluated to TRUE.