{"id":60,"date":"2017-10-26T15:44:38","date_gmt":"2017-10-26T06:44:38","guid":{"rendered":"http:\/\/oksisi2.ddns.net:8092\/wordpress\/?p=60"},"modified":"2017-10-26T15:44:38","modified_gmt":"2017-10-26T06:44:38","slug":"kotlin-basics-%ec%a0%9c%ec%96%b4%eb%ac%b8","status":"publish","type":"post","link":"https:\/\/charlezz.com\/?p=60","title":{"rendered":"[Kotlin Basics] \uc81c\uc5b4\ubb38"},"content":{"rendered":"<h1>\uc81c\uc5b4\ubb38 : if, when, for, while<\/h1>\n<h2>If \ubb38\ubc95<\/h2>\n<p>\ucf54\ud2c0\ub9b0\uc5d0\uc11c\ub294 if \ub294 \ud45c\ud604\uc911\ud558\ub098\ub85c \uac12\uc744 \ud558\ub098 \ub9ac\ud134\ud558\uac8c \ub41c\ub2e4. \uadf8\ub7ec\ubbc0\ub85c \uc0bc\ud56d\uc5f0\uc0b0\uc790\ub294 \uc874\uc7ac\ud558\uc9c0 \uc54a\ub294\ub2e4. \uc65c\ub0d0\ud558\uba74 if\ubb38\ubc95\uc774 \uadf8 \uc5ed\ud560\uc744 \ud558\uace0 \uc788\uc73c\ub2c8\uae4c.<br \/>\n&nbsp;<\/p>\n<pre>\/\/ Traditional usage\nvar max = a\nif (a &lt; b) max = b\n\/\/ With else\nvar max: Int\nif (a &gt; b) {\n max = a\n} else {\n max = b\n}\n\/\/ As expression\nval max = if (a &gt; b) a else b<\/pre>\n<p>\uc704 \ud45c\ud604\uc740 \uc774\ub807\uac8c\ub3c4 \ud45c\ud604\ud560 \uc218 \uc788\ub2e4.<\/p>\n<pre>val max = if (a &gt; b) {\n print(\"Choose a\")\n a\n} else {\n print(\"Choose b\")\n b\n}<\/pre>\n<p>\ubcc0\uc218\uc5d0 \uac12\uc744 \ub300\uc785\ud560 \ubaa9\uc801\uc73c\ub85c if\ubb38\uc744 \uc0ac\uc6a9\ud560 \uacbd\uc6b0 \ubc18\ub4dc\uc2dc else \ubb38\uc774 \ud3ec\ud568\ub418\uc5b4\uc788\uc5b4\uc57c\ud55c\ub2e4.<\/p>\n<h2>when \ubb38\ubc95<\/h2>\n<p>when\uc740 switch\ubb38\uc744 \ub300\uccb4\ud55c\ub2e4.<\/p>\n<pre>when (x) {\n 1 -&gt; print(\"x == 1\")\n 2 -&gt; print(\"x == 2\")\n else -&gt; { \/\/ Note the block\n print(\"x is neither 1 nor 2\")\n }\n}<\/pre>\n<p>when\uc740 \uc778\uc790\ub4e4\uc744 \ubaa8\ub4e0 \ube0c\ub80c\uce58\uc640 \uc21c\uc11c\ub300\ub85c \ub9cc\uc871\ud560\ub54c\uae4c\uc9c0 \ube44\uad50\ud558\uac8c \ub41c\ub2e4. when \ub610\ud55c expression(\ud45c\ud604\uc2dd) \uc774\uc790 statement(\uc120\uc5b8\ubb38)\ub85c \uc774\uc6a9\uac00\ub2a5\ud558\ub2e4. \ub9cc\uc57d expression\uc73c\ub85c\ub9cc \uc4f0\uc778\ub2e4\uba74 \ube0c\ub79c\uce58\uc5d0\uc11c \ub9cc\uc871\ud55c \uac12\uc740 \uc804\uccb4 \ud45c\ud604\uc2dd\uc758 \uac12\uc774 \ub41c\ub2e4. \uc120\uc5b8\ubb38 \ud615\ud0dc\ub85c \uc0ac\uc6a9\ud560 \ub54c\uc5d0\ub294 \uac01 \ube44\uad50 \uad6c\ubb38\uc2dd\uc758 \uac12\uc740 \ubb34\uc2dc\ub41c\ub2e4.<br \/>\nelse \ub294 \uc544\ubb34\ub7f0 \uc870\uac74\ub3c4 \ub9cc\uc871\ud558\uc9c0 \ubabb\ud560 \ub54c \uc2e4\ud589\ub41c\ub2e4. when\uc774 \ub9cc\uc57d \ud45c\ud604\uc2dd\uc73c\ub85c \uc0ac\uc6a9\ub420 \ub54c, \ucef4\ud30c\uc77c\ub7ec \uc785\uc7a5\uc5d0\uc11c \ubaa8\ub4e0 \ube0c\ub79c\uce58\ub4e4\uc758 \uc870\uac74\ub4e4\uc774 \ud558\ub098\ub77c\ub3c4 \ub9cc\uc871\ud558\ub294 \uacbd\uc6b0\uac00 \ud798\ub4e4\ub2e4\uace0 \ud310\ub2e8\ub41c\ub2e4\uba74,\u00a0else\ub294 \ud544\uc218\uc801\uc73c\ub85c \uc0ac\uc6a9\ub418\uc5b4\uc838\uc57c \ud55c\ub2e4.<br \/>\n&nbsp;<br \/>\n\ub9cc\uc57d \ub9ce\uc740 \uc870\uac74\ub4e4\uc774 \uac19\uc740 \ubc29\ubc95\uc73c\ub85c \ub2e4\ub8e8\uc5b4 \uc9c4\ub2e4\uba74, \uc870\uac74\uc2dd\uc744 \ucf64\ub9c8\ub85c \uc5ee\uc744\uc218 \uc788\ub2e4.<\/p>\n<pre>when (x) {\n 0, 1 -&gt; print(\"x == 0 or x == 1\")\n else -&gt; print(\"otherwise\")\n}<\/pre>\n<p>\uc784\uc758\uc758 \ud45c\ud604\uc2dd\uc744 \uc4f8\uc218\ub3c4 \uc788\ub2e4.<\/p>\n<pre>when (x) {\n parseInt(s) -&gt; print(\"s encodes x\")\n else -&gt; print(\"s does not encode x\")\n}<\/pre>\n<p>\ubc94\uc704 \uc5f0\uc0b0\uc790\ub97c \uc774\uc6a9\ud558\uc5ec \uccb4\ud06c \ud560 \uc218\ub3c4 \uc788\ub2e4.<\/p>\n<pre>when (x) {\n in 1..10 -&gt; print(\"x is in the range\")\n in validNumbers -&gt; print(\"x is valid\")\n !in 10..20 -&gt; print(\"x is outside the range\")\n else -&gt; print(\"none of the above\")\n}<\/pre>\n<p>\ub2e4\ub978 \uac00\ub2a5\uc131\uc740 \uac12\uc744 is \ub610\ub294 !is\ub85c \ud2b9\uc815\ud55c \uc790\ub8cc\ud615\uc744 \uccb4\ud06c\ud558\ub294\uac83\uc774\ub2e4. smart cast\ub54c\ubb38\uc5d0 \ubcc4\ub3c4\uc758 \uac80\uc0ac \uc5c6\uc774 \uc790\ub8cc\ud615\uc758 \uba54\uc18c\ub4dc\uc640 \ud504\ub85c\ud37c\ud2f0\uc5d0 \uc811\uadfc\ud560 \uc218 \uc788\ub2e4.<\/p>\n<pre>fun hasPrefix(x: Any) = when(x) {\n is String -&gt; x.startsWith(\"prefix\")\n else -&gt; false\n}<\/pre>\n<p>when\uc740 \ub610\ud55c if &#8211; else if \uc5f0\uacc4\uc758 \ub300\uccb4\ud615\uc73c\ub85c \uc0ac\uc6a9 \ub420 \uc218\ub3c4 \uc788\ub2e4. \ub9cc\uc57d \uc778\uc790\uac00 \uc5c6\ub2e4\uba74,\ube0c\ub79c\uce58 \uc870\uac74\uc740 \uac04\ub2e8\ud788 boolean \ud45c\ud604\uc2dd\uc774 \ub41c\ub2e4. \uadf8\ub9ac\uace0 \uc870\uac74\uc774 true\uc778 \ube0c\ub79c\uce58\uac00 \uc2e4\ud589\ub41c\ub2e4<\/p>\n<pre>when {\n x.isOdd() -&gt; print(\"x is odd\")\n x.isEven() -&gt; print(\"x is even\")\n else -&gt; print(\"x is funny\")\n}<\/pre>\n<p>for \ubc18\ubcf5<br \/>\nfor\ubb38\uc740 iterator\ub97c \uc81c\uacf5\ud558\ub294 \uc5b4\ub5a4 \ud0c0\uc785\uc758 \ub0b4\uc6a9\uc744 \uc21c\ucc28\uc801\uc73c\ub85c \ud0d0\ubc29\ud55c\ub2e4. c#\uc758 foreach\uc640\ub3c4 \uac19\ub2e4.<\/p>\n<pre>for (item in collection) print(item)\n<\/pre>\n<p>\uc911\uad04\ud638\ub85c \ubc14\ub514\ubd80\ubd84\uc744 \ub9cc\ub4e4\uc218\ub3c4 \uc788\ub2e4.<\/p>\n<pre>for (item: Int in ints) {\n \/\/ ...\n}<\/pre>\n<p>\uc704\uc5d0\uc11c \uc5b8\uae09\ud55c\uac83\ucc98\ub7fc for\ubb38\uc740 iterator\ub97c \uc81c\uacf5\ud558\ub294 \uac83\uc744 \uc21c\ud68c \ud55c\ub2e4. \uc608\ub97c\ub4e4\uba74<\/p>\n<ul>\n<li>iterator() \ud568\uc218\ub97c \uac16\uac70\ub098 \ub9ac\ud134\ud558\ub294 \uc790\ub8cc\ud615\uc758 \uacbd\uc6b0<\/li>\n<li>next()\ud568\uc218\ub97c \uac16\ub294 \uacbd\uc6b0<\/li>\n<li>hasNext()\ub97c \uac16\uac70\ub098 Boolean\ud615\uc744 \ub9ac\ud134\ud558\ub294 \uacbd\uc6b0<\/li>\n<\/ul>\n<p>\uc774\ub7ec\ud55c \ud568\uc218\ub4e4\uc774 \uc5f0\uc0b0\uc790\ub85c\uc368 \ud544\uc694\ud558\ub2e4.<br \/>\nfor\ubb38\uc740 iterator \uac1d\uccb4\ub97c \uc0dd\uc131\ud558\uc9c0 \ubabb\ud558\ub294 \ubc30\uc5f4\uc5d0 \ub300\ud574 \uc778\ub371\uc2a4 \uae30\ubc18\uc758 \ubc18\ubcf5\uc744 \uc218\ud589\ud55c\ub2e4<br \/>\n\ub9cc\uc57d \ubc30\uc5f4\uc774\ub098 \uc778\ub371\uc2a4\ub97c \uac16\ub294 \ub9ac\uc2a4\ud2b8\ub97c \ubc18\ubcf5\uc218\ud589\ud558\uace0 \uc2f6\ub2e4\uba74 \uc774\ub807\uac8c \ud558\uba74 \ub41c\ub2e4.<\/p>\n<pre>for (i in array.indices) {\n print(array[i])\n}<\/pre>\n<p>\ubc94\uc704\ub97c \ud1b5\ud55c \uc21c\ud658\uc740\u00a0\ucd5c\uc801\ud654\ub41c \uad6c\ud604\uc73c\ub85c \ubcc4\ub3c4\uc758 \uc624\ube0c\uc81d\ud2b8 \uc0dd\uc131 \uc5c6\uc774 \ucef4\ud30c\uc77c\ub41c\ub2e4.<br \/>\n\ub300\uc548\uc73c\ub85c\ub294 withIndex \ub77c\uc774\ube0c\ub7ec\ub9ac \ud568\uc218\ub97c \uc0ac\uc6a9\ud560 \uc218 \uc788\ub2e4.<\/p>\n<pre>for ((index, value) in array.withIndex()) {\n println(\"the element at $index is $value\")\n}<\/pre>\n<h2>While \ubc18\ubcf5<\/h2>\n<p>while \uadf8\ub9ac\uace0 do..while\uc740 \ubcf4\ud1b5 \ub2e4\ub978 \uc5b8\uc5b4\ub4e4\ucc98\ub7fc \ub3d9\uc791\ud55c\ub2e4.<\/p>\n<pre>while (x &gt; 0) {\n x--\n}\ndo {\n val y = retrieveData()\n} while (y != null) \/\/ y is visible here!<\/pre>\n<h2>\ubc18\ubcf5\ubb38\uc5d0\uc11c\uc758 break \uc640 continue<\/h2>\n<p>\ucf54\ud2c0\ub9b0\uc740 \uc804\ud1b5\uc73c\ub85c \uc0ac\uc6a9\ub418\uace0 \uc788\ub294 break\uc640 continue \uc5f0\uc0b0\uc790\ub97c \uc81c\uacf5\ud55c\ub2e4.<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc81c\uc5b4\ubb38 : if, when, for, while If \ubb38\ubc95 \ucf54\ud2c0\ub9b0\uc5d0\uc11c\ub294 if \ub294 \ud45c\ud604\uc911\ud558\ub098\ub85c \uac12\uc744 \ud558\ub098 \ub9ac\ud134\ud558\uac8c \ub41c\ub2e4. \uadf8\ub7ec\ubbc0\ub85c \uc0bc\ud56d\uc5f0\uc0b0\uc790\ub294 \uc874\uc7ac\ud558\uc9c0 \uc54a\ub294\ub2e4. \uc65c\ub0d0\ud558\uba74 if\ubb38\ubc95\uc774 \uadf8 \uc5ed\ud560\uc744 \ud558\uace0 \uc788\uc73c\ub2c8\uae4c. &nbsp; \/\/ Traditional usage var max = a if (a &lt; b) max = b \/\/ With else var max: Int if (a &gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[5],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/60"}],"collection":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=60"}],"version-history":[{"count":0,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions"}],"wp:attachment":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}