<p class="p1">In this article, you will learn to use selection statements if, if-else, if-else-if <span style="color: var(--color-text);">to control the flow of execution of a program based on certain conditions.</span></p>
<p class="p1">Selection statements also known as conditional statements allows a program to test several conditions, and execute instructions based on which condition is true.</p>
<h4><span style="color: #000080;"><strong>The conditional statements:</strong></span></h4>
<ul class="points">
<li><strong><span style="color: #0000ff;">if statement</span></strong></li>
<li><strong><span style="color: #0000ff;">if-else statement</span></strong></li>
<li><strong><span style="color: #0000ff;">if-else-if ladder</span></strong></li>
<li><strong><span style="color: #0000ff;">Nested if</span></strong></li>
</ul>
<h3><span style="color: #000080;"><strong>if statement</strong></span></h3>
<p class="p1">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>
<h4><strong><span style="color: #0000ff;">Syntax</span></strong></h4>
<p class="p2">The syntax of <strong><span style="color: #008000;">if</span></strong> statement:</p>
<pre>if (condition) {
 <strong><span style="color: #008000;">// block of code to be executed if the condition is true</span></strong>
 }</pre>
<p class="p2">The <span style="color: #0000ff;"><strong>condition</strong></span> is a boolean expression (returns either <span style="color: #008000;"><strong>true</strong></span> or <strong><span style="color: #008000;">false)</span></strong>.</p>
<p class="p2">If the condition is evaluated to <span style="color: #008000;"><strong>true</strong></span>, statements inside the body of <strong><span style="color: #0000ff;">if</span></strong> (statements inside curly braces) gets executed.</p>
<p class="p2">If the expression is evaluated to <strong><span style="color: #008000;">false</span></strong>, statements inside the body of <strong><span style="color: #0000ff;">if</span> </strong>are skipped from execution.</p>
<h4><span style="color: #0000ff;"><strong>Flowchart:</strong></span></h4>
<p><img class="aligncenter size-full wp-image-1776" src="https://i1.wp.com/c1ctech.com/wp-content/uploads/2020/04/if_exp_img-2110217443-1586109464792.png?ssl=1&;w=862" alt="if_exp_img" width="431" height="439" /></p>
<h4 class="p1"><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre>int x = 20;
int y = 18;
if (x >; y) {
 System.out.println("x is greater than y");
}
System.out.println("Outside if statement");</pre>
<h4 class="p1"><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>x is greater than y
Outside if statement</pre>
<h4 class="p1"><span style="color: #0000ff;"><b>Example explained</b></span></h4>
<p class="p1">In the above example, when x=20 and y=18:</p>
<ul class="ul1">
<li class="li1">condition <strong><span style="color: #0000ff;">20>;18</span></strong> is checked, yields true and <b>&#8220;<span style="color: #008000;">x is greater than y&#8221;</span></b> gets printed.</li>
</ul>
<p>when x=18 and y=20:</p>
<ul class="ul1">
<li>condition <strong><span style="color: #0000ff;">18>;20</span></strong> is checked, yields false.</li>
<li class="li1">
<p class="p1">the Java compiler skips the execution of the body of <strong><span style="color: #0000ff;">if</span></strong> statement <span style="color: var(--color-text);">and </span><span style="color: #008000;"><b>&#8220;Outside if statement&#8221;</b></span><span style="color: var(--color-text);"> gets printed.</span></p>
</li>
</ul>
<h3><span style="color: #000080;"><strong>if-else statement</strong></span></h3>
<p class="p1">The <span style="color: #0000ff;"><strong>if</strong> </span>statement executes a certain section of code if the test expression is evaluated to <strong><span style="color: #008000;">true</span></strong>. 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 <strong><span style="color: #008000;">false</span></strong>.</p>
<h4><strong><span style="color: #0000ff;">Syntax</span></strong></h4>
<p class="p1">The syntax of<strong><span style="color: #008000;"> if-else</span> </strong>statement:</p>
<pre>if (condition) {
 <strong><span style="color: #008000;">// block of code to be executed if the condition is true</span></strong>
} else {
 <strong><span style="color: #008000;">// block of code to be executed if the condition is false</span></strong>
}</pre>
<p class="p2">If the condition is evaluated to <span style="color: #008000;"><strong>true</strong></span>, statements inside the body of <strong><span style="color: #0000ff;">if</span></strong> are executed.</p>
<p class="p2">If the condition is evaluated to <strong><span style="color: #008000;">false</span></strong>, statements inside the body of <strong><span style="color: #0000ff;">else</span> </strong>are executed.</p>
<h4><span style="color: #0000ff;"><strong>Flowchart:</strong></span></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="501" height="442" /></p>
<h4 class="p1"><span style="color: #0000ff;"><strong>Example</strong></span></h4>
<pre> int age = 16;
 if (age >;= 18) {
 System.out.println("You are eligible to vote");
 } else {
 System.out.println("You are not eligible to vote");
 }</pre>
