Site icon C1CTech

Kotlin when Expression

<p class&equals;"p1">In this article&comma; you will learn about <span style&equals;"color&colon; &num;0000ff&semi;"><strong>when<&sol;strong><&sol;span> expression in Kotlin with the help of various examples&period;<&sol;p>&NewLine;<p class&equals;"p1">In Kotlin&comma; <span style&equals;"color&colon; &num;0000ff&semi;"><b>when<&sol;b><&sol;span> replaces the <strong><span style&equals;"color&colon; &num;0000ff&semi;">switch<&sol;span><&sol;strong> operator of other languages like Java&comma; C&comma; C&plus;&plus;&comma; etc&period;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>when<&sol;strong><&sol;span> matches its argument against all branches sequentially until some branch condition is satisfied&period;<&sol;p>&NewLine;<p class&equals;"p2">After the first match found&comma; it reaches to end of the <span style&equals;"color&colon; &num;008000&semi;"><strong>when<&sol;strong><&sol;span> block and executes the code next to when block&period;<&sol;p>&NewLine;<p class&equals;"p1">In case&comma; if none of the branch conditions are satisfied then it will execute the <strong><span style&equals;"color&colon; &num;008000&semi;">else<&sol;span><&sol;strong> branch&period;<&sol;p>&NewLine;<p class&equals;"p2">Unlike switch cases in java or any other programming language&comma; we do not require <span style&equals;"color&colon; &num;008000&semi;"><strong>break<&sol;strong><&sol;span> statement at the end of each case&period;<&sol;p>&NewLine;<p class&equals;"p1">In Kotlin&comma; <span style&equals;"color&colon; &num;0000ff&semi;"><b>when <&sol;b><span style&equals;"color&colon; &num;000000&semi;">can<&sol;span><&sol;span> be used in two ways&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li><span style&equals;"color&colon; &num;000000&semi;">when as a statement<&sol;span><&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;000000&semi;">when as an expression<&sol;span><&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Using <span style&equals;"color&colon; &num;0000ff&semi;">when<&sol;span> as a Statement<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">Using <strong><span style&equals;"color&colon; &num;008000&semi;">when<&sol;span><&sol;strong> as a statement&comma; the values&sol;conditions of all individual branches are compared sequentially with the <strong><span style&equals;"color&colon; &num;008000&semi;">when<&sol;span><&sol;strong> argument and execute the corresponding branch where condition matches&period;<&sol;p>&NewLine;<p class&equals;"p1">If none of the branches satisfied with the condition then it will execute the <span style&equals;"color&colon; &num;008000&semi;"><strong>else<&sol;strong><&sol;span> branch&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><b><span style&equals;"color&colon; &num;0000ff&semi;">Note&colon;<&sol;span> when<&sol;b><&sol;span> can be used as a statement with or without <strong><span style&equals;"color&colon; &num;008000&semi;">else <&sol;span><&sol;strong>branch&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example <&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon;Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;getting input from user<&sol;strong><&sol;span>&NewLine; var reader &equals; Scanner&lpar;System&period;&grave;in&grave;&rpar;&NewLine; print&lpar;"Enter a number between 1 to 7&colon; "&rpar;&NewLine; var day &equals; reader&period;nextInt&lpar;&rpar;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;when statement<&sol;strong><&sol;span>&NewLine; when &lpar;day&rpar; &lbrace;&NewLine; 1 -&gt&semi; println&lpar;"Monday"&rpar;&NewLine; 2 -&gt&semi; println&lpar;"Tuesday"&rpar;&NewLine; 3 -&gt&semi; println&lpar;"Wednesday"&rpar;&NewLine; 4 -&gt&semi; println&lpar;"Thursday"&rpar;&NewLine; 5 -&gt&semi; println&lpar;"Friday"&rpar;&NewLine; 6 -&gt&semi; println&lpar;"Saturday"&rpar;&NewLine; 7 -&gt&semi; println&lpar;"Sunday"&rpar;&NewLine; else -&gt&semi; println&lpar;"Invalid day"&rpar;&NewLine; &rcub; &NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>Enter a number between 1 to 7&colon; 5&NewLine;Friday<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong> <&sol;span>If you want to give more than one statement inside a branch then you must enclose it inside curly braces&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma; <span style&equals;"color&colon; &num;000080&semi;"><strong>when day &equals; 5&colon; <&sol;strong><&sol;span><&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">the value <strong>5<&sol;strong> is checked&comma; yields true and <span style&equals;"color&colon; &num;008000&semi;"><b>&OpenCurlyDoubleQuote;Friday”<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><strong>  when x&equals;8&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1">none of the branches satisfied with the condition&comma; execute <span style&equals;"color&colon; &num;008000&semi;"><strong>else<&sol;strong><&sol;span> branch and <span style&equals;"color&colon; &num;008000&semi;"><b>&OpenCurlyDoubleQuote;Invalid day”<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Using <span style&equals;"color&colon; &num;0000ff&semi;">when<&sol;span> as an Expression<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">Using <span style&equals;"color&colon; &num;0000ff&semi;"><strong>when<&sol;strong><&sol;span> as an expression or <span style&equals;"color&colon; &num;0000ff&semi;"><strong>when expression<&sol;strong><&sol;span>&comma; returns a value&comma; that we can store it in a variable and use it&period;<&sol;p>&NewLine;<p class&equals;"p1">If when is used as an expression&comma; the value of the satisfied branch becomes the value of the overall expression&period;<&sol;p>&NewLine;<p class&equals;"p1">If none of the branch conditions are satisfied with the argument&comma; the <span style&equals;"color&colon; &num;008000&semi;"><strong>else<&sol;strong><&sol;span> branch is executed&period;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon;<&sol;strong> <&sol;span>It is mandatory to use <strong><span style&equals;"color&colon; &num;008000&semi;">else<&sol;span><&sol;strong> branch while using when as an expression&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example <&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &NewLine; &sol;&sol;getting input from user<&sol;strong><&sol;span>&NewLine; var reader &equals; Scanner&lpar;System&period;&grave;in&grave;&rpar;&NewLine; print&lpar;"Enter a number between 1 to 7&colon; "&rpar;&NewLine; var day &equals; reader&period;nextInt&lpar;&rpar;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &NewLine; &sol;&sol;when expression<&sol;strong><&sol;span>&NewLine; var dayName &equals; when &lpar;day&rpar; &lbrace;&NewLine; 1 -&gt&semi; "Monday"&NewLine; 2 -&gt&semi; "Tuesday"&NewLine; 3 -&gt&semi; "Wednesday"&NewLine; 4 -&gt&semi; "Thursday"&NewLine; 5 -&gt&semi; "Friday"&NewLine; 6 -&gt&semi; "Saturday"&NewLine; 7 -&gt&semi; "Sunday"&NewLine; else -&gt&semi; println&lpar;"Invalid day"&rpar;&NewLine;&NewLine; &rcub;&NewLine; println&lpar;"The Name of the day of the week is &colon; &dollar;dayName"&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>Enter a number between 1 to 7&colon; 4&NewLine;The Name of the day of the week is &colon; Thursday<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example explained<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma; <span style&equals;"color&colon; &num;000080&semi;"><strong>when day &equals; 4&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">the value <strong>4<&sol;strong> is checked&comma; yields true and <span style&equals;"color&colon; &num;008000&semi;"><b>&OpenCurlyDoubleQuote;Thursday”<&sol;b><&sol;span> is assigned to <strong><span style&equals;"color&colon; &num;008000&semi;">dayName<&sol;span><&sol;strong> variable and gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><strong>  when x&equals;8&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1">none of the branches satisfied with the condition&comma; execute <span style&equals;"color&colon; &num;008000&semi;"><strong>else<&sol;strong><&sol;span> branch and <span style&equals;"color&colon; &num;008000&semi;"><b>&OpenCurlyDoubleQuote;Invalid day”<&sol;b><&sol;span> is assigned to <strong><span style&equals;"color&colon; &num;008000&semi;">dayName<&sol;span><&sol;strong> variable and gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;000000&semi;"><strong>There are different ways to use when block in Kotlin&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Combine two or more branch conditions with a comma <&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">We can use multiple branches in a single one separated by a comma&period;<&sol;p>&NewLine;<p class&equals;"p1">When common logic is shared by some branches then we can combine them in a single branch&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; &NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;getting input from user<&sol;strong><&sol;span>&NewLine; var reader &equals; Scanner&lpar;System&period;&grave;in&grave;&rpar;&NewLine; print&lpar;"Enter a number between 1 to 10&colon; "&rpar;&NewLine; var num &equals; reader&period;nextInt&lpar;&rpar;&NewLine; &NewLine; when &lpar;num&rpar; &lbrace;&NewLine; 2&comma; 4&comma; 6&comma; 8 -&gt&semi; println&lpar;"&dollar;num is an even number less than 10&period;"&rpar;&NewLine; 1&comma; 3&comma; 5&comma; 7&comma; 9 -&gt&semi; println&lpar;"&dollar;num is an odd number less than 10&period;"&rpar;&NewLine; else -&gt&semi; println&lpar;"Invalid number"&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>Enter a number between 1 to 10&colon; 5&NewLine;5 is an odd number less than 10&period;<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Check the input value in a range or not <&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">To check whether the argument passed in <span style&equals;"color&colon; &num;0000ff&semi;"><strong>when<&sol;strong><&sol;span> block exists in particular Range or not&comma; we can use <span style&equals;"color&colon; &num;008000&semi;"><b>in<&sol;b><&sol;span> or<strong><span style&equals;"color&colon; &num;008000&semi;"> &excl;in<&sol;span><&sol;strong> operator&period;<&sol;p>&NewLine;<p class&equals;"p1">If the argument lies in a particular range then <strong><span style&equals;"color&colon; &num;0000ff&semi;">in<&sol;span><&sol;strong> operator returns <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong> and if the argument does not lie in particular range then <strong><span style&equals;"color&colon; &num;0000ff&semi;">&excl;in<&sol;span><&sol;strong> returns <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine;&NewLine; var number &equals; 14&NewLine; when &lpar;number&rpar; &lbrace;&NewLine; in 1&period;&period;10 -&gt&semi; println&lpar;"Input is provided in the range 1 to 10"&rpar;&NewLine; in 11&period;&period;20 -&gt&semi; println&lpar;"Input is provided in the range 11 to 20"&rpar;&NewLine; &excl;in 1&period;&period;20 -&gt&semi; println&lpar;"Input is not provided in the range 1 to 20"&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>Input is provided in the range 11 to 20<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><b>Check given variable is of a certain type or not <&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">To check whether a value is of a particular type in runtime&comma; we can use <span style&equals;"color&colon; &num;0000ff&semi;"><b>is<&sol;b><&sol;span> or <span style&equals;"color&colon; &num;0000ff&semi;"><b>&excl;is<&sol;b><&sol;span> operator&period;<&sol;p>&NewLine;<p class&equals;"p1"> For example&comma; If the variable is <strong><span style&equals;"color&colon; &num;008000&semi;">Integer<&sol;span><&sol;strong> type then <span style&equals;"color&colon; &num;0000ff&semi;"><strong>is Int <&sol;strong><&sol;span>returns <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong> else return <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; var num&colon; Any &equals; <strong><span style&equals;"color&colon; &num;008000&semi;">"Welcome to C1CTech"&NewLine;<&sol;span><&sol;strong>&NewLine; when&lpar;num&rpar;&lbrace;&NewLine; is Int -&gt&semi; println&lpar;"It is an Integer"&rpar;&NewLine; is String -&gt&semi; println&lpar;"It is a String"&rpar;&NewLine; is Double -&gt&semi; println&lpar;"It is a Double"&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>It is a String<&sol;pre>&NewLine;<h4><&sol;h4>&NewLine;<h4 class&equals;"p2"><span style&equals;"color&colon; &num;000080&semi;"><strong>Using when as a replacement for an if-else-if chain <&sol;strong><&sol;span><&sol;h4>&NewLine;<p>We can use <strong><span style&equals;"color&colon; &num;008000&semi;">when<&sol;span><&sol;strong> as a replacement for <strong><span style&equals;"color&colon; &num;008000&semi;">if-else-if<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>If no argument is supplied then the branch conditions are simply boolean expressions&comma; and a branch is executed only when its condition is <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&colon;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>fun main&lpar;args&colon; Array&lt&semi;String&gt&semi;&rpar; &lbrace;&NewLine; &NewLine; val marks &equals; 65&NewLine;&NewLine; when &lbrace;&NewLine; marks &gt&semi;&equals; 80 -&gt&semi; println&lpar;"You got A&plus; grade"&rpar;&NewLine; marks &gt&semi;&equals; 75 -&gt&semi; println&lpar;"You got A grade"&rpar;&NewLine; marks &gt&semi;&equals; 60 -&gt&semi; println&lpar;"You got B grade"&rpar;&NewLine; marks &gt&semi;&equals; 45 -&gt&semi; println&lpar;"You got C grade"&rpar;&NewLine; marks &gt&semi;&equals; 35 -&gt&semi; println&lpar;"You got D grade"&rpar;&NewLine; else -&gt&semi; println&lpar;"You are fail"&rpar;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>You got B grade<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version