Skip to content
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
14 changes: 14 additions & 0 deletions _content/tour/flowcontrol.article
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ of the `else` blocks.
(Both calls to `pow` return their results before the call to `fmt.Println`
in `main` begins.)

Because of the way Go parses source code you must put the `else` on the same line
as the closing brace of the associated `if` block.
`else` after a newline generates a syntax error:

if v := math.Pow(x, n); v < lim {
return v
}
else { // syntax error: unexpected else, expecting }
fmt.Printf("%g >= %g\n", v, lim)
}

If you are interested in details on why this is, read the Language Specification sections
on [[/ref/spec#Semicolons][Semicolons]] and [[/ref/spec#If_statements][If Statements]].

.play flowcontrol/if-and-else.go

* Exercise: Loops and Functions
Expand Down