Purpose | The <switch> statement is used to compare an item against multiple criteria and to perform a set of statements based on the result of the comparisons. Statements are executed in every <case> clause in which the comparison yields true. Please note that the <switch> statement can have zero or more <case> clauses; however there must only be zero or one <default> clause. The <switch> statement differs from the <if> statement in that the <if> statement only executes one set of consequential statements after the comparison rather than the multiple consequential execution of the <switch> statement. | |
Format |
<switch> <item> {item to be compared} <item> <case> <value op="{comparison operator}" type="{comparison datatype}"> {comparison value} </value> {execution statements} </case> . . . <default> {execution statements} </default> </switch>
| |
{item to be compared} | an <eval> statement that evaluates to a stand alone variable, array or attribute or an object or member variable or array element or an attribute on an object. | |
{comparison operator} | the operator to be used in the comparison for the <case> clause | |
{comparison datatype} | the datatype to be used in the comparison for the <case> clause | |
{comparison value} | the value (or an expression yielding a value) to be used in the comparison for the <case> clause | |
{execution statements} | the statements to be executed if the comparison yielded true in the <value> clause of the <case> clause-- the statements in the <default> clause will only be executed if none of the <case;> clauses evaluated to true | |
Example #1 |
<switch> <item> <eval object="MyEmployee" member="EmployeeID" /> <item> <case> <value op="=" type="int"> 12345 </value> <xout>You must be Joe Schmo</xout> </case> <case> <value op="=" type="int"> 98765 </value> <xout>You must be John Q</xout> </case> <default> <xout>I have no idea who you are</xout> </default> </switch> If the EmployeeID member of the MyEmployee object yields 12345 then the first <case> clause evaluates to true and the string You must be Joe Schmo is sent to the output stream. If the EmployeeID member of the MyEmployee object yields 98765 then the second <case> clause yields true and the string You must be John Q is sent to the output stream. Lastly, if the EmployeeID member of the MyEmployee object yields neither 12345 nor 98765 then neither of the <case> clauses yield true and so the <default> clause executes, writing the string I have no idea who you are to the output stream. |
|
Example #2 |
<switch> <item> <eval object="MyPal" member="Age" /> <item> <case> <value op=">=" type="int"> 18 </value> <xout>\r\nAdult</xout> </case> <case> <value op=">=" type="int"> 35 </value> <xout>\r\nEligible to run for president</xout> </case> <case> <value op=">=" type="int"> 65 </value> <xout>\r\nSenior Citizen</xout> </case> <default> <xout>Sorry, you're not old enough yet</xout> </default> </switch>
If the Age member of the MyPal object yields 20 then the first <case> clause evaluates to true and the string Adult is sent to the output stream on a new line. If the Age member of the MyPal object yields 37 then the second <case> clause yields true and the string Eligible to run for president is sent to the output stream on a new line. If the Age member of the MyPal object yields 69 then the second <case> clause yields true and the string Senior Citizen is sent to the output stream on a new line. Lastly, if the Age member of the MyPal object yields any number less than 18 then none of the <case> clauses yield true and so the <default> clause executes, writing the string Sorry, you're not old enough yet to the output stream. The net effect is that for someone aged 69, the following lines would be written to the output stream: |