Learn to use conditionals and loop statements in Golang

王林
Release: 2023-12-23 13:19:01
Original
825 people have browsed it

Learn to use conditionals and loop statements in Golang

To master conditional statements and loop statements in Golang, you need specific code examples

In Golang, conditional statements and loop statements are a very important part of the program. Conditional statements are used to determine the execution flow of the program, while loop statements are used to repeatedly execute a section of code. This article will introduce conditional statements and loop statements in Golang in detail and provide specific code examples.

  1. Conditional statements

Conditional statements are used to execute different code blocks based on whether the condition is true or false. In Golang, conditional statements include if statements, if-else statements and switch statements.

1.1. if statement

The if statement is used to execute the code block under the specified condition. If the condition is true, the code block after the if is executed. The sample code is as follows:

package main

import "fmt"

func main() {
    age := 18

    if age >= 18 {
        fmt.Println("你已经成年了")
    }
}
Copy after login

In the above code, if the value of age is greater than or equal to 18, then "You are an adult" is output. Otherwise, no action is performed.

1.2. if-else statement

The if-else statement is used to execute different blocks of code when the condition is true and when the condition is false. The sample code is as follows:

package main

import "fmt"

func main() {
    age := 17

    if age >= 18 {
        fmt.Println("你已经成年了")
    } else {
        fmt.Println("你还未成年")
    }
}
Copy after login

In the above code, if the value of age is greater than or equal to 18, then output "You are an adult"; otherwise, output "You are not yet a minor".

1.3. switch statement

The switch statement is used to select different code blocks for execution based on the value of the expression. The sample code is as follows:

package main

import "fmt"

func main() {
    fruit := "apple"

    switch fruit {
    case "apple":
        fmt.Println("这是一个苹果")
    case "banana":
        fmt.Println("这是一个香蕉")
    default:
        fmt.Println("这不是水果")
    }
}
Copy after login

In the above code, select different code blocks to output based on the value of fruit. If the value of fruit is "apple", then output "This is an apple"; if the value of fruit is "banana", then output "This is a banana"; otherwise, Output "This is not a fruit".

  1. Loop statement

Loop statement is used to repeatedly execute a section of code until a specific condition is met. In Golang, loop statements include for loops and range loops.

2.1. for loop

The for loop is used to repeatedly execute a piece of code until a specific condition is met. The sample code is as follows:

package main

import "fmt"

func main() {
    for i := 0; i < 5; i++ {
        fmt.Println(i)
    }
}
Copy after login

In the above code, use a for loop to output numbers from 0 to 4.

2.2. Range loop

The range loop is used to iterate over the elements in an array, slice, map, or channel. The sample code is as follows:

package main

import "fmt"

func main() {
    nums := []int{1, 2, 3, 4, 5}

    for index, value := range nums {
        fmt.Println("索引:", index, "值:", value)
    }
}
Copy after login

In the above code, use the range loop to traverse the nums slice and output the index and value of each element.

Through the code examples in this article, I believe that readers have mastered the basic usage of conditional statements and loop statements in Golang. When actually writing code, conditional statements and loop statements are very commonly used and can help us implement complex logic and repeated operations. Therefore, proficiency in these statements will play an important role in improving programming abilities.

The above is the detailed content of Learn to use conditionals and loop statements in Golang. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!