In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the characteristics of if-else, switch and for". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Process control is a term in the field of computer computing, which refers to the order in which individual instructions (or statements, subroutines) are run or evaluated when the program is running. Similar concepts exist in both declarative programming languages and functional programming languages.
If-else
If-else features:
Conditional statements do not need to be wrapped in parentheses ()
Curly braces {} must have, and left curly braces {must be on the same line as if or else
After if, variable initialization statements can be added before conditional statements, separated by;.
Package mainimport "fmt" func main () {if 7% 2 = = 0 {fmt.Println ("7 is even")} else {fmt.Println ("7 is odd") / / 7 is odd} if 8% 4 = = 0 {fmt.Println ("8 is divisible by 4") / / 8 is divisible by 4} if num: = 9 Num "is negative")} else if num "has 1 digit") / / 9 has 1 digit} else {fmt.Println (num, "has multiple digits")}} switch
Switch features:
Left curly braces {must be on the same line as switch
Conditional expressions do not restrict constants or integers
After switch, you can add variable initialization statements to use; split.
Conditional expressions may not be set, in which case the entire switch structure is equivalent to the logical function of multiple if-else
Multiple result options can appear in a single case
If you add the fallthrough keyword to case, you will continue to execute the next case. There is no need to judge the conditional statement of case.
Switch supports default statements and executes default statements when all case is not satisfied.
Package mainimport ("fmt"time") func main () {I: = 2 fmt.Print ("write", I, "as") switch I {case 1: fmt.Println ("one") case 2: fmt.Println ("two") / / write 2 as two fallthrough case 3: fmt.Println ("three") / / three case 4,5,6: fmt.Println ("four, five") Six ")} switch num: = 9 Num {case 1: fmt.Println ("one") default: fmt.Println ("nine") / / nine} switch time.Now (). Weekday () {case time.Saturday Time.Sunday: fmt.Println ("it's the weekend") default: fmt.Println ("it's a weekday") / / it's a weekday} t: = time.Now () switch {case t.Hour () s before noon ") default: fmt.Println (" it's after noon ") / / it's after noon}} for
For features:
Conditional expressions do not need to be wrapped in parentheses ()
Curly braces {} must have, and left curly braces {must be on the same line as for
Continue and break are supported.
Package mainimport ("fmt") func main () {I: = 1 / / only conditional for i for j: = 7 J for {fmt.Println ("loop") break} / / ergodic array a: = [...] int {10,20,30,40} for I: = range a {fmt.Println (I)} for I, v: = range a {fmt.Println (I, v)} / / ergodic slices s: = [] string {"a", "b" "c"} for I: = range s {fmt.Println (I)} for I, v: = range s {fmt.Println (I, v)} / / the ergodic dictionary m: = map [string] int {"a": 10, "b": 20, "c": 30} for k: = range m {fmt.Println (k)} for k, v: = range m {fmt.Println (k) V)} goto Break,continue
Goto features:
You can only jump within the function. You need to use it with the tag.
Internal variable declaration statements cannot be skipped
You can only jump to the peer scope or the upper scope, not the internal scope.
Package mainimport ("fmt") func main () {/ / jump out of the loop for I: = 0;; iTunes + {if I = = 2 {goto L1} fmt.Println (I)} L1: fmt.Println ("Done") / / skip variable declaration, do not allow / / goto L2 / / j: = 1 / / L2:}
Break features:
Used alone to jump out of the execution of for, switch, and select statements where break is currently located
Used together with the tag, it is used to jump out of the execution of the for, switch, and select statements identified by the tag, and can be used to jump out of multiple loops, but the tag and break must be in the same function.
Package mainimport ("fmt") func main () {/ / break jumps to the label, and then skips the for loop L3: for I: = 0;; for + {if I > = 2 {break L3} if j > 4 {break} fmt.Println (I, j)}
Continue features:
Used alone to jump out of this iteration of the for loop where continue is currently located
Used with the tag to jump out of this iteration of the for statement identified by the tag, provided that the tag and continue must be in the same function.
Package mainimport ("fmt") func main () {/ / continue jumps to the label and executes i++L4: for I: = 0; iTunes + {for j: = 0 J if i > 4 {break L4} if I > = 2 {continue L4} if j > 4 {continue} fmt.Println (I, j)}}
In addition, there are break and continue, which can be used with loop statements and jump statements. Jump statements can be very useful in some scenarios, but they are also prone to inexplicable problems, so they should be used more carefully.
This is the end of the introduction of "what are the characteristics of if-else, switch and for". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.