Skip to content

Commit 1424f1e

Browse files
authored
Merge pull request #231 from onflow/control-flow-scope
Edit Control Flow and Scope articles
2 parents f6b6c85 + 94ad001 commit 1424f1e

2 files changed

Lines changed: 48 additions & 106 deletions

File tree

docs/language/control-flow.md

Lines changed: 44 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ Control flow statements control the flow of execution in a function.
99

1010
If-statements allow a certain piece of code to be executed only when a given condition is true.
1111

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:
1817

1918
```cadence
2019
let a = 0
@@ -32,10 +31,7 @@ if (a != 0) {
3231
// `b` is `1`
3332
```
3433

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:
3935

4036
```cadence
4137
let a = 0
@@ -50,8 +46,7 @@ if a == 1 {
5046
// `b` is `2`
5147
```
5248

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:
5550

5651
```cadence
5752
let a = 0
@@ -78,19 +73,13 @@ if a == 1 {
7873
// `b` is `2`
7974
```
8075

81-
## Optional Binding
76+
## Optional binding
8277

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.
8579

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.
8981

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:
9483

9584
```cadence
9685
let maybeNumber: Int? = 1
@@ -116,29 +105,15 @@ if let number = noNumber {
116105

117106
## Switch
118107

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.
121109

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.
126111

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.
132113

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.
137115

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:
142117

143118
```cadence
144119
fun word(_ n: Int): String {
@@ -167,8 +142,7 @@ word(4) // returns "other"
167142

168143
### Duplicate cases
169144

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:
172146

173147
```cadence
174148
fun test(_ n: Int): String {
@@ -195,29 +169,17 @@ word(1) // returns "one", not "also one"
195169

196170
### `break`
197171

198-
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.
201173

202-
### No Implicit Fallthrough
174+
### No implicit fallthrough
203175

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.
209177

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.
212179

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.
217181

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:
221183

222184
```cadence
223185
fun words(_ n: Int): [String] {
@@ -251,21 +213,15 @@ words(4) // returns `["other"]`
251213

252214
## Looping
253215

216+
The following sections describe looping statements.
217+
254218
### while-statement
255219

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.
258221

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.
263223

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:
269225

270226
```cadence
271227
var a = 0
@@ -278,20 +234,13 @@ while a < 5 {
278234

279235
### For-in statement
280236

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.
283238

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.
288240

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.
291242

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:
295244

296245
```cadence
297246
let array = ["Hello", "World", "Foo", "Bar"]
@@ -307,11 +256,7 @@ for element in array {
307256
// "Bar"
308257
```
309258

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):
315260

316261
```cadence
317262
let array = ["Hello", "World", "Foo", "Bar"]
@@ -327,8 +272,7 @@ for index, element in array {
327272
// 3
328273
```
329274

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:
332276

333277
```cadence
334278
let dictionary = {"one": 1, "two": 2}
@@ -358,10 +302,9 @@ dictionary.forEachKey(fun (key: String): Bool {
358302
})
359303
```
360304

361-
### Ranges in Loops
305+
### Ranges in loops
362306

363-
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:
365308

366309
```cadence
367310
let range: InclusiveRange<UInt> = InclusiveRange(1, 100, step: 2)
@@ -419,8 +362,7 @@ while range.contains(index) {
419362

420363
### `continue` and `break`
421364

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:
424366

425367
```cadence
426368
var i = 0
@@ -448,8 +390,7 @@ for element in array {
448390
449391
```
450392

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:
453394

454395
```cadence
455396
var x = 0
@@ -476,7 +417,9 @@ for element in array {
476417

477418
## Immediate function return: return-statement
478419

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
425+
[`InclusiveRange` value]: ./values-and-types/inclusive-range.md

docs/language/scope.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ title: Scope
33
sidebar_position: 8
44
---
55

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:
87

98
```cadence
109
let x = 10
@@ -34,7 +33,7 @@ fun doubleAndAddOne(_ n: Int): Int {
3433
double(1)
3534
```
3635

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):
3837

3938
```cadence
4039
let x = 2
@@ -47,7 +46,7 @@ fun test(): Int {
4746
test() // is `3`
4847
```
4948

50-
Scope is lexical, not dynamic.
49+
Scope is lexical, not dynamic:
5150

5251
```cadence
5352
let x = 10
@@ -64,7 +63,7 @@ fun g(): Int {
6463
g() // is `10`, not `20`
6564
```
6665

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):
6867

6968
```cadence
7069
let x = 2

0 commit comments

Comments
 (0)