If / Else#
In all programming languages, comparisons form the basis for flow control. Conditional statements are used to perform different actions based on different conditions.
Conditional Statements#
MivaScript provides the following conditional statements:
- MvIF expression: execute some code only if a specified condition is true.
- MvIF…MvELSE statement: execute one block of code if the condition is true and another block if it is false.
- MvIF…MvELSEIF…MvELSE statement: select one of many blocks of code to be executed.
Attributes#
- EXPR: Required. Returns a value interpreted as a conditional expression, typically involving a comparison, string, or logic operator. The expression must evaluate to true for MivaScript to process the code that follows the
<MvIF>
tag. An<MvIF>
tag must have a closing</MvIF>
end tag.
Syntax#
<MvIF EXPR="{ condition }">
Execute the code here if this condition is true, otherwise skip it.
</MvIF>
Examples#
Basic If…Else#
<MvIF EXPR="{ s.tm_hour LT 12 }">
Good Morning.<br>
<MvELSE>
Good Afternoon.<br>
</MvIF>
In this example, the code executed branches based on the system clock’s current hour of the day, where LT
means Less Than. See Operators.
If…ElseIf…Else#
<MvIF EXPR="{ g.month LE 3 }">
First Quarter.<br>
<MvELSEIF EXPR="{ g.month LE 6 }">
Second Quarter.<br>
<MvELSEIF EXPR="{ g.month LE 9 }">
Third Quarter.<br>
<MvELSE>
Fourth Quarter.<br>
</MvIF>
Multiple conditions can be used to determine which of several blocks of code to execute.
Nested Conditionals#
You can nest <MvIF>
tags—enclose one inside another. Each <MvIF>
tag must have a corresponding </MvIF>
end-tag. Indenting nested tags is not required but makes your code more readable.
<MvIF EXPR="{ age GE 17 }">
This person may be eligible to drive a motor vehicle.<br>
<MvIF EXPR="{ age GT 80 }">
This person requires a re-examination.<br>
</MvIF>
The license fee is $40.75.<br>
</MvIF>