\n\u5bfc\u8bfb<\/td>\n | \u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709 break \u548c continue\uff0c\u90fd\u53ef\u4ee5\u642d\u914d\u5faa\u73af\u8bed\u53e5\u548c\u8df3\u8f6c\u8bed\u53e5\u4f7f\u7528\u3002\u8df3\u8f6c\u8bed\u53e5\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u4f1a\u975e\u5e38\u5b9e\u7528\uff0c\u4f46\u4e5f\u5f88\u5bb9\u6613\u51fa\u73b0\u4e00\u4e9b\u83ab\u540d\u5176\u5999\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u4f7f\u7528\u8d77\u6765\u8981\u66f4\u8c28\u614e\u4e9b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n <\/p>\n if-else<\/strong><\/div>\nif-else\u7279\u70b9\uff1a<\/strong><\/span><\/div>\n\u6761\u4ef6\u8bed\u53e5\u4e0d\u9700\u8981\u4f7f\u7528\u5c0f\u62ec\u53f7()\u5305\u8d77\u6765;<\/ol>\n\u82b1\u62ec\u53f7{}\u5fc5\u987b\u6709\uff0c\u5e76\u4e14\u5de6\u82b1\u62ec\u53f7{\u5fc5\u987b\u548cif\u6216else\u5728\u540c\u4e00\u884c;<\/ol>\n\u5728if\u4e4b\u540e\uff0c\u6761\u4ef6\u8bed\u53e5\u4e4b\u524d\u53ef\u4ee5\u6dfb\u52a0\u53d8\u91cf\u521d\u59cb\u5316\u8bed\u53e5\uff0c\u4f7f\u7528;\u5206\u9694\u3002<\/ol>\npackage main \r\n \r\nimport \"fmt\" \r\n \r\nfunc main() { \r\n if 7%2 == 0 { \r\n fmt.Println(\"7 is even\") \r\n } else { \r\n fmt.Println(\"7 is odd\") \/\/ 7 is odd \r\n } \r\n \r\n if 8%4 == 0 { \r\n fmt.Println(\"8 is divisible by 4\") \/\/ 8 is divisible by 4 \r\n } \r\n \r\n if num := 9; num < 0 { \r\n fmt.Println(num, \"is negative\") \r\n } else if num < 10 { \r\n fmt.Println(num, \"has 1 digit\") \/\/ 9 has 1 digit \r\n } else { \r\n fmt.Println(num, \"has multiple digits\") \r\n } \r\n} \r\n<\/pre>\nswitch<\/strong><\/div>\nswitch\u7279\u70b9\uff1a<\/strong><\/span><\/div>\n\u5de6\u82b1\u62ec\u53f7 { \u5fc5\u987b\u548c switch \u5728\u540c\u4e00\u884c;<\/ol>\n\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e0d\u9650\u5236\u5e38\u91cf\u6216\u8005\u6574\u6570;<\/ol>\nswitch \u540e\u53ef\u6dfb\u52a0\u53d8\u91cf\u521d\u59cb\u5316\u8bed\u53e5\uff0c\u4f7f\u7528 ; \u5206\u5272;<\/ol>\n\u53ef\u4ee5\u4e0d\u8bbe\u5b9a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u5728\u6b64\u79cd\u60c5\u51b5\u4e0b\uff0c\u6574\u4e2a switch \u7ed3\u6784\u4e0e\u591a\u4e2a if-else \u7684\u903b\u8f91\u4f5c\u7528\u7b49\u540c;<\/ol>\n\u5355\u4e2a case \u4e2d\u53ef\u4ee5\u51fa\u73b0\u591a\u4e2a\u7ed3\u679c\u9009\u9879;<\/ol>\n\u5728 case \u4e2d\u6dfb\u52a0 fallthrough \u5173\u952e\u5b57\uff0c\u4f1a\u7ee7\u7eed\u6267\u884c\u7d27\u8ddf\u7684\u4e0b\u4e00\u4e2a case\uff0c\u4e0d\u9700\u8981\u5224\u65ad case \u7684\u6761\u4ef6\u8bed\u53e5;<\/ol>\nswitch \u652f\u6301 default \u8bed\u53e5\uff0c\u5f53\u6240\u6709 case \u90fd\u4e0d\u6ee1\u8db3\u65f6\uff0c\u6267\u884c default \u8bed\u53e5\u3002<\/ol>\npackage main \r\n \r\nimport ( \r\n \"fmt\" \r\n \"time\" \r\n) \r\n \r\nfunc main() { \r\n i := 2 \r\n fmt.Print(\"write \", i, \" as \") \r\n switch i { \r\n case 1: \r\n fmt.Println(\"one\") \r\n case 2: \r\n fmt.Println(\"two\") \/\/ write 2 as two \r\n fallthrough \r\n case 3: \r\n fmt.Println(\"three\") \/\/ three \r\n case 4, 5, 6: \r\n fmt.Println(\"four, five, six\") \r\n } \r\n \r\n switch num := 9; num { \r\n case 1: \r\n fmt.Println(\"one\") \r\n default: \r\n fmt.Println(\"nine\") \/\/ nine \r\n } \r\n \r\n switch time.Now().Weekday() { \r\n case time.Saturday, time.Sunday: \r\n fmt.Println(\"it's the weekend\") \r\n default: \r\n fmt.Println(\"it's a weekday\") \/\/ it's a weekday \r\n } \r\n \r\n t := time.Now() \r\n switch { \r\n case t.Hour() < 12: \r\n fmt.Println(\"it's before noon\") \r\n default: \r\n fmt.Println(\"it's after noon\") \/\/ it's after noon \r\n } \r\n} \r\n<\/pre>\nfor<\/strong><\/div>\nfor\u7279\u70b9\uff1a<\/strong><\/span><\/div>\n\u6761\u4ef6\u8868\u8fbe\u5f0f\u4e0d\u9700\u8981\u4f7f\u7528\u5c0f\u62ec\u53f7 () \u5305\u8d77\u6765;<\/ol>\n\u82b1\u62ec\u53f7 {} \u5fc5\u987b\u6709\uff0c\u5e76\u4e14\u5de6\u82b1\u62ec\u53f7 { \u5fc5\u987b\u548c for \u5728\u540c\u4e00\u884c;<\/ol>\n\u652f\u6301 continue \u548c break\u3002<\/ol>\npackage main \r\n \r\nimport ( \r\n \"fmt\" \r\n) \r\n \r\nfunc main() { \r\n i := 1 \r\n \/\/ \u53ea\u6709\u6761\u4ef6 \r\n for i <= 3 { \r\n fmt.Println(i) \r\n i = i + 1 \r\n } \r\n \r\n \/\/ \u6709\u53d8\u91cf\u521d\u59cb\u5316\u548c\u6761\u4ef6 \r\n for j := 7; j <= 9; j++ { \r\n fmt.Println(j) \r\n } \r\n \r\n \/\/ \u6b7b\u5faa\u73af \r\n for { \r\n fmt.Println(\"loop\") \r\n break \r\n } \r\n \r\n \/\/ \u904d\u5386\u6570\u7ec4 \r\n a := [...]int{10, 20, 30, 40} \r\n for i := range a { \r\n fmt.Println(i) \r\n } \r\n for i, v := range a { \r\n fmt.Println(i, v) \r\n } \r\n \r\n \/\/ \u904d\u5386\u5207\u7247 \r\n s := []string{\"a\", \"b\", \"c\"} \r\n for i := range s { \r\n fmt.Println(i) \r\n } \r\n for i, v := range s { \r\n fmt.Println(i, v) \r\n } \r\n \r\n \/\/ \u904d\u5386\u5b57\u5178 \r\n m := map[string]int{\"a\": 10, \"b\": 20, \"c\": 30} \r\n for k := range m { \r\n fmt.Println(k) \r\n } \r\n for k, v := range m { \r\n fmt.Println(k, v) \r\n } \r\n} \r\n<\/pre>\ngoto\uff0cbreak\uff0ccontinue<\/strong><\/div>\ngoto \u7279\u70b9\uff1a<\/strong><\/span><\/div>\n\u53ea\u80fd\u5728\u51fd\u6570\u5185\u8df3\u8f6c\uff0c\u9700\u8981\u914d\u5408\u6807\u7b7e\u4e00\u8d77\u4f7f\u7528;<\/ol>\n\u4e0d\u80fd\u8df3\u8fc7\u5185\u90e8\u53d8\u91cf\u58f0\u660e\u8bed\u53e5;<\/ol>\n\u53ea\u80fd\u8df3\u5230\u540c\u7ea7\u4f5c\u7528\u57df\u6216\u8005\u4e0a\u5c42\u4f5c\u7528\u57df\u5185\uff0c\u4e0d\u80fd\u8df3\u5230\u5185\u90e8\u4f5c\u7528\u57df\u5185\u3002<\/ol>\npackage main \r\n \r\nimport ( \r\n \"fmt\" \r\n) \r\n \r\nfunc main() { \r\n \/\/ \u8df3\u51fa\u5faa\u73af \r\n for i := 0; ; i++ { \r\n if i == 2 { \r\n goto L1 \r\n } \r\n fmt.Println(i) \r\n } \r\nL1: \r\n fmt.Println(\"Done\") \r\n \r\n \/\/ \u8df3\u8fc7\u53d8\u91cf\u58f0\u660e\uff0c\u4e0d\u5141\u8bb8 \r\n \/\/ goto L2 \r\n \/\/ j := 1 \r\n \/\/ L2: \r\n} \r\n<\/pre>\nbreak \u7279\u70b9\uff1a<\/strong><\/span><\/div>\n\u5355\u72ec\u4f7f\u7528\uff0c\u7528\u4e8e\u8df3\u51fa break \u5f53\u524d\u6240\u5728\u7684 for\u3001 switch\u3001 select \u8bed\u53e5\u7684\u6267\u884c;<\/ol>\n\u548c\u6807\u7b7e\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u8df3\u51fa\u6807\u7b7e\u6240\u6807\u8bc6\u7684 for\u3001 switch\u3001 select \u8bed\u53e5\u7684\u6267\u884c\uff0c\u53ef\u7528\u4e8e\u8df3\u51fa\u591a\u91cd\u5faa\u73af\uff0c\u4f46\u6807\u7b7e\u548c break \u5fc5\u987b\u5728\u540c\u4e00\u4e2a\u51fd\u6570\u5185\u3002<\/ol>\npackage main \r\n \r\nimport ( \r\n \"fmt\" \r\n) \r\n \r\nfunc main() { \r\n \/\/ break \u8df3\u8f6c\u5230\u6807\u7b7e\u5904\uff0c\u7136\u540e\u8df3\u8fc7 for \u5faa\u73af \r\nL3: \r\n for i := 0; ; i++ { \r\n for j := 0; ; j++ { \r\n if i >= 2 { \r\n break L3 \r\n } \r\n if j > 4 { \r\n break \r\n } \r\n fmt.Println(i, j) \r\n } \r\n } \r\n} \r\n<\/pre>\ncontinue \u7279\u70b9\uff1a<\/strong><\/span><\/div>\n\u5355\u72ec\u4f7f\u7528\uff0c\u7528\u4e8e\u8df3\u51fa continue \u5f53\u524d\u6240\u5728\u7684 for \u5faa\u73af\u7684\u672c\u6b21\u8fed\u4ee3;<\/ol>\n\u548c\u6807\u7b7e\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u8df3\u51fa\u6807\u7b7e\u6240\u6807\u8bc6\u7684 for \u8bed\u53e5\u7684\u672c\u6b21\u9009\u4ee3\uff0c\u4f46\u6807\u7b7e\u548c continue \u5fc5\u987b\u5728\u540c\u4e00\u4e2a\u51fd\u6570\u5185\u3002<\/ol>\npackage main \r\n \r\nimport ( \r\n \"fmt\" \r\n) \r\n \r\nfunc main() { \r\n \/\/ continue \u8df3\u8f6c\u5230\u6807\u7b7e\u5904\uff0c\u7136\u540e\u6267\u884c i++ \r\nL4: \r\n for i := 0; ; i++ { \r\n for j := 0; j < 6; j++ { if i > 4 { \r\n break L4 \r\n } \r\n if i >= 2 { \r\n continue L4 \r\n } \r\n if j > 4 { \r\n continue \r\n } \r\n fmt.Println(i, j) \r\n } \r\n } \r\n} \r\n<\/pre>\n\u603b\u7ed3<\/strong><\/div>\n\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\uff0c\u5206\u522b\u662f\u6761\u4ef6\u8bed\u53e5\uff0c\u9009\u62e9\u8bed\u53e5\uff0c\u5faa\u73af\u8bed\u53e5\u548c\u8df3\u8f6c\u8bed\u53e5\u3002<\/p>\n \u6761\u4ef6\u8bed\u53e5\uff1a \u5bf9\u5e94\u5173\u952e\u8bcd if\uff0celse \u548c else if;<\/ol>\n\u9009\u62e9\u8bed\u53e5\uff1a \u5bf9\u5e94\u5173\u952e\u8bcd switch\uff0ccase\uff0cfallthrough \u548c default;<\/ol>\n\u5faa\u73af\u8bed\u53e5\uff1a \u5bf9\u5e94\u5173\u952e\u8bcd for \u548c range;<\/ol>\n\u8df3\u8f6c\u8bed\u53e5\uff1a \u5bf9\u5e94\u5173\u952e\u8bcd goto\u3002<\/ol>\n\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u6709 break \u548c continue\uff0c\u90fd\u53ef\u4ee5\u642d\u914d\u5faa\u73af\u8bed\u53e5\u548c\u8df3\u8f6c\u8bed\u53e5\u4f7f\u7528\u3002\u8df3\u8f6c\u8bed\u53e5\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u4f1a\u975e\u5e38\u5b9e\u7528\uff0c\u4f46\u4e5f\u5f88\u5bb9\u6613\u51fa\u73b0\u4e00\u4e9b\u83ab\u540d\u5176\u5999\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u4f7f\u7528\u8d77\u6765\u8981\u66f4\u8c28\u614e\u4e9b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u6761\u4ef6\u8bed\u53e5\u4e0d\u9700\u8981\u4f7f\u7528\u5c0f\u62ec\u53f7()\u5305\u8d77\u6765; \u82b1\u62ec\u53f7{}\u5fc5\u987b\u6709\uff0c\u5e76\u4e14\u5de6\u82b1\u62ec\u53f7{\u5fc5\u987b\u548cif\u6216else\u5728\u540c\u4e00\u884c; \u5728if\u4e4b […]<\/p>\n","protected":false},"author":1329,"featured_media":224131,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-224128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=224128"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224128\/revisions"}],"predecessor-version":[{"id":224132,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224128\/revisions\/224132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/224131"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=224128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=224128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=224128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} |