order and the nesting of the control structures. The real value of the
expressions used has no effect on the control flow analyses.
-~~~
+~~~nitish
if true then
return
else
return
end
print 1 # Compile error: unreachable statement
+~~~
+~~~
if true then
return
end
## if
~~~
-if exp then stm
-if exp then stm else stm
+var exp = true
+# ...
+if exp then print 1
+if exp then print 2 else print 2
if exp then
- stms
+ print 1
+ print 2
end
if exp then
- stms
+ print 1
+ print 2
else if exp then
- stms
+ print 10
+ print 20
else
- stms
+ print 100
+ print 200
end
~~~
structure; then an error is signaled since a statement cannot begin with
`else`.
-~~~
-if exp then stm # OK: complete 'if' structure
-else stm # Syntax error: unexpected 'else'
+~~~nitish
+if exp then print 1 # OK: complete 'if' structure
+else print 2 # Syntax error: unexpected 'else'
~~~
## while
~~~
-while exp do stm
-while exp do
- stms
+var x = 0
+while x < 10 do x += 1
+print x # outputs 10
+
+while x < 20 do
+ print x # outputs 10 11 ... 19
+ x += 1
end
~~~
`for` declares an automatic variable used to iterates on `Collection` (`Array` and `Range` are both `Collection`).
~~~
-for x in [1..5] do print x # outputs 1 2 3 4 5
-for x in [1, 4, 6] do
- print x # outputs 1 4 6
+for i in [1..5] do print i # outputs 1 2 3 4 5
+for i in [1, 4, 6] do
+ print i # outputs 1 4 6
end
~~~
Step can also be used to specify the size of each increment at the end of a for cycle.
~~~
-for x in [9 .. 4].step(-1) do print x # outputs 9 8 7 6 5 4
-for x in [9 .. 4[.step(-2) do print x # outputs 9 7 5
+for i in [9 .. 4].step(-1) do print i # outputs 9 8 7 6 5 4
+for i in [9 .. 4[.step(-2) do print i # outputs 9 7 5
~~~
## loop
~~~
loop
- stms
+ print 1
if exp then break
- stms
+ print 2
end
~~~
~~~
do
- var x = 5
- print x
+ var j = 5
+ print j
end
-# x is not defined here
+# j is not defined here
~~~
## break, continue and label
`label` can be used with `break` or `continue` to act on a specific control structure (not necessary the current one). The corresponding `label` must be defined after the `end` keyword of the designated control structure.
~~~
-for i in [0..width[ do
- for j in [0..height[ do
- if foo(i, j) then break label outer_loop
+for i in [0..10[ do
+ for j in [0..10[ do
+ if i + j > 15 then break label outer_loop
+ print "{i},{j}"
# The 'break' breaks the 'for i' loop
end
end label outer_loop
~~~
do
- stmts
- if expr then break label block
- stmts
+ print 1 # printed
+ if exp then break label block
+ print 2 # not printed because exp is true
end label block
~~~
`assert` verifies that a given Boolean expression is true, or else it aborts. An optional label can be precised, it will be displayed on the error message. An optional `else` can also be added and will be executed before the abort.
~~~
-assert bla: whatever else
- # "bla" is the label
- # "whatever" is the expression to verify
+assert bla: exp else
+ # `bla` is the label
+ # `exp` is the expression to verify
print "Fatal error in module blablabla."
print "Please contact the customer service."
end