Golang Entry Series six Golang control statement

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Preface: This paper mainly expounds the use of if else, switch statement and for loop in Golang.

One, if Else statement

1.1 Basic syntax

In some cases, the parentheses on either side of the conditional statement can be omitted, and when the conditions are complex, you can use parentheses to make the code more readable. Conditions allowed are eligible, use &&, | | | Or!, you can use parentheses to promote the operation precedence of an expression and to improve the readability of the code.

第一种形式:if condition {    // do something } else {    // do something }第二种形式:if initialization; condition {    // do something}

1.2 Example

package mainimport "fmt"func main() {// 第一种形式n := 7if n >= 0 {fmt.Printf("%d is >=0\n",n)} else {fmt.Printf("%d is <0\n",n)}//第二种形式//初始化的变量为局部变量if m := 9; m < 0 {fmt.Printf("%d is <0\n",m)} else if m > 0 {fmt.Printf("%d is >0\n",m)} else {fmt.Printf("%d is = 0",m)}//带bool值的if bool1 := true; !bool1 {fmt.Printf("you are not right")} else if bool1 {fmt.Printf("you are right")}}

Second, switch statement

2.1 Basic syntax

Each case branch is unique and is tested from top to bottom until the match is reached. If you want to continue executing the code for subsequent branches after executing the code for each branch, you can use the Fallthrough keyword to achieve the purpose.

switch var1 {    case val1:        ...    case val2:        ...    default:        ...}

2.2 Example

package mainimport "fmt"func main() {switch n := 7; {case n > 0 && n == 7 :fmt.Printf("%d is > 0 && n == 7\n",n)fallthroughcase n > 5 || n == 5 :fmt.Printf("%d is > 5 || n == 5\n",n)//fallthroughdefault :fmt.Printf("%d is unavailable\n",n)}}

Third, for statement

3.1 Basic syntax

Before the loop starts, it executes and executes only one initialization statement, the initialization variable is a local variable, and the conditional statement is judged before each loop starts, and once the result is false, the loop body is exited, and the modifier statement is typically used to increase or decrease the counter and execute after the loop is finished.

 for initialisation; condition; post {        }

3.2 Example

package mainimport "fmt"func main() {for n := 1; n < 10; n++ {for m := 1; m <= n; m++ {fmt.Printf("%d * %d = %d\t",m,n,m*n)}fmt.Println()}}

Iv. Break and Continue

4.1 Break statement

A break is scoped to the most internal structure after the statement appears, and it can be used in any form for loops (counters, condition judgments, and so on). However, in a switch or SELECT statement, the break statement acts as a result of skipping the entire block of code and executing subsequent code.

package mainimport "fmt"func main() {for n := 1; n < 5; n++ {for m := 1; m < 5; m++ {if m == 2 {break}fmt.Printf("%d  VS  %d\n",n,m)}}}

4.2 Continue statements

Keyword continue the process of ignoring the remaining loop body and going directly to the next loop, the keyword continue can only be used in the For loop

package mainimport "fmt"func main() {for n := 1; n < 5; n++ {if n == 2 {continue}fmt.Printf("%d\n",n)}}

V. Goto STATEMENT

The for, switch, or SELECT statements can be used with identifiers in the form of a label (label), the name of the label is case-sensitive, and all uppercase letters are recommended for readability

package mainimport "fmt"func main() {for i := 0; i <= 5; i++ {for j := 0; j <= 5; j++ {if j == 4 {goto LABEL1}fmt.Printf("i is: %d, and j is: %d\n", i, j)}}LABEL1:}

In order to facilitate communication, I opened the public number and QQ Group, QQ Group: 291519319, like the technology to exchange it together

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.