IF, ELSIF and ELSE ​
Execute code branches based on evaluation of a condition.
Syntax ​
Checking just one condition.
IF Condition {
# Body with commands ...
} .
# Note: the curly brackets are needed (terminal symbols).
Checking one condition and providing an alternative if the condition is false.
IF Condition {
# Body with commands ...
} ELSE {
# Body with commands ...
} .
# Note: the curly brackets are needed (terminal symbols).
Checking multiple conditions and providing an alternative if all of the conditions are false.
IF Condition {
# Body with commands ...
} ELSIF Condition {
# Body with commands ...
} ELSE {
# Body with commands ...
} .
# Note: the curly brackets are needed (terminal symbols).
Parameters ​
Condition
(boolValue) - An expression that is evaluated and whose resulting value is interpreted to be either true or false. This is the<main-parameter>
of the corresponding command.
Details ​
These commands allows to control whether specific code is executed or not based on a condition. The condition is specified via the <main-parameter>
and should be an expression. If it evaluates to true, then the commands in the corresponding body (between the curly brackets {}
) are executed.
In total there are three different types of commands that rely on each other: IF
, ELSIF
and ELSE
. The order in which they are specified is relevant:
- The first must always be an
IF
command. - Then zero or more
ELSIF
commands can be used. - At the end one
END
command can be used.
The IF
and ELSIF
commands are evaluated in the order in which they are specified, so the body of the first of them whose condition evaluates to true is executed while all others are ignored. The body of the ELSE
command is executed only when all of the conditions evaluate to false.
Such IF
, ELSIF
and ELSE
structures may be nested in as many hierarchical levels as required.
These commands can be used in combination with the commands that set variables, like AVAL or BITMAPINFO, to realize different graphical representations for different objects of the same class.
See Also ​
Examples ​
For an object, draw a yellow circle and if the "Type" attribute is set to "global"
then draw another smaller yellow circle inside.
GRAPHREP
# Note: SHADOW is on by default.
AVAL sType:"Type"
FILL color:"yellow"
ELLIPSE rx:0.7cm ry:0.7cm
IF (sType = "global") {
ELLIPSE rx:0.45cm ry:0.45cm
}
For an object, draw a light green rectangle if its "Type" is "global"
or a light green circle otherwise. If the "Priority" attribute is also set to true then draw the same shape smaller inside of the object in a darker shade.
GRAPHREP
# Note: SHADOW is on by default.
AVAL sType:"Type"
AVAL as-original-type sPriority:"Priority"
FILL color:"palegreen"
IF (sType = "global") {
RECTANGLE x:-0.7cm y:-0.7cm w:1.4cm h:1.4cm
IF (sPriority) {
FILL color:"limegreen"
RECTANGLE x:-0.45cm y:-0.45cm w:0.9cm h:0.9cm
}
} ELSE {
ELLIPSE rx:0.7cm ry:0.7cm
IF (sPriority) {
FILL color:"limegreen"
ELLIPSE rx:0.45cm ry:0.45cm
}
}
For an object, draw a rectangle and fill it in different shades of blue based on the object's width. Also write the object's width in the middle.
GRAPHREP width-sizing:asymmetrical
SHADOW off
TABLE w:2.5cm h:0.6cm cols:1 rows:1
w1:100%
h1:100%
STRETCH off
IF (tabw1 < 2cm) {
FILL color:"lightcyan"
} ELSIF (tabw1 < 3cm) {
FILL color:"paleturquoise"
} ELSIF (tabw1 < 4cm) {
FILL color:"skyblue"
} ELSE {
FILL color:"steelblue"
}
RECTANGLE x:(tabx0) y:(taby0) w:(tabw1) h:(tabh1)
TEXT (tabw1) x:(tabx0 + (tabw1/2)) y:(taby0 + (tabh1/2)) w:c h:c
Versions and Changes ​
Available since ADOxx 1.3