Selective structure
A selective structure allows you to make the execution of an instruction (or group of instructions enclosed in curly brackets { .... }) dependent of a specified condition. Only if the condition is fulfilled will the instruction be executed, otherwise the instruction is ignored and passed over.
int $height = 2;
int $depth;
if ($height > 2) {
$depth = $height;
polyCube -w 1 -h $height -d $depth;
}
if ($depth < 1) {
polySphere -r 1;
}
Explanation:
The if-else construct
The if-construct above does nothing but ignore the instructions when the condition is not fulfilled. The if-else construct allows you to specify an alternative instruction (or group of instructions) that will be executed only if the condition is not fulfilled.
int $height = 2;
int $depth;
if ($height > 2) {
$depth = $height;
polyCube -w 1 -h $height -d $depth;
} else {
polySphere -r 1;
}
Conditional operators
To check the equality of two values, you must use the double equality sign ==. The single equality sign = is the assignment operator.
Symbol | Meaning |
< | smaller than |
> | greater than |
<= | smaller than or equal to |
>= | greater than or equal to |
== | equal to |
!= | not equal to |
Logical operators
You can combine two or more conditions (each enclosed in parentheses) using the logical operators AND, OR and NOT.
Symbol | Name | Meaning |
&& | AND | both conditions must be fulfilled |
|| | OR | one or the other condition must be fulfilled |
! | NOT | the condition may not be fulfilled |
Example:
int $height = 2;
int $depth;
if (($height > 2) && !($depth < 1)) {
$depth = $height;
polyCube -w 1 -h $height -d $depth;
} else {
polySphere -r 1;
}