The if .. then .. else control structure
A conditional statement has the form:if condition then action else alternate-action
Scriptol writes the "then" word, either "let" when the action is a single statement or ":" when it is a block of statements. If the block of statements begins on the following line, the end of line terminates the condition, and the colon is optional.
Example of a complete if structure: | if
name = "pussycat" print "this is a cat" print "it seems hurted" else print "it is another animal" /if |
The "else" part is optional. Thus, a "if" structure is a condition, followed by one or several statements (that may be other if structures) and ended by "/if".
The "/" symbol is a shortcut for "end", and is used to terminate any control structure. "/if" means for "end of if structure".
The condition may be a comparison as above, or any kind of test. The "in" test already viewed, a boolean test, or anything that evaluates to true or false.
The compiler knows the first line of the control structure is terminated as it encounters the end of line.
If you want to put a statement on this first line, a colon is required to terminate the heading of the "if" structure:
You can put also the /if on the same line, but in this case the compiler needs for a semicolon to terminate the statement:
Example: if a = b : print t; /if
These conventions are the same for any other control structure.
All in a single line: | if a = b : print "hello"; /if |
One statement
Often the "if" statement is used to perform conditionally a single statement
as break, print, etc... and the syntax would have to be simpler in a such
case. And also, when a statement exits the processing, it seems not natural
for the code to add anything after it.
For example:
if condition : break; /if or
if condition : return; /if
The /if seems to be useless since we don't reach it, and Scriptol just allows
it to be removed, providing the compiler is informed it is a simplified structure,
that is done thanks to the one-statement syntax, and if needed, the "let"
keyword.
The word "let" means for Last, Execute, Terminate.
The let statement is always the last part of a control structure. When "let"
is followed by another keyword (break, print, etc...) it may be omitted after
the condition (not at end of while).
When a condition is followed by let rather than by : or by an end of line,
that means the body of the structure is a single statement, and can't be more
than one statement. This rule applies to all control structures, but "do"
that has no condition at beginning.
Syntax:
if condition let statement
if condition let statement; else statement
One-statement examples: | if
x < 10 break if (x + y) = 0 print "some text" if a < 10 print "less"; else print "more" |
Composite if
We have just studied above the simplest form of the if control structure.
In fact, it is in Scriptol a very powerful structure that may perform several
comparisons or other kinds of tests in alternative blocks, and is suitable
for pattern-matching. Supposing you want to compare a variable (number, text,
array) with several values, the if structure can have this form:
if variable
= value1: statements
= value2: statements
= value3: statements
else statements
/if
Only the statements which follow the value matching the variable will be processed.
If no value is matched, the statements following "else" will be processed.
One may also test if the variable is < or > to a value, and so one.
Example of composite if structure | if
a < 10 : print "less" = 10 : print "equal" > 10 : print "grater" /if |
One may also test if the value is inside an array:
Example of composite if using in | if
a in array(1,2,3): print "first" in array(4,5,6): print "second" in array(7,8) : print "third" else print "other" /if |
These operators may be used and mixed in a composite if:
=, <, >, <=, >=, !=, <>, in, match.
Exercises |
1) An array holds the name of four fruits: orange, kiwi, pear, plum. |