{"id":560,"date":"2023-02-25T19:05:13","date_gmt":"2023-02-25T11:05:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=560"},"modified":"2023-04-29T20:36:03","modified_gmt":"2023-04-29T12:36:03","slug":"swift-control-flow-and-control-structure-description-if-switch-for-while","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-control-flow-and-control-structure-description-if-switch-for-while\/","title":{"rendered":"Swift &#8211; \u63a7\u5236\u6d41\u53ca\u63a7\u5236\u7ed3\u6784\u8bf4\u660e\uff08if\uff0cswitch\uff0cfor\uff0cwhile\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h2>if \u8bed\u53e5<\/h2>\n<pre><code class=\"language-swift\">if count &gt;= 3 {\n    print(&quot;yes&quot;)\n} else {\n    print(&quot;no&quot;)\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>switch \u8bed\u53e5<\/h2>\n<p>\uff081\uff09Swift\u4e2d\u4e0d\u9700\u8981\u5728case\u5757\u4e2d\u663e\u793a\u5730\u4f7f\u7528break\u8df3\u51faswitch\u3002\u5982\u679c\u60f3\u8981\u5b9e\u73b0C\u98ce\u683c\u7684\u843d\u5165\u7279\u6027\uff0c\u53ef\u4ee5\u7ed9\u9700\u8981\u7684case\u5206\u652f\u63d2\u5165fallthrough\u8bed\u53e5<\/p>\n<pre><code class=\"language-swift\">let fruit = &quot;apple&quot;\nswitch fruit {\n    case &quot;apple&quot;:\n        print(&quot;good&quot;)\n        fallthrough\n    case &quot;banana&quot;,&quot;orange&quot;:\n        print(&quot;great&quot;)\n    default:\n        print(&quot;bad&quot;)\n}<\/code><\/pre>\n<p>\uff082\uff09case\u5206\u652f\u8fd8\u53ef\u4ee5\u8fdb\u884c\u533a\u95f4\u5339\u914d<\/p>\n<pre><code class=\"language-swift\">let age = 5\nswitch age {\n    case 0...11:\n        print(&quot;\u6b63\u592a&quot;)\n    case 12...30:\n        print(&quot;\u5c11\u5e74&quot;)\n    default:\n        print(&quot;\u5927\u53d4&quot;)\n}<\/code><\/pre>\n<p>\uff083\uff09case\u5206\u652f\u540c\u6837\u652f\u6301\u5355\u4fa7\u533a\u95f4\u5339\u914d<\/p>\n<pre><code class=\"language-swift\">let num = -5\nswitch num {\ncase ..&lt;0:\n    print(&quot;\u8d1f\u6570&quot;)\ncase 0:\n    print(&quot;0&quot;)\ncase 0...:\n    print(&quot;\u6b63\u6570&quot;)\ndefault:\n    print(&quot;\u672a\u77e5&quot;)\n}<\/code><\/pre>\n<p>\uff084\uff09\u4f7f\u7528\u5143\u7ec4\u5339\u914d\uff08\u5224\u65ad\u5c5e\u4e8e\u54ea\u4e2a\u8c61\u9650\uff09<\/p>\n<pre><code class=\"language-swift\">let point = (2,2)\nswitch point {\n    case (0,0):\n        print(&quot;\u5750\u6807\u5728\u539f\u70b9&quot;)\n    case (_,0):\n        print(&quot;\u5750\u6807\u5728x\u8f74\u4e0a&quot;)\n    case (0,_):\n        print(&quot;\u5750\u6807\u5728y\u8f74\u4e0a&quot;)\n    case (-3...3, -3...3):\n        print(&quot;\u5750\u6807\u5728\u957f\u5bbd\u4e3a6\u7684\u6b63\u65b9\u5f62\u5185&quot;)\n    default:\n        print(&quot;\u5728\u4ec0\u4e48\u5730\u65b9&quot;)\n}<\/code><\/pre>\n<p>\uff085\uff09case\u4e2d\u8fd8\u53ef\u4ee5\u4f7f\u7528where\u5173\u952e\u5b57\u6765\u505a\u989d\u5916\u7684\u5224\u65ad\u6761\u4ef6<\/p>\n<pre><code class=\"language-swift\">var height = 1.72\nswitch height{\n    case 1...3 where height == 1.72:\n        print(&quot;case 1&quot;)\n    case 1...3 where height == 2:\n        print(&quot;case 2&quot;)\n    default:\n        print(&quot;default&quot;)\n}<\/code><\/pre>\n<h2>for \u5faa\u73af\u8bed\u53e5<\/h2>\n<p>\uff081\uff09for\u6761\u4ef6\u9012\u589e\u5faa\u73af<\/p>\n<p>\u6ce8\u610f\uff1a\u8fd9\u79cdC\u8bed\u8a00\u98ce\u683c\u7684for\u5faa\u73af\u8bed\u6cd5\u5df2\u5728Swift3\u4e2d\u88ab\u5e9f\u9664\uff0c\u5efa\u8bae\u4f7f\u7528\u4e0b\u9762\u7684for-in\uff0cfor-each\u5199\u6cd5<\/p>\n<pre><code class=\"language-swift\">\/\/\u5df2\u5f03\u7528\n\/\/for var i=1; i&lt;100; i++ {\n\/\/  print(&quot;\\(i)&quot;)\n\/\/}<\/code><\/pre>\n<p>\uff082\uff09for-in\u5faa\u73af<\/p>\n<pre><code class=\"language-swift\">for i in 1..&lt;100 {\n    print(&quot;\\(i)&quot;)\n}\n\n\/\/\u904d\u5386\u6570\u7ec4\u5143\u7d20\nlet numbers = [1,2,4,7]\nfor num in numbers{\n    print(&quot;\\(num)&quot;)\n}\n\n\/\/\u904d\u5386\u5b57\u5178\nlet nameOfAge = [&quot;lily&quot;:18, &quot;Candy&quot;:24]\nfor (aName, iAge) in nameOfAge {\n    print(&quot;\\(aName) is \\(iAge)&quot;)\n}\n\n\/\/\u904d\u5386\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\nfor chare in &quot;hangge&quot;.characters {\n    print(chare)\n}<\/code><\/pre>\n<p>\uff083\uff09for-each\u5faa\u73af<\/p>\n<pre><code class=\"language-swift\">(1...10).forEach {\n    print($0)\n}<\/code><\/pre>\n<h2>while \u5faa\u73af\u8bed\u53e5<\/h2>\n<pre><code class=\"language-swift\">while i&lt;100 {\n    i += 1\n}\n\nrepeat {\n    i += 1\n} while i&lt;100<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 if \u8bed\u53e5 if count &gt;= 3 { print(&quot;yes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-560","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}