<p class="p1">In this article, you will learn to use if expression in Kotlin with the help of examples.</p>
<p>There are different types of if expression in Kotlin:</p>
<ul>
<li><span style="color: #0000ff;"><strong>if-else expression</strong></span></li>
<li><span style="color: #0000ff;"><strong>if-else-if ladder expression</strong></span></li>
<li><span style="color: #0000ff;"><strong>nested if expression</strong></span></li>
</ul>
<h3 class="p1"><span style="color: #000080;"><b>Traditional if Statement</b></span></h3>
<h4><span style="color: #0000ff;"><strong>if statement</strong></span></h4>
<p>The if statement checks the given condition. If the condition evaluates to be true then the block of code/statements will execute otherwise not.</p>
<p class="p2"><span style="color: #0000ff;"><strong>Syntax :</strong></span></p>
<pre>if (condition) {
 <span style="color: #008000;"><strong>// block of code to be executed if the condition is true</strong></span>
}</pre>
<h4><span style="color: #0000ff;"><strong>if-else statement</strong></span></h4>
<p class="p1">The <span style="color: #008000;"><b>if</b> </span>statement executes a certain section of code if the test expression is evaluated to <span style="color: #008000;"><b>true</b></span>. The if statement may have an optional else block. Statements inside the body of else statement are executed if the test expression is evaluated to <span style="color: #008000;"><b>false</b></span>.</p>
<p class="p2"><strong><span style="color: #0000ff;">Syntax :</span></strong></p>
<pre>if (condition) {
 <span style="color: #008000;"><strong>// block of code to be executed if the condition is true</strong></span>
} else {
 <strong><span style="color: #008000;">// block of code to be executed if the condition is false</span></strong>
}</pre>
<h3></h3>
<h3 class="p1"><span style="color: #000080;"><b>Kotlin if-else Expression</b></span></h3>
<p class="p1">In Kotlin, <span style="color: #0000ff;"><strong>if</strong></span> is an expression, i.e. it returns a value. But it is not used as standalone, it is used with <strong><span style="color: #0000ff;">if-else</span> </strong>expression and the result of an if-else expression is assigned into a variable.</p>
<h4><strong><span style="color: #0000ff;">Flowchart:</span></strong></h4>
<p><img class=" wp-image-1777 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/04/if-else-exp-img-836912659-1586109540548.png" alt="if-else-exp-img" width="519" height="458" /></p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {
 val num1 = 10
 val num2 = 20
 val result = if (num1 >; num2) {
 "$num1 is greater than $num2"
 } else {
 "$num1 is smaller than $num2"
 }
 println(result)
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>10 is smaller than 20</pre>
<h4 class="p1"><span style="color: #0000ff;"><strong>Example explained</strong></span></h4>
<p class="p1">In the above example, <strong>when x=10, y=20:</strong></p>
<ul class="ul1">
<li class="li1">condition <strong><span style="color: #0000ff;">10>;20</span></strong> is checked, yields false and <span style="color: #008000;"><b>“10 is smaller than 20”</b></span> gets printed.</li>
</ul>
<p class="p1"><strong> when x=20, y=10 :</strong></p>
<ul>
<li class="p1">condition <strong><span style="color: #0000ff;">20>;10</span></strong> is checked, yields true and <span style="color: #008000;"><b>“10 is greater than 20”</b> </span>gets printed.</li>
</ul>
<p class="p1">We can remove the curly braces of if-else body by writing if expression in only one statement.</p>
<p><strong><span style="color: #0000ff;">For example:</span></strong></p>
<pre>fun main(args: Array<;String>;) {
 val num1 = 10
 val num2 = 20
 val result = if (num1 >; num2) "$num1 is greater than $num2" else "$num1 is smaller than $num2"
 println(result)
}</pre>
<p class="p1">Using if-else expression in one single line statement is like ternary operator in Java. Kotlin does not support any ternary operator.</p>
<h3 class="p1"><strong><span style="color: #000080;">Kotlin if-else-if Ladder Expression</span></strong></h3>
<p>When we want to execute a particular block of code among many blocks we can use if-else-if ladder.</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>

<p><strong><span style="color: #0000ff;">Note:</span></strong> If the block of if, else, else-if contains more than one expression, the last expression is returned as the value of the block.</p>
<h4><strong><span style="color: #0000ff;">Flowchart:</span></strong></h4>
<p><img class=" wp-image-1778 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/04/ladder-if-exp-img.png" alt="ladder-if-exp-img" width="629" height="550" /></p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {
 val marks = 65

 val result = if (marks >;= 80) {
 print("You got A+ grade with marks: ")
 marks
 } else if (marks >;= 75) {
 print("You got A grade with marks: ")
 marks
 } else if (marks >;= 60) {
 print("You got B grade with marks: ")
 marks
 } else if (marks >;= 45) {
 print("You got C grade with marks: ")
 marks
 } else if (marks >;= 35) {
 print("You got D grade with marks: ")
 marks
 } else {
 print("You are fail with marks: ")
 marks
 }
 println(result)
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>You got B grade with marks: 65</pre>
<h4 class="p1"><span style="color: #0000ff;"><strong>Example explained</strong></span></h4>
<p class="p1">In the above example, when marks is 65:</p>
<ul class="ul1">
<li class="li1">condition 1 is checked. <span style="color: #0000ff;"><b>65>;=80</b></span>, yields false.</li>
<li class="li1">condition 2 is checked. <span style="color: #0000ff;"><b>65>;=75</b></span>, yields false.</li>
<li class="li1">condition 3 is checked. <span style="color: #0000ff;"><b>65>;=60</b></span>, yields true and <span style="color: #008000;"><b>“You got B grade with marks: 65”</b></span> gets printed.</li>
</ul>
<h3></h3>
<h3 class="p1"><strong><span style="color: #000080;">Kotlin Nested if Expression</span></strong></h3>
<p class="p1">An <span style="color: #0000ff;"><strong>if</strong> </span>expression can be used inside the block of another <strong><span style="color: #0000ff;">if</span> </strong>expression known as nested if expression.</p>
<h4><strong><span style="color: #0000ff;">Flowchart:</span></strong></h4>
<p><img class=" wp-image-1779 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/04/nested_if_new.png" alt="nested_if_new" width="611" height="401" /></p>
<h4><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>fun main(args: Array<;String>;) {
 val num1 = 10
 val num2 = 20
 val num3 = 30

 val max = if (num1 >; num2) {
 if (num1 >; num3)
 num1
 else
 num3
 } else {
 if (num2 >; num3)
 num2
 else
 num3
 }
 println("maximum number = $max")
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>maximum number = 30</pre>
<h4 class="p1"><span style="color: #0000ff;"><strong>Example explained</strong></span></h4>
<p class="p1">In the above example, when num1=10, num2=20, num3=30 :</p>
<ul class="ul1">
<li class="li1">condition <span style="color: #0000ff;"><strong>10>;20</strong></span> is checked, yields <strong><span style="color: #008000;">false</span></strong>, skip if block and move to <strong><span style="color: #008000;">else</span></strong> block.</li>
<li class="li1">condition <strong><span style="color: #0000ff;">20>;30</span></strong> is checked, yields <strong><span style="color: #008000;">false</span></strong>.</li>
<li class="li1">move to else part and return <strong><span style="color: #008000;">num3</span></strong> as the maximum number.</li>
</ul>
<p>when num1=30, num2=20, num3=10 :</p>
<ul class="ul1">
<li class="li1">condition <strong><span style="color: #0000ff;">30>;20</span></strong> is checked, yields <strong><span style="color: #008000;">true</span></strong>.</li>
<li>condition <strong><span style="color: #0000ff;">30>;10</span></strong> is checked, yields <strong><span style="color: #008000;">true</span></strong>, return <strong><span style="color: #008000;">num1</span></strong> as the maximum number.</li>
</ul>


