![]() |
9. Flow Control
Introduction
Most of the Miva Script code that you have seen so far has consisted of a series of tags that were executed in a straightforward, top-to-bottom manner. In fact, though, most programs have a more complex structure, or 'flow of control'. Program structures typically involve decision-making of different sort, and repetitions.
Decision-making
<MvIF>
Processes the code between the <MvIF> and </MvIF> tags if and only if condition is true. EXPR is required.
- <MvIF EXPR="{condition}">
- ...code...
- </MvIF>
- EXPR
The <MvIF> tag lets the program test a condition and then take an action (or no action) depending on whether the condition is true or false. Here is an example that you saw in the section on expressions:
- <MvIF EXPR="{age GE 17}">
- <p><b>This person is eligible to drive a motor vehicle.</b></p>
- </MvIF>
In this example, the <MvIF> tests the condition '{age GE 17}' (that is, "is the value of the variable age greater than or equal to 17?"). If the condition is true, the HTML tags and text between the <MvIF> and the corresponding </MvIF> end-tag are displayed by the browser. If the condition is not true, no action is taken.
Attribute
<MvIF> has one attribute, EXPR, whose value is interpreted as a conditional expression, typically one involving a comparison, string, or logical operator. The action--text, HTML, or Miva Script code that you want processed if the condition is true--must follow the <MvIF> tag. You can put as many lines as you need here. Finally, an <MvIF> tag must have a corresponding </MvIF> end tag; this comes at the end of the action.
Nesting
You can nest <MvIF> tags--that is, enclose one inside another. You must make sure that each <MvIF> tag has a corresponding </MvIF> end-tag.
- <MvIF EXPR="{age GE 17}">
- <p><b>This person may be eligible to drive a
- motor vehicle.</b></p>
- <MvIF EXPR="{age GT 80}">
- <p><b>This person requires a re-examination.</b></p>
- </MvIF>
- <p><b>The license fee is $40.75.</b></p>
- </MvIF>
In this example, if the value of age is 45, the code will be executed as follows:
- The first <MvIF> is evaluated.
- Since age is greater than or equal to 17, the program continues with the next line after the <MvIF>.
- This line is executed, and the text is displayed.
- The second <MvIF> is evaluated.
- Since age is not greater than 80, the next line after this <MvIF> is not executed. Instead, the program jumps to the next line after the </MvIF> end-tag that corresponds to the second <MvIF>.
- This line is executed, and the text is displayed.
This person may be eligible to drive a motor vehicle.
The license fee is $40.75.Either Or Decisions
<MvELSE>
With the simple <MvIF> described above, the program can choose whether to carry out an action. Often, though, there will be two possible actions that you might want the program to take, one if a condition is true, and another if the condition is false. In this situation, you would use the <MvELSE> tag.
Process the code between the <MvIF> and <MvELSE> tags if condition is true. Otherwise (that is, if condition is false) process the code between the <MvELSE> and </MvIF> tags. EXPR is required. <MvELSE> is an empty tag.
- <MvIF EXPR="{condition}">
- ...code...
- <MvELSE>
- ...code...
- </MvIF>
- Example:
- <MvIF EXPR="{age GE 17}">
- <p><b>This person is eligible to drive a motor vehicle.</b></p>
- <MvELSE>
- <p><b>This person is too young to drive a motor vehicle.</b></p>
- </MvIF>
In this example, if age is greater than or equal to 17, the first paragraph will be displayed. If age is less than 17, the second paragraph will be displayed.
<MvELSE> consists of a single tag, with no attributes. It divides the <MvIF> into two 'branches'--the code to execute if the condition is true, and the code to execute if the condition is false. If the condition is true, the code between the <MvIF> and the <MvELSE> is executed; otherwise, the code between the <MvELSE> and the </MvIF> is executed. The syntax for using <MvELSE> is:
- <MvIF EXPR="{expr}">
- ...actions for "true" case...
- <MvELSE>
- ...actions for "false" case...
- </MvIF>
Multiple Decisions
Sometimes the possibilities that a program has to deal with cannot be described in either/or terms--there may be three, four, or more possible situations, each requiring a different action. In Miva Script code this kind of decision making is represented with multiple, nested <MvIF>-<MvELSE>-</MvIF> tags.
- <MvIF expr="{age LT 17}">
<p>This person is too young to drive a motor vehicle.</p>
- <MvELSE>
- <MvIF expr="{age GE 17 AND age LE 114}">
- <p>This person is eligible to drive a motor vehicle.</p>
- <MvELSE>
- <p>This person is too old to drive a motor vehicle.</p>
- </MvIF>
- </MvIF>
In this example there are three cases that the program has to deal with:
age is between 17 and 114, inclusive
The first case is covered by the first 'branch' of the first <MvIF>. If age is not less than 17, the program jumps to the second (<MvELSE>) branch of the <MvIF>. The code in this branch is another <MvIF>, which covers the other two cases. If age is between 17 and 114, the first branch is executed, otherwise, the second branch of the second <MvIF> is executed.
You can nest <MvIF>-<MvELSE>-</MvIF> tags to as many levels as you like, as long as you match up the <MvIF> and </MvIF> tags properly.
Repetition
<MvWHILE>
Repeats the code between <MvWHILE> and </MvWHILE> until the condition expression, given by the required attribute EXPR, is false, or an <MvWHILESTOP> (optional) is executed. <MvWHILESTOP> is a tag.
- <MvWHILE EXPR="{expression}">
- ...code...
- ....<MvWHILESTOP>
- ...code...
- </MvWHILE>
- where:
- EXPR
Many programming tasks require a series of steps to be repeated several times. Sometimes the number of repetitions is known in advance; other times the repetitions continue until a particular condition is true. This activity is called a loop; in Miva Script, general-purpose loops are carried out with <MvWHILE>...</MvWHILE> tags. (Some other tags, such as <MvIMPORT> and <MvCALL>, also loop through their input.)
<MvWHILE> has one attribute, EXPR, whose value will be interpreted as a condition. The code (text, HTML, and Miva Script tags) between <MvWHILE> and </MvWHILE> will be repeated as long as the condition expr is true. You can put as many lines of code as you need here.
Repeating a Fixed Number of Times, <MvWHILE>
Here is an example that displays the first five positive 'square' integers:
- <MvASSIGN name="num" value="1">
- <MvWHILE EXPR="{num LE 5}">
- <MvEVAL EXPR="{num * num}"><br>
- <MvASSIGN name="num" value="{num + 1}">
- </MvWHILE>
1
4
9
16
25This code starts by assigning an initial value for the variable num. In this context num functions as a counter, a variable that is used to count how many times the loop repeats. Since we know exactly how many times to repeat, we can construct a condition to reflect that. In this case we use the condition '{num LE 5}', that is, the loop will repeat while num is less than or equal to 5.
The <MvEVAL> tag evaluates and displays the square of num (num multiplied by itself). Then, since we are counting loop repetitions, the value of num is increased by 1 using <MvASSIGN>. The program then jumps back to the <MvWHILE> tag and tests the condition again. As long as num is less than or equal to 5, the loop repeats. These steps are carried out 5 times; at that point the value of num is 6, so the next time the condition is tested, it is found to be false, and the loop is not repeated. The program then jumps to the next line after the </MvWHILE> tag.
This example uses a common programming technique--setting up a counter that increments each time the loop is repeated, and then testing that counter to decide whether the loop should keep on repeating. Common programming mistakes are forgetting to increment the counter inside the loop, and not setting it to the correct initial value before the loop starts. Note that in this case the value of the counter is used in other calculations inside the loop; this is not always the case.
Repeating a Non-fixed Number of Times
The 'counter' technique can be used if you know exactly how many times you want the loop to repeat. Often you do not know this--the number of repetitions depends on some condition that will not be achieved in a predictable (or easily predictable) number of steps. For example, it may depend on input from a user or from a data file.
In this case you have to set up your loop condition as described above, but now the condition depends on something other than a counter. You have to make sure that which ever variable(s) the loop condition tests are ones that are affected by the code inside the loop. A common programming mistake is to set up a loop condition that is always true because that condition is never affected by what goes on inside the loop.
Terminating a Loop
Processing can be halted inside an <MvWHILE> ...</MvWHILE> loop by using the <MvWHILESTOP> tag. This will stop execution of the code inside the loop. The program then jumps to the first line after the </MvWHILE>
- <MvASSIGN NAME="num" VALUE="0">
- <BLOCKQUOTE>
- <MvWHILE EXPR="{num LE 5}">
- <MvASSIGN NAME="num" VALUE="{num + 1}">
- This loop has executed
- <MvEVAL EXPR="{num}"> times.<BR>
- <MvIF EXPR="{num GE 3}">
- <MvWHILESTOP>
- </MvIF>
- </MvWHILE>
- <B>Finished</B>
- </BLOCKQUOTE>
Since the condition governing this loop is '{num LE 5}' you might expect the loop to be repeated five times. In fact, though, the loop repeats only three times because when the condition '{num GE 3}' is met, the <MvWHILESTOP> is executed, and the loop terminates.
| Join Our Mailing List | Privacy Policy | Store Policies | Contact Us |
| © Copyright 2008 Miva Merchant. All Rights Reserved. |