Process control, all in one place

Process control, all in one place

[[415477]]

Watching the Olympics recently has gotten my blood boiling. The Chinese Olympic athletes are simply amazing, not only in their performance, but also in their temperament, spirit, and energy. They are great in all aspects.

What moved me the most in this Olympics was seeing some older athletes achieve great results: Lü Xiaojun was 37, Ma Long was 32, Su Bingtian was 32, and Gong Lijiao won her first Olympic gold medal at the age of 32. Even athletes who are so limited by age can constantly break through themselves, let alone us? Are we still worried online every day that programmers will be optimized at the age of 35?

So don’t use age as an excuse for yourself. Don’t think that you can’t do this or that because you’re too old. Just do it.

if-else

Features:

  • Conditional statements do not need to be enclosed in parentheses ();
  • The curly braces {} must be present, and the left curly brace { must be on the same line as if or else;
  • After the if statement and before the conditional statement, you can add variable initialization statements, separated by ;.
  1. package main
  2.  
  3. import "fmt"  
  4.  
  5. func main() {
  6. if 7%2 == 0 {
  7. fmt.Println( "7 is even" )
  8. } else {
  9. fmt.Println( "7 is odd" ) // 7 is odd
  10. }
  11.  
  12. if 8%4 == 0 {
  13. fmt.Println( "8 is divisible by 4" ) // 8 is divisible by 4
  14. }
  15.  
  16. if num := 9; num < 0 {
  17. fmt.Println(num, "is negative" )
  18. } else if num < 10 {
  19. fmt.Println(num, "has 1 digit" ) // 9 has 1 digit
  20. } else {
  21. fmt.Println(num, "has multiple digits" )
  22. }
  23. }

switch

Features:

  • The left curly brace { must be on the same line as the switch;
  • Conditional expressions are not restricted to constants or integers;
  • You can add variable initialization statements after switch, using ; to separate;
  • The conditional expression can be omitted. In this case, the entire switch structure is equivalent to the logical function of multiple if-else statements.
  • Multiple result options can appear in a single case;
  • Adding the fallthrough keyword to a case will continue execution to the next case without judging the conditional statement of the case;
  • Switch supports default statements. When all cases are not satisfied, the default statement is executed.
  1. package main
  2.  
  3. import (
  4. "fmt"  
  5. "time"  
  6. )
  7.  
  8. func main() {
  9. i := 2
  10. fmt.Print( "write " , i, " as " )
  11. switch i {
  12. case 1:
  13. fmt.Println( "one" )
  14. case 2:
  15. fmt.Println( "two" ) // write 2 as two
  16. fallthrough
  17. case 3:
  18. fmt.Println( "three" ) // three
  19. case 4, 5, 6:
  20. fmt.Println( "four, five, six" )
  21. }
  22.  
  23. switch num := 9; num {
  24. case 1:
  25. fmt.Println( "one" )
  26. default :
  27. fmt.Println( "nine" ) // nine
  28. }
  29.  
  30. switch time .Now().Weekday() {
  31. case   time .Saturday, time .Sunday:
  32. fmt.Println( "it's the weekend" )
  33. default :
  34. fmt.Println( "it's a weekday" ) // it's a weekday
  35. }
  36.  
  37. t := time .Now()
  38. switch {
  39. case t.Hour () < 12 :
  40. fmt.Println( "it's before noon" )
  41. default :
  42. fmt.Println( "it's after noon" ) // it's after noon
  43. }
  44. }

for

Features:

  • The conditional expression does not need to be enclosed in parentheses ();
  • The curly braces {} must be present, and the left curly brace { must be on the same line as for;
  • Supports continue and break.
  1. package main
  2.  
  3. import (
  4. "fmt"  
  5. )
  6.  
  7. func main() {
  8. i := 1
  9. // Only condition
  10. for i <= 3 {
  11. fmt.Println(i)
  12. i = i + 1
  13. }
  14.  
  15. // There are variable initialization and conditions
  16. for j := 7; j <= 9; j++ {
  17. fmt.Println(j)
  18. }
  19.  
  20. // Infinite loop
  21. for {
  22. fmt.Println( "loop" )
  23. break
  24. }
  25.  
  26. // Traverse the array
  27. a := [...] int {10, 20, 30, 40}
  28. for i := range a {
  29. fmt.Println(i)
  30. }
  31. for i, v := range a {
  32. fmt.Println(i, v)
  33. }
  34.  
  35. // Traverse the slice
  36. s := []string{ "a" , "b" , "c" }
  37. for i := range s {
  38. fmt.Println(i)
  39. }
  40. for i, v := range s {
  41. fmt.Println(i, v)
  42. }
  43.  
  44. // Traverse the dictionary
  45. m := map[string] int { "a" : 10, "b" : 20, "c" : 30}
  46. for k := range m {
  47. fmt.Println(k)
  48. }
  49. for k, v := range m {
  50. fmt.Println(k, v)
  51. }
  52. }

goto, break, continue

Features of goto:

  • Can only jump within a function and needs to be used with a label;
  • Internal variable declaration statements cannot be skipped;
  • You can only jump to the same-level scope or the upper-level scope, but not to the inner scope.
  1. package main
  2.  
  3. import (
  4. "fmt"  
  5. )
  6.  
  7. func main() {
  8. // Break out of the loop
  9. for i := 0; ; i++ {
  10. if i == 2 {
  11. goto L1
  12. }
  13. fmt.Println(i)
  14. }
  15. L1:
  16. fmt.Println( "Done" )
  17.  
  18. // Skip variable declaration, not allowed
  19. // goto L2
  20. // j := 1
  21. // L2:
  22. }

break Features:

  • Used alone to break out of the execution of the for, switch, or select statement in which the break is currently located;
  • Used together with a label to jump out of the execution of the for, switch, or select statement identified by the label. It can be used to jump out of multiple loops, but the label and break must be in the same function.
  1. package main
  2.  
  3. import (
  4. "fmt"  
  5. )
  6.  
  7. func main() {
  8. // break jumps to the label and then skips the for loop
  9. L3:
  10. for i := 0; ; i++ {
  11. for j := 0; ; j++ {
  12. if i >= 2 {
  13. break L3
  14. }
  15. if j > 4 {
  16. break
  17. }
  18. fmt.Println(i, j)
  19. }
  20. }
  21. }

continue Features:

  • Used alone to jump out of the current iteration of the for loop in which continue is located;
  • Used together with a label to jump out of the current iteration of the for statement identified by the label, but the label and continue must be in the same function.
  1. package main
  2.  
  3. import (
  4. "fmt"  
  5. )
  6.  
  7. func main() {
  8. // continue jumps to the label and then executes i++
  9. L4:
  10. for i := 0; ; i++ {
  11. for j := 0; j < 6; j++ {
  12. if i > 4 {
  13. break L4
  14. }
  15. if i >= 2 {
  16. continue L4
  17. }
  18. if j > 4 {
  19. continue  
  20. }
  21. fmt.Println(i, j)
  22. }
  23. }
  24. }

Summarize

This article mainly introduces process control statements, namely conditional statements, selection statements, loop statements and jump statements.

  • Conditional statements: corresponding to the keywords if, else and else if;
  • Selection statement: corresponding to the keywords switch, case, fallthrough and default;
  • Loop statement: corresponds to the keywords for and range;
  • Jump statement: corresponds to the keyword goto.

In addition, there are break and continue, which can be used with loop statements and jump statements.

Jump statements are very useful in some scenarios, but they can easily cause some inexplicable problems, so use them with caution.

The mind map and source code in the article have been uploaded to GitHub, and students in need can download them by themselves.

Address: https://github.com/yongxinz/gopher/tree/main/sc

This article is reprinted from the WeChat public account "AlwaysBeta", which can be followed through the following QR code. To reprint this article, please contact the AlwaysBeta public account.

<<:  Ministry of Industry and Information Technology: my country's 5G mobile terminal connections reached 365 million, accounting for more than 80% of the world

>>:  5G is here, and so is mainstream adoption for industrial IoT startups

Recommend

Comparison of LPWAN technologies: Ten criteria for successful implementation

Low Power Wide Area Network (LPWAN) is the fastes...

Exploration of 5G and edge computing applications in the post-epidemic era

AT&T and many other leading wireless network ...

These seven points of network technology, weak current people must know

Weak current people have one thing in common: mos...

Problems that edge computing needs to solve urgently

At present, edge computing has been widely recogn...

CloudCone Easter Promotion: $15/year KVM-1GB/30GB/3TB/Los Angeles Data Center

CloudCone offers three special VPS packages for t...

In-depth interpretation of the principles and applications of HTTP/3

HTTP3 is the latest version of the HTTP protocol....

Ten major trends in the future of industrial Internet

In recent years, major countries around the world...

The role of gateways in computer networks

A gateway is a computer on a network that provide...

Mainstream IoT wireless technologies are here!

This article covers: Bluetooth, WiFi, BLE, Zigbee...

Should I switch to a Wi-Fi 6 router as the holidays approach?

If 2019 is the first year of Wi-Fi 6 commercializ...