“This is the ninth day of my participation in the August More Text Challenge. For more details, see August More Text Challenge
Conditional judgment if
-
Judgment is often needed in program development. Depending on the situation, the program executes in different branches. This is where you can use if
-
If stands for “if” in a program, if the condition is true, it’s executed, otherwise it’s not executed
-
In the compiler, parentheses can be written around the expression, but are usually omitted
// Use the if expression {} aloneCopy the code
- In the if syntax, all expressions end up as bool, and true means true
If alone
- If alone only affects its own block of code
func main(a) {
score := 65
if score >= 60 {
fmt.Println("Pass")}}Copy the code
- You can declare a variable in an if expression, and the scope of the variable can only be used in an if
func main(a) {
if score:=60; score>=60{
fmt.Println("Passed.")}}Copy the code
- Multiple individual IF’s have no effect on each other
func main(a) {
score := 65
if score >= 60 {
fmt.Println("Pass")}if score < 60 {
fmt.Println("Fail")}}Copy the code
Program debugging
- Program debugging, commonly known as :debug, through a specific means, a statement a statement to observe the process of program execution
- Debug is often used in error calls. When the result is not what you expect and the code is large,debug can find out which line is in trouble
- Program debugging step
- A red circle appears when you click before a line in the program
- Right click and select Debug ‘file name’
- Click the button or use the shortcut key to select: down to execute (F6), into the call (F5), up to jump out (F7) observe the program
if … The else structure
- If can be used alone or in combination with else, if it is mutually exclusive… The else structure
- if.. Else structure if the if is true then else
func main(a) {
i := 59
if i >= 60 {
fmt.Println("Pass")}else{
fmt.Println("Fail")}}Copy the code
If multiple nesting
- If each structure can be nested within each other
- In theory, if can be nested in many layers
- Examples of two layers of nested code
func main(a) {
score := 77
if score >= 60 {
if score >= 60 && score < 70 {
fmt.Println("Pass")}if score >= 70 && score < 80 {
fmt.Println("Medium")}if score >= 80 && score < 90 {
fmt.Println("Good")}if score >= 90 && score <= 100 {
fmt.Println("Good")}}else {
fmt.Println("Fail")}}Copy the code
if … else if …. The else structure
- This structure is used when multiple conditions need to be determined
- This structure is a whole. As soon as one of the conditions is true none of the rest of the judgment, immediately end
- We can’t have else
- Rewrite the nested code as follows
func main(a) {
score := 77
if score >= 90 {
fmt.Println("Good")}else if score >= 80 {
fmt.Println("Good")}else if score >= 70 {
fmt.Println("Medium")}else if score >= 60 {
fmt.Println("Pass")}else {
fmt.Println("Fail")}}Copy the code
Switch condition judgment
Switch Structure Introduction
- Switch is also a conditional statement
- Support for multiple writing, and if.. else if … The else structure is similar in function, but the details need more attention
- Basic Switch syntax
Switch [define variable;] [variable] {case conditions/concrete values: / / code case conditions/concrete values: default: / / code / / code}Copy the code
- Switch By default, only one case branch is executed from the top down
- The upper and lower positions of default are not affected. Default is executed only when all cases are invalid
Switch Usage (1)
- The switch structure can be used when a variable has a fixed number of values
func main(a) {
num := 16
switch num {
case 2:
fmt.Println("Two base")
case 8:
fmt.Println("8 hexadecimal")
case 10:
fmt.Println("Decimal")
case 16:
fmt.Println("Hex")
default:
fmt.Println("Incorrect content")
}
fmt.Println("End of procedure")}Copy the code
- The switch also supports defining variables in a conditional position with the valid range of the current switch
func main(a) {
switch num := 16; num {
case 2:
fmt.Println("Two base")
case 8:
fmt.Println("8 hexadecimal")
case 10:
fmt.Println("Decimal")
case 16:
fmt.Println("Hex")
default:
fmt.Println("Incorrect content")
}
fmt.Println("End of procedure")}Copy the code
Switch Usage (2)
- When the condition is a range rather than a fixed value
func main(a) {
score := 71
switch {
case score >= 90:
fmt.Println("Good")
case score >= 80:
fmt.Println("Good")
case score >= 70:
fmt.Println("Medium")
case score >= 60:
fmt.Println("Pass")
default:
fmt.Println("Fail")
}
fmt.Println("End of procedure")}Copy the code
Switch Usage (3)
- Case conditions support multiple values, each separated by commas
func main(a) {
month := 5
switch month {
case 1.3.5.7.8.10.12:
fmt.Println("31 days")
case 2:
fmt.Println("28 or 29 days.")
default:
fmt.Println("30 days")
}
fmt.Println("End of procedure")}Copy the code
Penetration and break
- A switch structure can only execute one case at most. Using fallthrough allows the next case/default to continue execution
func main(a) {
switch num := 1; num {
case 1:
fmt.Println("1")
fallthrough
case 2:
fmt.Println("2")
case 3:
fmt.Println("3")
fallthrough
case 4:
fmt.Println("4")
default:
fmt.Println("Not 1, 2, 3, 4")
}
fmt.Println("End of procedure")}Copy the code
- Break can be used in switches and loops to indicate an immediate end, regardless of how much code remains behind the current structure
func main(a) {
switch num := 1; num {
case 1:
fmt.Println("1")
break
fmt.Println("None of the code after break is executed.")
fallthrough
case 2:
fmt.Println("2")
case 3:
fmt.Println("3")
fallthrough
case 4:
fmt.Println("4")
default:
fmt.Println("Not 1, 2, 3, 4")
}
fmt.Println("End of procedure")}Copy the code