Control structures

Control structures allow to process conditionally a block of statements, or to process them repetitively.
They have been improved in Scriptol to suppress frequent causes of errors, the most common being the infinite loops, and to apply the principle of pattern-matching that is appraised by programmers...

The control structures are:

if
 normal if.
 one-instruction if.
 composite if.

for
 normal for.
 one-instruction for.
 - options: in interval, in array.

scan
 by function.
 one-instruction scan.
 tagged scan.

while
 normal while.
 one-instruction while.
- options: let, forever.

do while
 normal.
 case / else / always.
 - option: forever.

do until

enum
 simple enum.
 dict enum.

Scriptol has a different syntax for one-line structure and multi-lines ones.

Multi-lines structure

A control structure usually has the syntax:

structure-name expression [:]
... instructions ...
/structure-name

The colon is optional (but if a statement is added on the same ligne). Here all statements including other structures may be embedded. Each statement is ended inside the body of a structure either by the end of line or a semicolon.

One-statement structure

In this case there is no ending but the end of line. A semi-colon, if present after the statement, terminates the whole construct instead.
A one-statement structure has the form:

keyword expression let statement    or
keyword expression ? statement      or
keyword
expression statement starting by a keyword.

The statement may be any basic statement and can't be a control structure.

"Let" means for "Last statement, Execute, Terminate". The let element is always the last part of a control structure and may be the single one.
The "?" symbol is just a shorter replacement for "let".

If the keyword "else" or "let" completes the control structure on the same line, the statement and, must be separated by a semicolon from the keywords "else" or "let".

Why this mono-line form?

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 examples:
if condition : break; /if
if condition : return; /if

The /if seems to be useless since we don't reach it, and Scriptol just allows it to be removed, 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
while condition let statement; let incrementing
while condition let statement
for range let statement
scan arrays let statement

Examples if x = 5 let y = a + 3
if x < 10 break
if (x + y) = 0 print "some text"
if a < 10 print "less"; else print "more"
for w in array1 print w
scan a,b let a[] * b[]


If you already have used others programming languages, you have been surprised by the differences with their control structure, and the powerful of the Scriptol's ones. The if structure, not only merge the power of the if and the switch case of C++ for example, but is allows to test cases of various types, and to use different operators... Scriptol's structures has been designed for a more intuitive programming and for the human thought expressed directly into programs.


 Exercises

1) Here is a classical structure
int i
for i in 1 .. 3
    i * 2
/for

Convert it into a one-line structure.

2) Do the same with this structure:
int i
for i in 1..3
  print i * 2
/for

Answers