<p class="p1">In this article, you will learn about <span style="color: #0000ff;"><strong>when</strong></span> expression in Kotlin with the help of various examples.</p>
<p class="p1">In Kotlin, <span style="color: #0000ff;"><b>when</b></span> replaces the <strong><span style="color: #0000ff;">switch</span></strong> operator of other languages like Java, C, C++, etc.</p>
<p class="p1"><span style="color: #0000ff;"><strong>when</strong></span> matches its argument against all branches sequentially until some branch condition is satisfied.</p>
<p class="p2">After the first match found, it reaches to end of the <span style="color: #008000;"><strong>when</strong></span> block and executes the code next to when block.</p>
<p class="p1">In case, if none of the branch conditions are satisfied then it will execute the <strong><span style="color: #008000;">else</span></strong> branch.</p>
<p class="p2">Unlike switch cases in java or any other programming language, we do not require <span style="color: #008000;"><strong>break</strong></span> statement at the end of each case.</p>
<p class="p1">In Kotlin, <span style="color: #0000ff;"><b>when </b><span style="color: #000000;">can</span></span> be used in two ways:</p>
<ul class="ul1">
<li><span style="color: #000000;">when as a statement</span></li>
<li><span style="color: #000000;">when as an expression</span></li>
</ul>
<h3></h3>
<h3><strong><span style="color: #000080;">Using <span style="color: #0000ff;">when</span> as a Statement</span></strong></h3>
<p class="p1">Using <strong><span style="color: #008000;">when</span></strong> as a statement, the values/conditions of all individual branches are compared sequentially with the <strong><span style="color: #008000;">when</span></strong> argument and execute the corresponding branch where condition matches.</p>
<p class="p1">If none of the branches satisfied with the condition then it will execute the <span style="color: #008000;"><strong>else</strong></span> branch.</p>
<p><span style="color: #008000;"><b><span style="color: #0000ff;">Note:</span> when</b></span> can be used as a statement with or without <strong><span style="color: #008000;">else </span></strong>branch.</p>
<h4><span style="color: #0000ff;"><strong>Example </strong></span></h4>
<pre>fun main(args:Array<;String>;) {

 <span style="color: #008000;"><strong>//getting input from user</strong></span>
 var reader = Scanner(System.`in`)
 print("Enter a number between 1 to 7: ")
 var day = reader.nextInt()

<span style="color: #008000;"><strong> //when statement</strong></span>
 when (day) {
 1 ->; println("Monday")
 2 ->; println("Tuesday")
 3 ->; println("Wednesday")
 4 ->; println("Thursday")
 5 ->; println("Friday")
 6 ->; println("Saturday")
 7 ->; println("Sunday")
 else ->; println("Invalid day")
 } 
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>Enter a number between 1 to 7: 5
Friday</pre>
<p><span style="color: #0000ff;"><strong>Note:</strong> </span>If you want to give more than one statement inside a branch then you must enclose it inside curly braces.</p>
<h4><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<p class="p1">In the above example, <span style="color: #000080;"><strong>when day = 5: </strong></span></p>
<ul class="ul1">
<li class="li1">the value <strong>5</strong> is checked, yields true and <span style="color: #008000;"><b>“Friday”</b></span> gets printed.</li>
</ul>
<p class="p1"><span style="color: #000080;"><strong> when x=8:</strong></span></p>
<ul>
<li class="p1">none of the branches satisfied with the condition, execute <span style="color: #008000;"><strong>else</strong></span> branch and <span style="color: #008000;"><b>“Invalid day”</b></span> gets printed.</li>
</ul>
<h3><strong><span style="color: #000080;">Using <span style="color: #0000ff;">when</span> as an Expression</span></strong></h3>
<p class="p1">Using <span style="color: #0000ff;"><strong>when</strong></span> as an expression or <span style="color: #0000ff;"><strong>when expression</strong></span>, returns a value, that we can store it in a variable and use it.</p>
<p class="p1">If when is used as an expression, the value of the satisfied branch becomes the value of the overall expression.</p>
<p class="p1">If none of the branch conditions are satisfied with the argument, the <span style="color: #008000;"><strong>else</strong></span> branch is executed.</p>
<p class="p1"><span style="color: #0000ff;"><strong>Note:</strong> </span>It is mandatory to use <strong><span style="color: #008000;">else</span></strong> branch while using when as an expression.</p>
<h4><span style="color: #0000ff;"><strong>Example </strong></span></h4>
<pre>fun main(args: Array<;String>;) {
<span style="color: #008000;"><strong> 
 //getting input from user</strong></span>
 var reader = Scanner(System.`in`)
 print("Enter a number between 1 to 7: ")
 var day = reader.nextInt()
<span style="color: #008000;"><strong> 
 //when expression</strong></span>
 var dayName = when (day) {
 1 ->; "Monday"
 2 ->; "Tuesday"
 3 ->; "Wednesday"
 4 ->; "Thursday"
 5 ->; "Friday"
 6 ->; "Saturday"
 7 ->; "Sunday"
 else ->; println("Invalid day")

 }
 println("The Name of the day of the week is : $dayName")
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>Enter a number between 1 to 7: 4
The Name of the day of the week is : Thursday</pre>
<h4><strong><span style="color: #0000ff;">Example explained</span></strong></h4>
<p class="p1">In the above example, <span style="color: #000080;"><strong>when day = 4:</strong></span></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ul class="ul1">
<li class="li1">the value <strong>4</strong> is checked, yields true and <span style="color: #008000;"><b>“Thursday”</b></span> is assigned to <strong><span style="color: #008000;">dayName</span></strong> variable and gets printed.</li>
</ul>
<p class="p1"><span style="color: #000080;"><strong> when x=8:</strong></span></p>
<ul>
<li class="p1">none of the branches satisfied with the condition, execute <span style="color: #008000;"><strong>else</strong></span> branch and <span style="color: #008000;"><b>“Invalid day”</b></span> is assigned to <strong><span style="color: #008000;">dayName</span></strong> variable and gets printed.</li>
</ul>
<p> ;</p>
<p class="p1"><span style="color: #000000;"><strong>There are different ways to use when block in Kotlin:</strong></span></p>
<h4 class="p2"><span style="color: #000080;"><b>Combine two or more branch conditions with a comma </b></span></h4>
<p class="p1">We can use multiple branches in a single one separated by a comma.</p>
<p class="p1">When common logic is shared by some branches then we can combine them in a single branch.</p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {
 
<span style="color: #008000;"><strong> //getting input from user</strong></span>
 var reader = Scanner(System.`in`)
 print("Enter a number between 1 to 10: ")
 var num = reader.nextInt()
 
 when (num) {
 2, 4, 6, 8 ->; println("$num is an even number less than 10.")
 1, 3, 5, 7, 9 ->; println("$num is an odd number less than 10.")
 else ->; println("Invalid number")
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>Enter a number between 1 to 10: 5
5 is an odd number less than 10.</pre>
<h4></h4>
<h4 class="p2"><span style="color: #000080;"><b>Check the input value in a range or not </b></span></h4>
<p class="p1">To check whether the argument passed in <span style="color: #0000ff;"><strong>when</strong></span> block exists in particular Range or not, we can use <span style="color: #008000;"><b>in</b></span> or<strong><span style="color: #008000;"> !in</span></strong> operator.</p>
<p class="p1">If the argument lies in a particular range then <strong><span style="color: #0000ff;">in</span></strong> operator returns <strong><span style="color: #008000;">true</span></strong> and if the argument does not lie in particular range then <strong><span style="color: #0000ff;">!in</span></strong> returns <strong><span style="color: #008000;">true</span></strong>.</p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {

 var number = 14
 when (number) {
 in 1..10 ->; println("Input is provided in the range 1 to 10")
 in 11..20 ->; println("Input is provided in the range 11 to 20")
 !in 1..20 ->; println("Input is not provided in the range 1 to 20")
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>Input is provided in the range 11 to 20</pre>
<h4></h4>
<h4 class="p2"><span style="color: #000080;"><b>Check given variable is of a certain type or not </b></span></h4>
<p class="p1">To check whether a value is of a particular type in runtime, we can use <span style="color: #0000ff;"><b>is</b></span> or <span style="color: #0000ff;"><b>!is</b></span> operator.</p>
<p class="p1"> For example, If the variable is <strong><span style="color: #008000;">Integer</span></strong> type then <span style="color: #0000ff;"><strong>is Int </strong></span>returns <strong><span style="color: #008000;">true</span></strong> else return <strong><span style="color: #008000;">false</span></strong>.</p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {
 var num: Any = <strong><span style="color: #008000;">"Welcome to C1CTech"
</span></strong>
 when(num){
 is Int ->; println("It is an Integer")
 is String ->; println("It is a String")
 is Double ->; println("It is a Double")
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>It is a String</pre>
<h4></h4>
<h4 class="p2"><span style="color: #000080;"><strong>Using when as a replacement for an if-else-if chain </strong></span></h4>
<p>We can use <strong><span style="color: #008000;">when</span></strong> as a replacement for <strong><span style="color: #008000;">if-else-if</span></strong>.</p>
<p>If no argument is supplied then the branch conditions are simply boolean expressions, and a branch is executed only when its condition is <strong><span style="color: #008000;">true</span></strong>:</p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {
 
 val marks = 65

 when {
 marks >;= 80 ->; println("You got A+ grade")
 marks >;= 75 ->; println("You got A grade")
 marks >;= 60 ->; println("You got B grade")
 marks >;= 45 ->; println("You got C grade")
 marks >;= 35 ->; println("You got D grade")
 else ->; println("You are fail")
 }

}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>You got B grade</pre>
<p> ;</p>


