Any Alambik script is built out of a series of statements.
A statement can be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing at all (an empty statement).
In addition, statements can be bundled into statement-groups by encapsulating several of them with curly braces.
A statement-group is considered to be a statement in its own right. All these various statement types will be described in this chapter.
One more thing: Operators (logical, mathematical, etc..) are statements too!
|The syntax of loops and conditions|
| The Alambik interpreter supports several different kinds
of conditional statements. In keeping with the general language philosophy,
such statements are implemented in the easiest and clearest way possible.
"If X equals 1 then otherwise "
would be scripted as:
"Increment X going from 1 to 30 ."
for (%x=1 to 30)
"Repeat until X equals 30 "
Here is the list of the decision and control instructions included with Alambik Script 4.0:
| Foundation of conditional logic, the IF construct
is one of the most important features in many languages, Alambik Script
included. It allows for the conditional execution of code fragments.
As described in the section on expressions, expr is evaluated in terms of its Boolean value. If expr evaluates to TRUE, Alambik will execute statement. If it evaluates to FALSE, Alambik will not execute statement.
The following example will execute a procedure if !a is bigger
Often you may want to have more than one statement executed conditionally. Of course, there's no need to wrap each individual statement with an if clause. Instead, you can group several statements together into a statement group.
For example, this code will execute a procedure and then add !b
to !a if !a is found to be bigger than !b.
IF statements can be nested recursively within other if statements, which gives you complete flexibility for the conditional execution of the various parts of your program.
IF statements can also handle two variables.
Note : The SWITCH CASE structure can prove useful for evaluating simple expressions having multiple potential outcomes.
Often you may want to execute a certain statement if a condition
is met, and a different statement if that condition is not met.
For example, the following code will execute procedure proc1()
if !a is bigger than !b, and proc2() otherwise.
if (!a > !b)
An ELSE statement will only be executed should the preceding IF expression evaluate to FALSE. Should there have been any preceding ELSEIF expressions in the same statement, it would be necessary that these evaluate to FALSE as well.
Note : The SWITCH
CASE structure can prove useful for
evaluating simple expressions having multiple potential outcomes.
|SWITCH () ... CASE()|
The SWITCH statement is similar to a series of IF statements all
operating on the same expression.
There is only one syntax for SWITCH case loops:
It's possible to execute several comparisons in a single case statement by separating the conditions with commas.
It is important to understand how the SWITCH statement is executed
in order to avoid mistakes.
Note: You can get out of a SWITCH
CASE structure at any
time by using the BREAK () instruction.
REPEAT UNTIL loops are very similar to DO WHILE loops. The first iteration of a REPEAT UNTIL loop is guaranteed to run (because the truth expression is only checked at the end of the iteration). REPEAT UNTIL structures can be overlapped.
There is only one syntax for REPEAT
Advanced C users may be familiar with a different usage of the REPEAT UNTIL loop, employed to allow the stopping of execution in the middle of code blocks. For this you can use the BREAK() instruction which can be included in any FOR () NEXT (), REPEAT () UNTIL (), or SWITCH () CASE () loop.
|FOR() ... NEXT()|
UNTIL loops work well when you don't know how many times
you will need to execute the statements in a loop.
The syntax is: FOR (counter = start TO end [Step increment])
There is only one syntax for FOR
Note: The arguments counter, start, end, and increment are all numeric. The increment argument can be either positive or negative. If increment is positive, start must be less than or equal to end or the statements in the loop will not execute. If increment is negative, start must be greater than or equal to end for the body of the loop to execute. If Step isn't set, then increment defaults to 1.
|DO() ... LOOP()|
A DO LOOP Construct repeatedly executes a group of statements.
There is only one syntax for DO() ... LOOP() loops:
Infinite loop without variables.
The RETURN () statement is used to quit a procedure or to "return"
a value from a function.
This kind of expression contains only numerical variables, tables
Note : To compare string variables, you can use the equals operator (==), but not the greater-than or lesser-than operators ">" or "<".