Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Break Statement | Control Structures
Introduction to GoLang

book
Break Statement

The break keyword can be used to prematurely exit a loop, even if it has not reached its stop condition.

Here's an example of how break can be used inside a loop:

index.go

index.go

copy
package main
import "fmt"

func main() {
for i := 1; i <= 5; i++ {
fmt.Println(i)
if i == 3 {
break
}
}
fmt.Println("Loop ended")
}
123456789101112
package main import "fmt" func main() { for i := 1; i <= 5; i++ { fmt.Println(i) if i == 3 { break } } fmt.Println("Loop ended") }

The provided code terminates the loop when i==3 instead of i==5. Any code after the break statement within the loop is automatically ignored since the loop breaks at that specific point.

The break keyword, also referred to as the break statement, can be a valuable tool for controlling the loop's flow. It enables you to impose additional conditions within the loop.

question mark

Which lines will be included in the output of the following code?

package main
import "fmt"

func main() {
for i := 0; i < 3; i++ {
fmt.Println ("statement before break")
break
fmt.Println ("statement after break")
}
}

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 7

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

some-alt