The scripting engine supports the following control structures: if, while, repeat and for.
An if statement evaluates its condition and then only executes the �then� part if the condition is true. An if statement is terminated by the �end� keyword. The basic syntax is:
if condition then
do something here
end
For example:
x = 50;
if x > 10 then
Dialog.Message("result", "x is
greater than 10");
end
y = 3;
if ((35 * y) < 100) then
Dialog.Message("", "y times 35
is less than 100");
end
In the above script, only the first dialog message would be shown, because the second if condition isn't true...35 times 3 is 105, and 105 is not less than 100.
You can also use else and elseif to add more �branches� to the if statement:
x = 5;
if x > 10 then
Dialog.Message("", "x is greater
than 10");
else
Dialog.Message("", "x is less than
or equal to 10");
end
In the preceding example, the second dialog message would be shown, because 5 is not greater than 10.
x = 5;
if x == 10 then
Dialog.Message("", "x is exactly 10");
elseif x == 11 then
Dialog.Message("", "x is exactly
11");
elseif x == 12 then
Dialog.Message("", "x is exactly
12");
else
Dialog.Message("", "x is not 10,
11 or 12");
end
In that example, the last dialog message would be shown, because x is not equal to 10, or 11, or 12.
The while statement is used to execute the same "block" of script over and over until a condition is met. Like if statements, while statements are terminated with the �end� keyword. The basic syntax is:
while condition do
do something here
end
The condition must be true in order for the actions inside the while statement (the �do something here� part above) to be performed. The while statement will continue to loop as long as this condition is true. Here's how it works:
If the condition is true, all of the actions between the �while� and the corresponding �end� will be performed. When the �end� is reached, the condition will be reevaluated, and if it's still true, the actions between the �while� and the �end� will be performed again. The actions will continue to loop like this until the condition evaluates to false.
For example:
a = 1;
while a < 10 do
a = a + 1;
end
In the preceding example, the �a = a + 1;� line would be performed 9 times.
You can break out of a while loop at any time using the �break� keyword. For example:
count = 1;
while count < 100 do
count = count + 1;
if count == 50 then
break;
end
end
Although the while statement is willing to count from 1 to 99, the if statement would cause this loop to terminate as soon as count reached 50.
The repeat statement is similar to the while statement, except that the condition is checked at the end of the structure instead of at the beginning. The basic syntax is:
repeat
do something here
until condition
For example:
i = 1;
repeat
i = i + 1;
until i > 10
This is similar to one of the while loops above, but this time, the loop is performed 10 times. The �i = i + 1;� part gets executed before the condition determines that a is now larger than 10.
You can break out of a repeat loop at any time using the �break� keyword. For example:
count = 1;
repeat
count = count + 1;
if count == 50 then
break;
end
until count > 100
Once again, this would exit from the loop as soon as count was equal to 50.
The for statement is used to repeat a block of script a specific number of times. The basic syntax is:
for variable = start,end,step
do
do something here
end
The variable can be named anything you want. It is used to �count� the number of trips through the for loop. It begins at the start value you specify, and then changes by the amount in step after each trip through the loop. In other words, the step gets added to the value in the variable after the lines between the for and end are performed. If the result is smaller than or equal to the end value, the loop continues from the beginning.
For example:
-- This loop counts from 1 to 10:
for x = 1, 10 do
Dialog.Message("Number", x);
end
The above example displays 10 dialog messages in a row, counting
from 1 to 10.
Note that the step is optional�if you don�t provide a value for the step,
it defaults to 1.
Here�s an example that uses a step of �-1� to make the for loop count backwards:
-- This loop counts from 10 down to 1:
for x = 10, 1, -1 do
Dialog.Message("Number", x);
end
That example would display 10 dialog messages in a row, counting back from 10 and going all the way down to 1.
You can break out of a for loop at any time using the �break� keyword. For example:
for i = 1, 100 do
if count == 50 then
break;
end
end
Once again, this would exit from the loop as soon as count was equal to 50.
There is also a variation on the for loop that operates on tables. For more information on that, see Using For to Enumerate Tables.