Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update control-flow.markdown - for statement section #1177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/site/control-flow.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ A `for` loop has three components:
3. A *body*. This is a curly block or a single statement. It gets executed once
for each iteration of the loop.

Note that a fresh variable is created for each iteration of the loop and so, if a [function](functions.html) in the body captures that variable, its value will not be affected by subsequent iterations.

<pre class="snippet">
var prints = []
for (i in 1..3) {
prints.add(Fn.new { System.print(i) })
}
for (print in prints) print.call() //> 1, 2, 3 not 3, 3, 3
</pre>

## Break statements

Sometimes, right in the middle of a loop body, you decide you want to bail out
Expand Down
Loading