Statements

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. For example: "If X equals 1 then … otherwise …" would be scripted as: if (%x==1) endif () "Increment X going from 1 to 30 …." for (%x=1 to 30) next () "Repeat until X equals 30 …" Repeat() Until (%x == 30) Here is the list of the decision and control instructions included with Alambik Script 4.0: DECISION

 IF() 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. if (expr) statement endif() 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 than !b: if (!a > !b)     procedure_name() endif() 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 (!a > !b)     procedure_name()     !b = !a + !b endif() 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. if (!a > !b) and (!a == !c)     procedure_name() endif() Note : The SWITCH…CASE structure can prove useful for evaluating simple expressions having multiple potential outcomes.

 ELSE() Often you may want to execute a certain statement if a condition is met, and a different statement if that condition is not met. This is what ELSE is for. ELSE tells an IF statement to execute an alternative piece of code in case the IF statement's expression evaluates to FALSE. For example, the following code will execute procedure proc1() if !a is bigger than !b, and proc2() otherwise. if (!a > !b)     proc(1) else()     proc(2) endif() 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. In many occasions, you may want to compare a single variable (or expression) with many different values, and then execute a different piece of code depending on which value it equals. This is exactly what the SWITCH statement is for. There is only one syntax for SWITCH case loops: switch (%var) case (==1)     proc1() case (==2)     proc2() case (==3)     proc3() case (==4)     proc4() caseelse()     proc(5) endswitch () It's possible to execute several comparisons in a single case statement by separating the conditions with commas. Example: switch (%var) case (>2,<8)     proc1() case (2,5,8)     proc2() endswitch () It is important to understand how the SWITCH statement is executed in order to avoid mistakes. The SWITCH statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only once a case statement is found with a value matching that of the SWITCH expression will Alambik begin to execute the specified statements. Caseelse() is the default choice when none of the cases is TRUE. CASE ELSE() must always be placed at the end just before ENDSWITCH; otherwise, any following cases will be ignored. Note: You can get out of a SWITCH…CASE structure at any time by using the BREAK () instruction.

 REPEAT() ... UNTIL() 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…UNTIL loops: repeat ()     proc1() until (keyboard.get ()==KEY_ESCAPE) 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() REPEAT…UNTIL loops work well when you don't know how many times you will need to execute the statements in a loop. When you do know you must execute your statements a specific number of times, however, a FOR…NEXT loop is often a better choice. Unlike the REPEAT…UNTIL loop, a FOR…NEXT loop uses a variable called a counter that increases or decreases in value during each repetition of the loop. You can overlap FOR...NEXT loops by placing one loop inside of another. You must, however, use distinct counter variable names for each loop. The syntax is: FOR (counter = start TO end [Step increment]) There is only one syntax for FOR…NEXT loops: %nb = 16 for (%k=1 to %nb step 2)     proc1() next () 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: do ()     text.display("Counting to 10 : "+str(%i),0,%i*10)     %i+=1 loop (10) Infinite loop without variables. Example : do () ... loop ()

 Return () The RETURN () statement is used to quit a procedure or to "return" a value from a function.

 Operators This kind of expression contains only numerical variables, tables or constants. Combining these in different ways results in a numerical value. In the case of text variables (strings), only == (equal), != (not equal), AND and OR can be used.

Arithmetic Operators

 Example Name Results !a + !b Addition Sum of !a and !b !a - !b Substraction Difference between !a and !b !a * !b Multiplication Product of !a and !b !a / !b Division Quotient of !a and !b !a mod !b Modulus Remainder of !a divided by !b

Assignment Operators

 Example Name Results !a = !b Assignment Assigns value of !a to !b Assignment can be used with any arithmetic or bitwise operator.

Bitwise Operators

 Example Name Results !a & !b Logical bitwise AND Bits that are set in both !a and !b are set. !a ^ !b Exclusive bitwise OR Bits that are set in either !a or !b (but not both) are set. !a | !b Logical bitwise OR Bits that are set in either !a or !b are set.

Comparison Operators

 Example Name Results !a == !b Equal TRUE if !a is equal to !b !a != !b Not Equal TRUE if !a is not equal to !b !a < !b Less than TRUE if !a is less than !b !a > !b Greater than TRUE if !a is greater then !b !a <= !b Less than or equal TRUE if !a is less than or equal to !b !a >= !b Greater than or equal TRUE if !a is greater then or equal to !b (%x==5) AND (%y==6) Logical AND TRUE if both !a and !b are TRUE (%x==5) OR (%y==6) Logical OR TRUE if either !a or !b is TRUE

Note : To compare string variables, you can use the equals operator (==), but not the greater-than or lesser-than operators ">" or "<".