<h4 class="p1"><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>You are not eligible to vote</pre>
<h4 class="p1"><span style="color: #0000ff;"><b>Example explained</b></span></h4>
<p class="p1">In the above example, when age is 16:</p>
<ul class="ul1">
<li class="li1">condition <strong><span style="color: #0000ff;">16>;=18</span></strong> is checked, yields false and <span style="color: #008000;"><b>&#8220;You are not eligible to vote&#8221;</b></span> gets printed.</li>
</ul>
<p>when age is 20:</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">condition <strong><span style="color: #0000ff;">20>;=18</span></strong> is checked, yields true and <span style="color: #008000;"><b>&#8220;You are eligible to vote&#8221;</b></span> gets printed.</li>
</ul>
<h3><span style="color: #000080;"><strong>if-else-if ladder</strong></span></h3>
<p class="p1">The if-else-if ladder statement executes one condition from multiple statements.</p>
<h4 class="p1"><strong><span style="color: #0000ff;">Syntax</span></strong></h4>
<pre>if (condition1) {
 <strong><span style="color: #008000;">// block of code to be executed if condition1 is true</span></strong>
 } else if (condition2) {
 <strong><span style="color: #008000;">// block of code to be executed if the condition1 is false and condition2 is true</span></strong>
 } else {
<strong><span style="color: #008000;"> // block of code to be executed if both condition1 and condition2 is false
</span></strong> }</pre>
<p class="p1">The expression is tested from the top (of the ladder) to downwards.</p>
<p class="p1">As soon as one of the conditions controlling the if is <strong><span style="color: #008000;">true</span></strong>, the statement associated with that if is executed, and the rest of the else-if ladder is bypassed.</p>
<p class="p1">If none of the conditions is true, then the final <strong><span style="color: #008000;">else</span></strong> statement will be executed.</p>
<h4><span style="color: #0000ff;"><strong>Flowchart:</strong></span></h4>
<p><img class="alignnone size-full wp-image-1778" src="https://c1ctech.com/wp-content/uploads/2020/04/ladder-if-exp-img.png" alt="ladder-if-exp-img" width="1416" height="1240" /></p>
<h4 class="p1"><strong><span style="color: #0000ff;">Example</span></strong></h4>
<pre>int marks = 65;

