Home > Article > Backend Development > Learn flow control statements in Golang
The flow control statements in Go provide if else, switch, for and while to control code execution based on conditions. Among them, if else is used to execute different code blocks based on conditions, switch is used to execute one of multiple code blocks based on conditions, and for and while are used to execute code in a loop until the condition is not met.
Learn Control Flow Statements in Go
Control flow statements are one of the most basic building blocks in programming. They are used to control the flow of code, allowing you to execute or skip specific blocks of code based on specific conditions. Go provides a series of flow control statements, including:
Actual case:
package main import "fmt" func main() { // if else 语句 x := 10 if x > 5 { fmt.Println("x is greater than 5") } else { fmt.Println("x is not greater than 5") } // switch 语句 switch x { case 10: fmt.Println("x is 10") case 20: fmt.Println("x is 20") default: fmt.Println("x is not 10 or 20") } // for 循环 for i := 0; i < 10; i++ { fmt.Println("i is", i) } // while 循环 i := 0 for i < 10 { fmt.Println("i is", i) i++ } }
Output:
x is greater than 5 x is 10 i is 0 i is 1 i is 2 i is 3 i is 4 i is 5 i is 6 i is 7 i is 8 i is 9
The above is the detailed content of Learn flow control statements in Golang. For more information, please follow other related articles on the PHP Chinese website!