Control flow statements in Go programming language are statements that control the flow of execution of the program. They allow you to specify the order in which statements are executed.
Syntax Examples
Following are the control flow statements in Go:
if statement:
The if
statement allows you to execute a block of code if a specified condition is true.
if a > b {
fmt.Println("a is greater than b")
}
if-else statement:
The if-else
statement allows you to execute a block of code if a specified condition is true and another block of code if the condition is false.
if a > b {
fmt.Println("a is greater than b")
} else {
fmt.Println("b is greater than a")
}
else-if statement:
The else-if
statement allows you to check multiple conditions in a single if
statement.
if a > b {
fmt.Println("a is greater than b")
} else if a < b {
fmt.Println("b is greater than a")
} else {
fmt.Println("a and b are equal")
}
switch statement:
The switch
statement allows you to select one of many blocks of code to execute based on a specified value.
switch day {
case "Monday":
fmt.Println("Today is Monday")
case "Tuesday":
fmt.Println("Today is Tuesday")
case "Wednesday":
fmt.Println("Today is Wednesday")
default:
fmt.Println("Unknown day")
}
for loop statement:
The for
loop allows you to execute a block of code multiple times based on a specified condition.
for i := 0; i < 5; i++ {
fmt.Println(i)
}
range loop statement:
The range
loop allows you to iterate over elements of an array, slice, map, string, or channel.
numbers := []int{1, 2, 3, 4, 5}
for index, value := range numbers {
fmt.Printf("Index: %d Value: %d\n", index, value)
}
break statement:
The break
statement allows you to terminate a loop or switch statement prematurely.
for i := 0; i < 5; i++ {
if i == 3 {
break
}
fmt.Println(i)
}
continue statement:
The continue
statement allows you to skip the current iteration of a loop and continue with the next iteration.
for i := 0; i < 5; i++ {
if i == 3 {
continue
}
fmt.Println(i)
}
These control flow statements can be used to create complex and flexible programs in Go by controlling the order in which different statements are executed.
Using loops
Here are some examples of loop statements used with collections in Go:
- Using
for
loop to iterate over an array:
numbers := [5]int{1, 2, 3, 4, 5}
for i := 0; i < len(numbers); i++ {
fmt.Print(numbers[i], " ")
}
// Output: 1 2 3 4 5
- Using
for range
loop to iterate over an array:
numbers := [5]int{1, 2, 3, 4, 5}
for index, value := range numbers {
fmt.Printf("Index: %d Value: %d\n", index, value)
}
// Output:
// Index: 0 Value: 1
// Index: 1 Value: 2
// Index: 2 Value: 3
// Index: 3 Value: 4
// Index: 4 Value: 5
- Using
for range
loop to iterate over a slice:
numbers := []int{1, 2, 3, 4, 5}
for index, value := range numbers {
fmt.Printf("Index: %d Value: %d\n", index, value)
}
// Output:
// Index: 0 Value: 1
// Index: 1 Value: 2
// Index: 2 Value: 3
// Index: 3 Value: 4
// Index: 4 Value: 5
- Using
for range
loop to iterate over a map:
ages := map[string]int{
"Alice": 25,
"Bob": 30,
"Carol": 35,
}
for name, age := range ages {
fmt.Printf("%s is %d years old\n", name, age)
}
// Output:
// Alice is 25 years old
// Bob is 30 years old
// Carol is 35 years old
- Using
for
loop to iterate over a slice andbreak
statement to exit once a condition is met:
numbers := []int{1, 2, 3, 4, 5}
for _, value := range numbers {
if value > 3 {
break
}
fmt.Print(value, " ")
}
// Output: 1 2 3
- Using
for
loop to iterate over a slice andcontinue
statement to skip an iteration based on a condition:
numbers := []int{1, 2, 3, 4, 5}
for _, value := range numbers {
if value%2 == 0 {
continue
}
fmt.Print(value, " ")
}
// Output: 1 3 5
2D Slice Loop
Here's an example of a 2D slice with nested loops in Go:
package main
import "fmt"
func main() {
// Create a 2D slice with 3 rows and 4 columns
numbers := [][]int{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
}
// Use nested loops to iterate over the 2D slice and print each element
for i := 0; i < len(numbers); i++ {
for j := 0; j < len(numbers[i]); j++ {
fmt.Print(numbers[i][j], " ")
}
fmt.Println()
}
}
This code creates a 2D slice with 3 rows and 4 columns, then uses nested loops to iterate over the slice and print each element. The outer loop iterates over each row, and the inner loop iterates over each column in the current row. The fmt.Println()
statement is used to move to the next line after printing each row so that the output is properly formatted.
Disclaim. This entire article was generated by AI. I have nothing to add. You are welcome to ask other question yourself and generate more examples. If you find dis insufficient, visit my website text article and learn more: https://sagecode.net/go/control.html
Have fun, don't give up. ππΌπ