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.