Eyeon:Script/Reference/Language/Statements
From VFXPedia
Contents |
Getting Started : Statements : for
- Note
The for statement is a looping structure used to execute the same code 'x' number of times. There are two forms, a Numerical For used to loop 'x' times, and a Table For used to loop through all entries in a table.
Numerical For Structure
for counter = initial, limit, increment do :block break end
Remarks
The for statement is perhaps one of the most commonly used structures in eyeonScript. It provides a quick and easy to employ method repeating code a specified number of times. If the numerical structure is used the for statement will execute from the value of 'initial' incrementing the value of counter by 'step' until the value of 'limit' is reached. If the optional 'step' value is not provided the counter is incremented by +1 for each iteration.
The following example uses the for statement in its most basic fashion, counting from 0 to 9.
for countme = 0, 9 do print(countme) end print("Done Counting")
The following example is exactly like the first, expect that it increases the value of the counter by two for each iteration of the loop.
for countme = 0, 9, 2 do print(countme) end print("Done Counting")
To exit a loop before the counter reaches its limit use the break statement, which will exit the loop immediately, setting the next line of execution to the line immediately following the end statement. A break ends the innermost enclosing loop.
for countme = 0, 9 do if countme == 7 then print("exiting early") break end print(countme) end print("Done Counting")
Table For Structure
for index, value in pairs(table) do
:block break
end
Remarks
This structure iterates through all elements in a table, providing index and value as local variables for use by the inner block. The loop exits when all elements in the table have been processed, or when the break statement is encountered.
x = {"1", "2", greeting = "Hello", filename = "C:\\test.eyeonscript"} for i, v in pairs(x) do print(i, v) end
Getting Started : Statements : if
Structure
if expression then :block elseif expression then :block else :block end
Remarks
The if statement evaluates an expression, and executes a specific chunk of code if the expression evaluates to true. The statement may contain any number of elseif clauses, which contain additional expressions to evaluate in the case that the first expression evaluates false. If all of the expressions specified evaluate to false then the block specified under else is executed.
The simplest possible example of an if statement would look like this.
a = "Hello" if a == "Hello" then print("Welcome") end
The above example uses no else clauses, either the expression evaluates to true, or none of the code between the then and end clauses will execute.
a = "Goodbye" if a == "Hello" then print("Welcome") else print("See Ya!") end
In the above
example, the variable a is not set to Hello, so our expression evaluates as
false, and only the code after the else clause is executed.
The following example checks the time, and if outputs a response based on the time of day.
-- get the hour, using the 24 hour format a = date("%H") if a < 12 then print("Good Morning") elseif a > 17 then print("Good Afternoon") else print("Good Evening") end
Only one of the above print statements can execute. Once one of the expressions evaluates to true, the chunk of code it contains is executed, and execution skips to the next line after the end clause.
Getting Started : Statements : repeat, until
Structure
repeat
:block
until expression
Remarks
The repeat statement will repeatedly loop through the statements in block as long as the expression in the until clause evaluates to false. When the expression evaluates to true, the loop will exit and the next line of the script will execute.
countme = 1 repeat print(countme) countme = countme +1 until countme == 10 print("done counting")
To exit a loop before the expression evaluates to true use the break statement, which will exit the loop immediately, setting the next line of execution to the line immediately following the until statement. A break ends the innermost enclosing loop.
countme = 1 repeat print(countme) countme = countme +1 if countme == 7 then print("exiting early") break end until countme == 10 print("done counting")
Getting Started : Statements : while
Structure
while expression do
:block break
end
Remarks
You can use while statements to loop through a block of statements an indefinite number of times. The statements are repeated while the expression evaluates to true. For example
countme = 1 print("counting to 10") while countme < 11 do print(countme) -- wait 1 second wait(1) countme = countme + 1 end print("done counting")
To exit a loop before the expression evaluates to false use the break statement, which will exit the loop immediately, setting the next line of execution to the line immediately following the end statement. A break ends the innermost enclosing loop.
countme = 1 print("counting to 10") while countme < 11 do if countme == 7 then print("exiting early") break end print(countme) -- wait 1 second wait(1) countme = countme + 1 end print("done counting")