ba1107ffaa2c78416164142d589a9eeaa714263a
[nit.git] / doc / manual / structure.md
1 # Control Structures
2
3 Traditional procedural control structures exist in Nit. They
4 also often exist in two versions: a one-liner and a block version.
5
6 ## Control Flow
7
8 Control structures dictate the control flow of the
9 program. Nit heavily refers to the control flow in its specification:
10
11 -   No unreachable statement;
12
13 -   No usage of undefined variables;
14
15 -   No function without a `return` with a value;
16
17 -   Adaptive typing.
18
19 Some structures alter the control flow but are not described in this
20 section: `and`, `or`, `not`, `or else` and `return`.
21
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.
25
26 ~~~
27 if true then
28     return
29 else
30     return
31 end
32 print 1 # Compile error: unreachable statement
33
34 if true then
35     return
36 end
37 print 1 # OK, but never executed
38 ~~~
39
40 ## if
41
42 ~~~
43 if exp then stm
44 if exp then stm else stm
45 if exp then
46     stms
47 end
48
49 if exp then
50     stms
51 else if exp then
52     stms
53 else
54     stms
55 end
56 ~~~
57
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
61 `else`.
62
63 ~~~
64 if exp then stm # OK: complete 'if' structure
65 else stm # Syntax error: unexpected 'else'
66 ~~~
67
68 ## while
69
70 ~~~
71 while exp do stm
72 while exp do
73     stms
74 end
75 ~~~
76
77 ## for
78
79 `for` declares an automatic variable used to iterates on `Collection` (`Array` and `Range` are both `Collection`).
80
81 ~~~
82 for x in [1..5] do print x # outputs 1 2 3 4 5
83 for x in [1, 4, 6] do
84     print x # outputs 1 4 6
85 end
86 ~~~
87
88 `for` can also be used with reversed ranges to iterate in reverse order.
89
90 Step can also be used to specify the size of each increment at the end of a for cycle.
91
92 ~~~
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
95 ~~~
96
97 ## loop
98
99 Infinite loops are mainly used with breaks. They are useful to implement *until* loops or to simulate the *exit when* control of Ada.
100
101 ~~~
102 loop
103     stms
104     if exp then break
105     stms
106 end
107 ~~~
108
109 Note that `loop` is different from `while true` because the control flow does not consider the values of expression.
110
111 ## do
112
113 Single `do` are used to create scope for variables or to be attached with labeled breaks.
114
115 ~~~
116 do
117     var x = 5
118     print x
119 end
120 # x is not defined here
121 ~~~
122
123 ## break, continue and label
124
125 Unlabeled `break` exits the current `for`, `while`, `loop`, Unlabeled `continue` skips the current `for`, `while`, `loop`.
126
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.
128
129 ~~~
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
134     end
135 end label outer_loop
136 ~~~
137
138 `label` can also be used with `break` and single `do` structures.
139
140 ~~~
141 do
142     stmts
143     if expr then break label block
144     stmts
145 end label block
146 ~~~
147
148 ## abort
149
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.
151
152 ## assert
153
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.
155
156 ~~~
157 assert bla: whatever else
158     # "bla" is the label
159     # "whatever" is the expression to verify
160     print "Fatal error in module blablabla."
161     print "Please contact the customer service."
162 end
163 ~~~