{"id":1320,"date":"2019-03-07T12:52:33","date_gmt":"2019-03-07T07:22:33","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1320"},"modified":"2019-03-07T12:52:33","modified_gmt":"2019-03-07T07:22:33","slug":"how-use-when-expression-kotlin-programming-language","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/","title":{"rendered":"How to use \u201cwhen\u201d expression in Kotlin programming language?"},"content":{"rendered":"<p>\u201dwhen\u201d is nothing but \u2018Switch\u2019 statement in C and Java language.There is also default case but this is expressed with else in When block.There is no break statement.We can attach multiple cases as well as a range(like 1&#8230;20) in one case.Also we can attach multiple condition in one case.The appropriate example is written below-<\/p>\n<p>fun main( args: Array&lt;String&gt;){<\/p>\n<p>\/\/ when as expression<\/p>\n<p>val x=2<\/p>\n<p>when (x){<\/p>\n<p>0,1,4 -&gt; println(\u201cx is one\u201d)<\/p>\n<p>2 -&gt; {<\/p>\n<p>println(\u201cx is two\u201d)<\/p>\n<p>println(\u201cHello\u201d)<\/p>\n<p>}<\/p>\n<p>3 -&gt; println(\u201cx is three\u201d)<\/p>\n<p>in 5&#8230;20 -&gt; println(\u201cx is between 5 to 20\u201d)<\/p>\n<p>else{<\/p>\n<p>println(\u201cx is unknown\u201d)<\/p>\n<p>println(\u201cI don\u2019t know the value of x\u201d)<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>Output:<\/p>\n<p>x is two<\/p>\n<p>Hello<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u201dwhen\u201d is nothing but \u2018Switch\u2019 statement in C and Java language.There is also default case but this is expressed with else in When block.There is no break statement.We can attach multiple cases as well as a range(like 1&#8230;20) in one case.Also we can attach multiple condition in one case.The appropriate example is written below- fun [&hellip;]<\/p>\n","protected":false},"author":35,"featured_media":1351,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[94,5],"tags":[95,87],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use \u201cwhen\u201d expression in Kotlin programming language?<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use \u201cwhen\u201d expression in Kotlin programming language?\" \/>\n<meta property=\"og:description\" content=\"\u201dwhen\u201d is nothing but \u2018Switch\u2019 statement in C and Java language.There is also default case but this is expressed with else in When block.There is no break statement.We can attach multiple cases as well as a range(like 1&#8230;20) in one case.Also we can attach multiple condition in one case.The appropriate example is written below- fun [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-07T07:22:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/StringTemplateExpressions-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/StringTemplateExpressions-1.png\",\"width\":1280,\"height\":720,\"caption\":\"openweb kotlin development\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/\",\"name\":\"How to use \\u201cwhen\\u201d expression in Kotlin programming language?\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/#primaryimage\"},\"datePublished\":\"2019-03-07T07:22:33+00:00\",\"dateModified\":\"2019-03-07T07:22:33+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/990bda7c6fac1aa9c1ffde7745f42628\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/how-use-when-expression-kotlin-programming-language\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/990bda7c6fac1aa9c1ffde7745f42628\",\"name\":\"Arindam Kundu\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1eea674a264b13f5b7c5895e36503137?s=96&r=g\",\"caption\":\"Arindam Kundu\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1320"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1320"}],"version-history":[{"count":3,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1320\/revisions"}],"predecessor-version":[{"id":1352,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1320\/revisions\/1352"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1351"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}