![]() UJML Language Reference |
Flow control in scripting determines which script statements are executed, and when.
Within a script or execute element, all scripting statements are executed one at a time in the order they occur in the UJML source. See Script Blocks, XML Scripts. Obviously, there are times when you want to skip some statements (conditional execution) or execute some statements over and over (looping). UJML provides flow control scripting statements which do exactly these things.
For XML scripting, this capability is provided with script elements for conditional execution and looping. For the scripting language, there are keywords with the same purposes. See Keywords. For both forms of scripting, calling a function or causing a state transition will cause UJML execution to branch and return. See Execution Order.
Sometimes you need to execute one or more statements only when a variable has a certain value. This is called conditional execution. The scripting language provides conditional execution using the if and else keywords. See Keywords. XML scripting provides conditional execution with the if and else scripting elements.
'If' conditionals
If statements have a Boolean condition which must evaluate to true or false. If true, then the code block immediately following the if is executed. If the condition is false, and an accompanying else condition is provided, the code block immediately following the else is executed. In either case you can nest other if/else combinations inside the conditional code blocks.
'Else If' conditionals
When the code block following the else statement is another if statement, that if statement is joined to the else. This allows you to create conditional execution combinations saying, in effect "If this condition is true, then do these things. Otherwise, if this other condition is true then do these other things." if/else if/else combinations may be continued for as many different conditions as you wish to test exclusively from the others. In this case when a condition does return true, the code block for that if statement is executed and none of the following conditional statements in that combination are tested.
Sometimes you need to execute one or more statements multiple times, perhaps with different variable values each time. Certainly you could write out the same statements over and over, but it would be better if you could just specify them once and have the language do the work. This is called looping, because the language statements are executed in a loop over and over until some condition is met. The scripting language provides looping using the for and while keywords. See Keywords. XML scripting provides looping with the while scripting element.
While loops
While loops are very simple; you specify a Boolean expression (the condition) which is executed each time the loop is executed. The statements in the following code block are executed over and over until the conditional expression evaluates to false. (In other words, the code specifies that some statements should continue to execute while a condition is true). To avoid 'infinite loops' in which UJML repeats the statements in the while loop over and over without stopping, you must make certain that the statements in the loop will cause the specified condition to evaluate to false at some point. When the condition is false, the first time the while loop is encountered the statements in the following code block are never executed.
For loops
For loops are slightly more complicated than while loops. Rather than one conditional expression you can specify up to three expressions: The first expression is a scripting statement that is executed once, before the loop is entered; it is usually an assignment statement that sets the initial value of a variable. The second expression is a Boolean expression (the condition) which is evaluated each time the loop is executed, much like the condition used for the while loop. The third expression is a scripting statement that is also executed every time through the loop; it is usually used to increment or decrement the variable for which the initial value was set in the first expression.
Any of the three for expressions may be omitted. If the second expression (the conditional) is omitted, then UJML returns an implicit value of true, resulting in an infinite loop.
Break and continue statements
You may sometimes need to terminate a loop early or terminate a particular iteration of a loop. UJML provides break and continue statements to enable this.
The break statement causes the innermost loop to terminate immediately, with control passing out of the loop to the next statement after the loop. Only one loop is terminated; if a loop is nested inside another loop, control passes to the parent loop.
The continue statement causes the innermost loop to terminate its current iteration and continue on to the next iteration. In the case of a for loop, the next iteration includes executing the increment expression.
When a function is called, UJML branches to the function and executes any code there before branching back and executing the statement following the statement which called the function. See Using Functions, User Defined Functions, Execution Order.
Yet another form of flow control in UJML is provided implicitly by state transitions. When you cause a state transition to occur by changing the value of a state variable, then UJML branches to the state transition and executes any code there before branching back and executing the statement following the statement which caused the state transition. See State Variables, State Transitions, Execution Order.
The following example calculates a new position of a box one movement unit to the right. It then uses a simple conditional expression to determine if the box has moved off of the screen and relocates it if it has. It is part of the helloworld4.ujml sample.
mPosX = mPosX + &MOVEMENT;;
if (mPosX &_GT;
(_getIntProperty(&_PROPERTY_INT_SCREEN_WIDTH;) - mWidth))
{
mPosX = _getIntProperty(&_PROPERTY_INT_SCREEN_WIDTH;) - mWidth;
}
The following example determines the colors used for a fuel gauge display based on the amount of fuel left. It is part of the lunarlander.ujml sample.
if (mFuelPct &_GT; 66)
{
mHUDFuelGaugeColor = &_COLOR_LIME;;
mHUDFuelPctColor = &_COLOR_BLACK;;
}
else if (mFuelPct &_GT; 33)
{
mHUDFuelGaugeColor = &_COLOR_YELLOW;;
mHUDFuelPctColor = &_COLOR_BLACK;;
}
else if (mFuelPct &_GT; 0)
{
mHUDFuelGaugeColor = &_COLOR_RED;;
mHUDFuelPctColor = &_COLOR_WHITE;;
}
else
{
mHUDFuelGaugeColor = &_COLOR_WHITE;;
mHUDFuelPctColor = &_COLOR_WHITE;;
}
The following example uses nested conditional statements to change an animation sprite's state based on the current value of several variables. It is part of the lunarlander.ujml sample.
if (mLanded)
{
_clear_state(sLander);
// Did we crash?
if (mCrashed)
{
sLander = &LANDER_CRASHED;;
}
else
{
sLander = &LANDER_STD;;
}
sInput = false;
sTick = false;
}
else
{
_clear_state(sLander);
// Are we showing the rocket flames?
if (mThrustFrame &_GTE; 0)
{
sLander = mThrustDirection + mThrustFrame;
mThrustFrame = mThrustFrame + 1;
if (mThrustFrame &_GT; 1)
{
mThrustFrame = -1;
}
}
else
{
sLander = &LANDER_STD;;
}
sTick = false;
sTick = true;
}
The following example uses two nested 'while' loops to display a two-dimensional grid of dots. It is part of the colors.ujml sample.
mGridCtrX = 0;
while (mGridCtrX &_LT; mGridCellsWide - 1)
{
mGridCtrY = 0;
while (mGridCtrY &_LT; mGridCellsHigh - 1)
{
sGrid[mGridCtrY][mGridCtrX] = true;
mGridCtrY = mGridCtrY + 1;
}
mGridCtrX = mGridCtrX + 1;
}
The following example uses a 'for' loop to perform a linear search through a string for a specific Unicode character code point. A conditional statement inside the loop checks for the code point and, if found, causes the loop to exit early. It is part of the strings.ujml sample.
// Assume failure.
posFound = -1;
targetLength = _strlen(target);
// Linear search for character codepoint.
for (pos = 0; pos &_LT; targetLength; pos++)
{
if (char == _getCharacterCode(target, pos))
{
posFound = pos;
pos = targetLength;
}
}|
Copyright (c) 2000-2007 UIEvolution, Inc. All rights reserved.
|
|
What do you think about this topic? Send feedback!
|