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
37 print 1 # OK, but never executed
44 if exp then stm else stm
58 Note that the following example is invalid since the first line is
59 syntactically complete thus the newline terminate the whole `if`
60 structure; then an error is signaled since a statement cannot begin with
64 if exp then stm # OK: complete 'if' structure
65 else stm # Syntax error: unexpected 'else'
79 `for` declares an automatic variable used to iterates on `Collection` (`Array` and `Range` are both `Collection`).
82 for x in [1..5] do print x # outputs 1 2 3 4 5
84 print x # outputs 1 4 6
88 `for` can also be used with reversed ranges to iterate in reverse order.
90 Step can also be used to specify the size of each increment at the end of a for cycle.
93 for x in [9 .. 4].step(-1) do print x # outputs 9 8 7 6 5 4
94 for x in [9 .. 4[.step(-2) do print x # outputs 9 7 5
99 Infinite loops are mainly used with breaks. They are useful to implement *until* loops or to simulate the *exit when* control of Ada.
109 Note that `loop` is different from `while true` because the control flow does not consider the values of expression.
113 Single `do` are used to create scope for variables or to be attached with labeled breaks.
120 # x is not defined here
123 ## break, continue and label
125 Unlabeled `break` exits the current `for`, `while`, `loop`, Unlabeled `continue` skips the current `for`, `while`, `loop`.
127 `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.
130 for i in [0..width[ do
131 for j in [0..height[ do
132 if foo(i, j) then break label outer_loop
133 # The 'break' breaks the 'for i' loop
138 `label` can also be used with `break` and single `do` structures.
143 if expr then break label block
150 `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.
154 `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.
157 assert bla: whatever else
159 # "whatever" is the expression to verify
160 print "Fatal error in module blablabla."
161 print "Please contact the customer service."