You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -9,12 +9,11 @@ Control flow statements control the flow of execution in a function.
9
9
10
10
If-statements allow a certain piece of code to be executed only when a given condition is true.
11
11
12
-
The if-statement starts with the `if` keyword, followed by the condition,
13
-
and the code that should be executed if the condition is true
14
-
inside opening and closing braces.
15
-
The condition expression must be boolean.
16
-
The braces are required and not optional.
17
-
Parentheses around the condition are optional.
12
+
The if-statement starts with the `if` keyword, followed by the condition, and the code that should be executed if the condition is true, inside opening and closing braces.
13
+
14
+
- The condition expression must be a boolean.
15
+
- The braces are required and not optional.
16
+
- Parentheses around the condition are optional:
18
17
19
18
```cadence
20
19
let a = 0
@@ -32,10 +31,7 @@ if (a != 0) {
32
31
// `b` is `1`
33
32
```
34
33
35
-
An additional, optional else-clause can be added to execute another piece of code
36
-
when the condition is false.
37
-
The else-clause is introduced by the `else` keyword followed by braces
38
-
that contain the code that should be executed.
34
+
An additional, optional else-clause can be added to execute another piece of code when the condition is false. The else-clause is introduced by the `else` keyword, followed by braces that contain the code that should be executed:
39
35
40
36
```cadence
41
37
let a = 0
@@ -50,8 +46,7 @@ if a == 1 {
50
46
// `b` is `2`
51
47
```
52
48
53
-
The else-clause can contain another if-statement, i.e., if-statements can be chained together.
54
-
In this case the braces can be omitted.
49
+
The else-clause can contain another if-statement (i.e., if-statements can be chained together). In this case, the braces can be omitted:
55
50
56
51
```cadence
57
52
let a = 0
@@ -78,19 +73,13 @@ if a == 1 {
78
73
// `b` is `2`
79
74
```
80
75
81
-
## Optional Binding
76
+
## Optional binding
82
77
83
-
Optional binding allows getting the value inside an optional.
84
-
It is a variant of the if-statement.
78
+
Optional binding allows getting the value inside an optional. It is a variant of the if-statement.
85
79
86
-
If the optional contains a value, the first branch is executed
87
-
and a temporary constant or variable is declared and set to the value contained in the optional;
88
-
otherwise, the else branch (if any) is executed.
80
+
If the optional contains a value, the first branch is executed and a temporary constant or variable is declared and set to the value contained in the optional; otherwise, the else branch (if any) is executed.
89
81
90
-
Optional bindings are declared using the `if` keyword like an if-statement,
91
-
but instead of the boolean test value, it is followed by the `let` or `var` keywords,
92
-
to either introduce a constant or variable, followed by a name,
93
-
the equal sign (`=`), and the optional value.
82
+
Optional bindings are declared using the `if` keyword like an if-statement, but instead of the boolean test value, it is followed by the `let` or `var` keywords, to either introduce a constant or variable, followed by a name, the equal sign (`=`), and the optional value:
94
83
95
84
```cadence
96
85
let maybeNumber: Int? = 1
@@ -116,29 +105,15 @@ if let number = noNumber {
116
105
117
106
## Switch
118
107
119
-
Switch-statements compare a value against several possible values of the same type, in order.
120
-
When an equal value is found, the associated block of code is executed.
108
+
Switch-statements compare a value against several possible values of the same type, in order. When an equal value is found, the associated block of code is executed.
121
109
122
-
The switch-statement starts with the `switch` keyword, followed by the tested value,
123
-
followed by the cases inside opening and closing braces.
124
-
The test expression must be equatable.
125
-
The braces are required and not optional.
110
+
The switch-statement starts with the `switch` keyword, followed by the tested value, followed by the cases inside opening and closing braces. The test expression must be equatable. The braces are required and not optional.
126
111
127
-
Each case is a separate branch of code execution
128
-
and starts with the `case` keyword,
129
-
followed by a possible value, a colon (`:`),
130
-
and the block of code that should be executed
131
-
if the case's value is equal to the tested value.
112
+
Each case is a separate branch of code execution and starts with the `case` keyword, followed by a possible value, a colon (`:`), and the block of code that should be executed if the case's value is equal to the tested value.
132
113
133
-
The block of code associated with a switch case
134
-
[does not implicitly fall through](#no-implicit-fallthrough),
135
-
and must contain at least one statement.
136
-
Empty blocks are invalid.
114
+
The block of code associated with a switch case [does not implicitly fall through], and must contain at least one statement. Empty blocks are invalid.
137
115
138
-
An optional default case may be given by using the `default` keyword.
139
-
The block of code of the default case is executed
140
-
when none of the previous case tests succeeded.
141
-
It must always appear last.
116
+
An optional default case may be given by using the `default` keyword. The block of code of the default case is executed when none of the previous case tests succeed. It must always appear last:
142
117
143
118
```cadence
144
119
fun word(_ n: Int): String {
@@ -167,8 +142,7 @@ word(4) // returns "other"
167
142
168
143
### Duplicate cases
169
144
170
-
Cases are tested in order, so if a case is duplicated,
171
-
the block of code associated with the first case that succeeds is executed.
145
+
Cases are tested in order, so if a case is duplicated, the block of code associated with the first case that succeeds is executed:
The block of code associated with a switch case may contain a `break` statement.
199
-
It ends the execution of the switch statement immediately
200
-
and transfers control to the code after the switch statement
172
+
The block of code associated with a switch case may contain a `break` statement. It ends the execution of the switch statement immediately and transfers control to the code after the switch statement.
201
173
202
-
### No Implicit Fallthrough
174
+
### No implicit fallthrough
203
175
204
-
Unlike switch statements in some other languages,
205
-
switch statements in Cadence do not "fall through":
206
-
execution of the switch statement finishes as soon as the block of code
207
-
associated with the first matching case is completed.
208
-
No explicit `break` statement is required.
176
+
Unlike switch statements in some other languages, switch statements in Cadence do not _fall through_: execution of the switch statement finishes as soon as the block of code associated with the first matching case is completed. No explicit `break` statement is required.
209
177
210
-
This makes the switch statement safer and easier to use,
211
-
avoiding the accidental execution of more than one switch case.
178
+
This makes the switch statement safer and easier to use, avoiding the accidental execution of more than one switch case.
212
179
213
-
Some other languages implicitly fall through
214
-
to the block of code associated with the next case,
215
-
so it is common to write cases with an empty block
216
-
to handle multiple values in the same way.
180
+
Some other languages implicitly fall through to the block of code associated with the next case, so it is common to write cases with an empty block to handle multiple values in the same way.
217
181
218
-
To prevent developers from writing switch statements
219
-
that assume this behaviour, blocks must have at least one statement.
220
-
Empty blocks are invalid.
182
+
To protect developers from writing switch statements that assume this behavior, blocks must have at least one statement. Empty blocks are invalid:
The following sections describe looping statements.
217
+
254
218
### while-statement
255
219
256
-
While-statements allow a certain piece of code to be executed repeatedly,
257
-
as long as a condition remains true.
220
+
While-statements allow a certain piece of code to be executed repeatedly, as long as a condition remains true.
258
221
259
-
The while-statement starts with the `while` keyword, followed by the condition,
260
-
and the code that should be repeatedly
261
-
executed if the condition is true inside opening and closing braces.
262
-
The condition must be boolean and the braces are required.
222
+
The while-statement starts with the `while` keyword, followed by the condition, and the code that should be repeatedly executed if the condition is true inside opening and closing braces. The condition must be boolean, and the braces are required.
263
223
264
-
The while-statement will first evaluate the condition.
265
-
If it is true, the piece of code is executed and the evaluation of the condition is repeated.
266
-
If the condition is false, the piece of code is not executed
267
-
and the execution of the whole while-statement is finished.
268
-
Thus, the piece of code is executed zero or more times.
224
+
The while-statement will first evaluate the condition. If it is true, the piece of code is executed, and the evaluation of the condition is repeated. If the condition is false, the piece of code is not executed, and the execution of the whole while-statement is finished. Thus, the piece of code is executed zero or more times:
269
225
270
226
```cadence
271
227
var a = 0
@@ -278,20 +234,13 @@ while a < 5 {
278
234
279
235
### For-in statement
280
236
281
-
For-in statements allow a certain piece of code to be executed repeatedly for
282
-
each element in an array.
237
+
For-in statements allow a certain piece of code to be executed repeatedly for each element in an array.
283
238
284
-
The for-in statement starts with the `for` keyword, followed by the name of
285
-
the element that is used in each iteration of the loop,
286
-
followed by the `in` keyword, and then followed by the array
287
-
that is being iterated through in the loop.
239
+
The for-in statement starts with the `for` keyword, followed by the name of the element that is used in each iteration of the loop, followed by the `in` keyword, and then followed by the array that is being iterated through in the loop.
288
240
289
-
Then, the code that should be repeatedly executed in each iteration of the loop
290
-
is enclosed in curly braces.
241
+
Then, the code that should be repeatedly executed in each iteration of the loop is enclosed in curly braces.
291
242
292
-
If there are no elements in the data structure, the code in the loop will not
293
-
be executed at all. Otherwise, the code will execute as many times
294
-
as there are elements in the array.
243
+
If there are no elements in the data structure, the code in the loop will not be executed at all. Otherwise, the code will execute as many times as there are elements in the array:
295
244
296
245
```cadence
297
246
let array = ["Hello", "World", "Foo", "Bar"]
@@ -307,11 +256,7 @@ for element in array {
307
256
// "Bar"
308
257
```
309
258
310
-
Optionally, developers may include an additional variable preceding the element name,
311
-
separated by a comma.
312
-
When present, this variable contains the current
313
-
index of the array being iterated through
314
-
during each repeated execution (starting from 0).
259
+
Optionally, developers may include an additional variable preceding the element name, separated by a comma. When present, this variable contains the current index of the array being iterated through during each repeated execution (starting from 0):
315
260
316
261
```cadence
317
262
let array = ["Hello", "World", "Foo", "Bar"]
@@ -327,8 +272,7 @@ for index, element in array {
327
272
// 3
328
273
```
329
274
330
-
To iterate over a dictionary's entries (keys and values),
331
-
use a for-in loop over the dictionary's keys and get the value for each key:
275
+
To iterate over a dictionary's entries (keys and values), use a for-in loop over the dictionary's keys and get the value for each key:
An [`InclusiveRange` value](./values-and-types/inclusive-range.md) can be used in a for-in statement in place of an array or dictionary. In this case,
364
-
the loop will iterate over all the values contained in the range, beginning with `range.start` and ending with `range.end`. E.g.
307
+
An [`InclusiveRange` value] can be used in a for-in statement in place of an array or dictionary. In this case, the loop will iterate over all the values contained in the range, beginning with `range.start` and ending with `range.end`. For example:
365
308
366
309
```cadence
367
310
let range: InclusiveRange<UInt> = InclusiveRange(1, 100, step: 2)
@@ -419,8 +362,7 @@ while range.contains(index) {
419
362
420
363
### `continue` and `break`
421
364
422
-
In for-loops and while-loops, the `continue` statement can be used to stop
423
-
the current iteration of a loop and start the next iteration.
365
+
In for-loops and while-loops, the `continue` statement can be used to stop the current iteration of a loop and start the next iteration:
424
366
425
367
```cadence
426
368
var i = 0
@@ -448,8 +390,7 @@ for element in array {
448
390
449
391
```
450
392
451
-
The `break` statement can be used to stop the execution
452
-
of a for-loop or a while-loop.
393
+
The `break` statement can be used to stop the execution of a for-loop or a while-loop:
453
394
454
395
```cadence
455
396
var x = 0
@@ -476,7 +417,9 @@ for element in array {
476
417
477
418
## Immediate function return: return-statement
478
419
479
-
The return-statement causes a function to return immediately,
480
-
i.e., any code after the return-statement is not executed.
481
-
The return-statement starts with the `return` keyword
482
-
and is followed by an optional expression that should be the return value of the function call.
420
+
The return-statement causes a function to return immediately (i.e., any code after the return-statement is not executed). The return-statement starts with the `return` keyword and is followed by an optional expression that should be the return value of the function call.
421
+
422
+
<!-- Relative links. Will not render on the page -->
423
+
424
+
[does not implicitly fall through]: #no-implicit-fallthrough
Copy file name to clipboardExpand all lines: docs/language/scope.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,7 @@ title: Scope
3
3
sidebar_position: 8
4
4
---
5
5
6
-
Every function and block (`{` ... `}`) introduces a new scope for declarations.
7
-
Each function and block can refer to declarations in its scope or any of the outer scopes.
6
+
Every function and block (`{` ... `}`) introduces a new scope for declarations. Each function and block can refer to declarations in its scope or any of the outer scopes:
8
7
9
8
```cadence
10
9
let x = 10
@@ -34,7 +33,7 @@ fun doubleAndAddOne(_ n: Int): Int {
34
33
double(1)
35
34
```
36
35
37
-
Each scope can introduce new declarations, i.e., the outer declaration is shadowed.
36
+
Each scope can introduce new declarations (i.e., the outer declaration is shadowed):
38
37
39
38
```cadence
40
39
let x = 2
@@ -47,7 +46,7 @@ fun test(): Int {
47
46
test() // is `3`
48
47
```
49
48
50
-
Scope is lexical, not dynamic.
49
+
Scope is lexical, not dynamic:
51
50
52
51
```cadence
53
52
let x = 10
@@ -64,7 +63,7 @@ fun g(): Int {
64
63
g() // is `10`, not `20`
65
64
```
66
65
67
-
Declarations are **not** moved to the top of the enclosing function (hoisted).
66
+
Declarations are **not** moved to the top of the enclosing function (hoisted):
0 commit comments