3 Traditional procedural control structures exist in Nit. They
4 also often exist in two versions: a one-liner and a block version.
8 Control structures dictate the control flow of the
9 program. Nit heavily refers to the control flow in its specification:
11 - No unreachable statement;
13 - No usage of undefined variables;
15 - No function without a `return` with a value;
19 Some structures alter the control flow but are not described in this
20 section: `and`, `or`, `not`, `or else` and `return`.
22 Note that the control flow is determined only from the position, the
23 order and the nesting of the control structures. The real value of the
24 expressions used has no effect on the control flow analyses.
32 print 1 # Compile error: unreachable statement
39 print 1 # OK, but never executed
48 if exp then print 2 else print 2
66 Note that the following example is invalid since the first line is
67 syntactically complete thus the newline terminate the whole `if`
68 structure; then an error is signaled since a statement cannot begin with
72 if exp then print 1 # OK: complete 'if' structure
73 else print 2 # Syntax error: unexpected 'else'
80 while x < 10 do x += 1
84 print x # outputs 10 11 ... 19
91 `for` declares an automatic variable used to iterates on `Collection` (`Array` and `Range` are both `Collection`).
94 for i in [1..5] do print i # outputs 1 2 3 4 5
96 print i # outputs 1 4 6
100 `for` can also be used with reversed ranges to iterate in reverse order.
102 Step can also be used to specify the size of each increment at the end of a for cycle.
105 for i in [9 .. 4].step(-1) do print i # outputs 9 8 7 6 5 4
106 for i in [9 .. 4[.step(-2) do print i # outputs 9 7 5
111 Infinite loops are mainly used with breaks. They are useful to implement *until* loops or to simulate the *exit when* control of Ada.
121 Note that `loop` is different from `while true` because the control flow does not consider the values of expression.
125 Single `do` are used to create scope for variables or to be attached with labeled breaks.
132 # j is not defined here
135 ## break, continue and label
137 Unlabeled `break` exits the current `for`, `while`, `loop`, Unlabeled `continue` skips the current `for`, `while`, `loop`.
139 `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.
144 if i + j > 15 then break label outer_loop
146 # The 'break' breaks the 'for i' loop
151 `label` can also be used with `break` and single `do` structures.
156 if exp then break label block
157 print 2 # not printed because exp is true
163 `abort` stops the program with a fatal error and prints a stack trace. Since there is currently no exception nor run-time-errors, abort is somewhat used to simulate them.
167 `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.
172 # `exp` is the expression to verify
173 print "Fatal error in module blablabla."
174 print "Please contact the customer service."