Go Language fourth day

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

5.0 Control Structure

This chapter is very simple, the program can not be separated from the control and loop statements, according to the requirements of each language to do, nothing special, let's see what the Go branch and loop is

The left brace after the keyword if and else { must be on the same line as the keyword, and if you use the ELSE-IF structure, the closing brace of the preceding block } must be on the same line as the ELSE-IF keyword. These two rules are mandated by the compiler.

If condition

{//do something

}

If condition

{

Do something

} else {

Do something

}

If condition

{

Do something

} else {

Do something

}

If initialization;condition{

Do something

}

Switch structure is more flexible than C and Java

Like what:

Switch VAR1 {

Case VAL1: ...

Case VAL2: ...

Default: ...

}

Var1 can be of any type, as long as it is the same type.

If you test multiple conditions can be separated by commas, such as case V1,V2,V3:

There is no need to use break to exit the code block, if there is a case to satisfy, automatically exit, if you want to continue to execute the following case, add the Fallthrough keyword.

Case V1:

....

Fallthrough

Switch can also make multiple branches more intuitive

Switch {

Case Condition1: ...

Case Condition2: ...

Default:.

.}

It's OK

Swtich Initialization {

Case VAL1: ...

Case VAL2: ...

Default: ...

}

Two examples

Switch {

Case I < 0:F1 ()

case i = = 0:F2 ()

case i > 0:f3 ()

}

Switch Result: = Calculate (); {

Case Result < 0: ...

Case result > 0: ...

Default://0

}

The cycle will see tomorrow.

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.