<strong>if (marks >;= 80) {
 System.out.println("You got A+ grade");
} else if (marks >;= 75) {
 System.out.println("You got A grade");
} else if (marks >;= 60) {
 System.out.println("You got B grade");
} else if (marks >;= 45) {
 System.out.println("You got C grade");
} else if (marks >;= 35) {
 System.out.println("You got D grade");
} else {
 System.out.println("You are fail");
}
</strong>
System.out.println("Outside if-else-if");</pre>
<h4 class="p1"><strong><span style="color: #0000ff;">Output</span></strong></h4>
<pre>You got B grade
Outside if-else-if</pre>
<h4 class="p1"><span style="color: #0000ff;"><b>Example explained</b></span></h4>
<p>In the above example, when marks is 65:</p>
<ul>
<li class="p1">condition 1 is checked. <strong><span style="color: #0000ff;">65>;=80</span></strong>, yields false.</li>
<li class="p1">condition 2 is checked. <strong><span style="color: #0000ff;">65>;=75</span></strong>, yields false.</li>
<li class="p1">condition 3 is checked. <strong><span style="color: #0000ff;">65>;=60</span></strong>, yields true and <strong><span style="color: #008000;">&#8220;You got B grade&#8221;</span></strong> gets printed.</li>
<li>the rest of the else-if ladder is just bypassed and <strong><span style="color: #008000;">&#8220;Outside if-else-if&#8221;</span></strong> gets printed.</li>
</ul>
<h3 class="p1"><span style="color: #000080;"><strong>Nested if</strong></span></h3>
<p class="p1"><span style="color: #008000;"><b>if statement inside an if statement</b></span> is known as nested if. if statement, in this case, is the target of another if or else statement.</p>
<h4 class="p1"><strong><span style="color: #0000ff;">Syntax</span></strong></h4>
<pre class="p1">if (condition1)
{
<span class="Apple-converted-space"> </span><span style="color: #008000;"><strong>// code to be executed
<span class="Apple-converted-space"> </span>// if condition1 is true
</strong></span>
<span class="Apple-converted-space"> </span>if (condition2)
<span class="Apple-converted-space"> </span>{
<span class="Apple-converted-space"> <span style="color: #008000;"> </span></span><strong><span style="color: #008000;">// code to be executed
<span class="Apple-converted-space"> </span>// if condition2 is true</span></strong>
<span class="Apple-converted-space"> </span>}
}</pre>
<p>When more then one condition needs to be true and one of the condition is the sub-condition of parent condition, nested if can be used.</p>
<h4><span style="color: #0000ff;"><strong>Flowchart:</strong></span></h4>
<p><img class="alignnone size-full wp-image-1779" src="https://c1ctech.com/wp-content/uploads/2020/04/nested_if_new.png" alt="nested_if_new" width="1348" height="884" /></p>
<h4><strong><span style="color: #0000ff;">Example</span></strong></h4>
<pre>int i = 10;

if (i == 10) {

<span style="color: #008000;"><strong>// Nested - if statement</strong></span>
<span style="color: #008000;"><strong>// Will only be executed if statement</strong></span>
<span style="color: #008000;"><strong>// above it is true</strong></span>
if (i <; 12)
 System.out.println("i is smaller than 12 too");
else
 System.out.println("i is not smaller than 12");
}</pre>
<h4><span style="color: #0000ff;"><strong>Output</strong></span></h4>
<pre>i is smaller than 12 too</pre>
<h4 class="p1"><span style="color: #0000ff;"><b>Example explained</b></span></h4>
<p class="p1">In the above example, when i is 10:</p>
<ul class="ul1">
<li class="li1">condition <strong><span style="color: #0000ff;">(10==10)</span></strong> is checked, yields <strong><span style="color: #008000;">true</span></strong>.</li>
<li class="li1">condition <span style="color: #0000ff;"><strong>(10<;12)</strong></span> is checked, yields <strong><span style="color: #008000;">true</span></strong> and <span style="color: #008000;"><b>&#8220;i is smaller than 12 too&#8221;</b></span> gets printed.</li>
</ul>
<p>when i is 13:</p>
<ul class="ul1">
<li class="li1">condition <strong><span style="color: #0000ff;">(13==13)</span></strong> is checked, yields <span style="color: #008000;"><strong>true</strong></span>.</li>
<li class="li1">condition <strong><span style="color: #0000ff;">(13<;12)</span></strong> is checked, yields <strong><span style="color: #008000;">false</span></strong> and else statement <span style="color: #008000;"><b>&#8220;i is not smaller than 12 &#8220;</b></span> gets printed.</li>
</ul>


