Control structures in a modern language

Modern languages ​​such as Scriptol can provide more concise and generic structures.

The if then control structure

For conditional processing, the instruction is the keyword if followed by a boolean expression, a list of instructions to be executed or not, depending on condition, and the keyword /if.

Here is the syntax in the Scriptol programming language.

if x = 5
  print "equal"
/if
We introduce an alternative by the keyword else: when the condition is false, another set of instructions must be processed.

if x = 5
  print "equal"
else
  print "different"
/if

When the body of the structure has only one instruction, and is not another structure, the syntax can be reduced tos one line.

if x = 5 print x 
If the instruction is not a command like "print", "break" or "continue" ... etc, the keyword "let" is required.

if x = 5 let y + 1 

composite if

The structure can be extended into a broader construct that is both an "if" structure and a "switch case", which is also more advanced than the equivalent C because we can test any type of variable.

if x 
= 5: print "equal"
> 5: print "more"
else
  print "less"
/if

for

The for loop is common to all languages, and the variation for each that can scan the contents of a list is added to all dynamic languages. But in JavaScript, For each is so inefficient that it is better to use the classic for loop.

The syntax is simplified in Scriptol through instruction in range .

for int x in 0 .. 10
  print x
/for

If the body of the structure has only one instruction, a simplified one-line syntax can be used:

for int x in 0 .. 10 print x

The control structure can browse for a range, but also a text or a table.

text hello = "hello"
for text t in hello  print t

array a = array( 1, 2, 3 )
for var x in a print x

while

The C language has created a while loop to execute until a condition is met. The disadvantage is that the C language​ and derivatives (C++, Java, C #) easily lead to infinite loops. Here are two examples.

while(x < 5);
{
printf("%d\n", x);
  x += 1:
}

Perhaps you have noticed immediately, (or maybe not!), but the program is buggy. The programmer, prompted by the habit of putting a semicolon after each statement accidentally added one to a post-condition!

Another example, without semicolon misplaced this time, and equally common. In this program we want to skip a block of statements when x is 5 (break statement), and exit the loop when x is 10 (continue statement).

while(true)
{
printf("%d\n", x); if(x == 10) break;
  if(x == 5) continue;
  ... some instructions ...
  x += 1;
}

Nothing shocking at first glance in this program, but if you look closer, once reached the value 5 in x, the counter will not change ever, because the increment is skipped by the continue statement. So the end of the loop will never be reached.

To increment a variable, Scriptol uses x + 1. This would not work in C or any language derived where the instructions are also expressions that return a value. So this is how looks a while loop in Scriptol.

while x < 5
  print x
  x + 1
/while 

An infinite loop is always possible with this syntax, but to avoid it, a more secure syntax is provided where the loop end is replaced by an instruction to increment the counter.

while true
print x if x = 10 break
if x = 5 continue
... some instructions ...
let x + 1

Consequently, in Scriptol, the continue statement jumps to the let statement and the condition variable is always incremented.

do case

Pattern-matching is implemented in Scriptol with the do ... case structure.

do
 case a = 1: ...
 case a > 1: ...
/do

Knowing that we can execute a processing in a loop by adding a condition "until" or "while" multiple options are available to suit any kind of automation.

do
...
until condition

Or:

do
...
/do while condition

Browsing several tables at once (Compatible with Scriptol 2)

To browse the contents of one or more arrays, and apply a treatment to each element of (the) array (s), the for loop by reading both the index and the value of each item is indispensable.

for i, v in  a 
   a[i] * a[i]
   print a[i]
/for

We use the value for reading only, the index for reading or writing.

Example with two arrays:

for i, v in a
   print a[i] + b[i]
/for

How to apply a function to an array

The map method takes as argument the name of a function. Each element of the array is then passed as a parameter to the function in a loop. The function should require a single argument.

a.map(fonction)

Complete example with a function with a single argument, which display the square of each element:

array a = array(1,2,3,4)
 
void fun(number x)
print x * x
return
print a.map(fun)

This displays : [1,4,9,16]

In conclusion, Scriptol has made progress in control structures. Very few were included in languages ​​created meanwhile, since 2001.