Site icon C1CTech

Java if…else

<p class&equals;"p1">In this article&comma; you will learn to use selection statements if&comma; if-else&comma; if-else-if <span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">to control the flow of execution of a program based on certain conditions&period;<&sol;span><&sol;p>&NewLine;<p class&equals;"p1">Selection statements also known as conditional statements allows a program to test several conditions&comma; and execute instructions based on which condition is true&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>The conditional statements&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<ul class&equals;"points">&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">if statement<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">if-else statement<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">if-else-if ladder<&sol;span><&sol;strong><&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Nested if<&sol;span><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>if statement<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">The if statement checks the given condition&period; If the condition evaluates to be true then the block of code&sol;statements will execute otherwise not&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p2">The syntax of <strong><span style&equals;"color&colon; &num;008000&semi;">if<&sol;span><&sol;strong> statement&colon;<&sol;p>&NewLine;<pre>if &lpar;condition&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; block of code to be executed if the condition is true<&sol;span><&sol;strong>&NewLine; &rcub;<&sol;pre>&NewLine;<p class&equals;"p2">The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>condition<&sol;strong><&sol;span> is a boolean expression &lpar;returns either <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span> or <strong><span style&equals;"color&colon; &num;008000&semi;">false&rpar;<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p class&equals;"p2">If the condition is evaluated to <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&comma; statements inside the body of <strong><span style&equals;"color&colon; &num;0000ff&semi;">if<&sol;span><&sol;strong> &lpar;statements inside curly braces&rpar; gets executed&period;<&sol;p>&NewLine;<p class&equals;"p2">If the expression is evaluated to <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&comma; statements inside the body of <strong><span style&equals;"color&colon; &num;0000ff&semi;">if<&sol;span> <&sol;strong>are skipped from execution&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Flowchart&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-1776" src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;if&lowbar;exp&lowbar;img-2110217443-1586109464792&period;png&quest;ssl&equals;1&amp&semi;w&equals;862" alt&equals;"if&lowbar;exp&lowbar;img" width&equals;"431" height&equals;"439" &sol;><&sol;p>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>int x &equals; 20&semi;&NewLine;int y &equals; 18&semi;&NewLine;if &lpar;x &gt&semi; y&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"x is greater than y"&rpar;&semi;&NewLine;&rcub;&NewLine;System&period;out&period;println&lpar;"Outside if statement"&rpar;&semi;<&sol;pre>&NewLine;<h4 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>x is greater than y&NewLine;Outside if statement<&sol;pre>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Example explained<&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma; when x&equals;20 and y&equals;18&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">20&gt&semi;18<&sol;span><&sol;strong> is checked&comma; yields true and <b>&&num;8220&semi;<span style&equals;"color&colon; &num;008000&semi;">x is greater than y&&num;8221&semi;<&sol;span><&sol;b> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>when x&equals;18 and y&equals;20&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li>condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">18&gt&semi;20<&sol;span><&sol;strong> is checked&comma; yields false&period;<&sol;li>&NewLine;<li class&equals;"li1">&NewLine;<p class&equals;"p1">the Java compiler skips the execution of the body of <strong><span style&equals;"color&colon; &num;0000ff&semi;">if<&sol;span><&sol;strong> statement <span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;">and  <&sol;span><span style&equals;"color&colon; &num;008000&semi;"><b>&&num;8220&semi;Outside if statement&&num;8221&semi;<&sol;b><&sol;span><span style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;"> gets printed&period;<&sol;span><&sol;p>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>if-else statement<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">The <span style&equals;"color&colon; &num;0000ff&semi;"><strong>if<&sol;strong> <&sol;span>statement executes a certain section of code if the test expression is evaluated to <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period; The if statement may have an optional else block&period; Statements inside the body of else statement are executed if the test expression is evaluated to <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">The syntax of<strong><span style&equals;"color&colon; &num;008000&semi;"> if-else<&sol;span> <&sol;strong>statement&colon;<&sol;p>&NewLine;<pre>if &lpar;condition&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; block of code to be executed if the condition is true<&sol;span><&sol;strong>&NewLine;&rcub; else &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; block of code to be executed if the condition is false<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p2">If the condition is evaluated to <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&comma; statements inside the body of <strong><span style&equals;"color&colon; &num;0000ff&semi;">if<&sol;span><&sol;strong> are executed&period;<&sol;p>&NewLine;<p class&equals;"p2">If the condition is evaluated to <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong>&comma; statements inside the body of <strong><span style&equals;"color&colon; &num;0000ff&semi;">else<&sol;span> <&sol;strong>are executed&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Flowchart&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;" wp-image-1777 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;if-else-exp-img-836912659-1586109540548&period;png" alt&equals;"if-else-exp-img" width&equals;"501" height&equals;"442" &sol;><&sol;p>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Example<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre> int age &equals; 16&semi;&NewLine; if &lpar;age &gt&semi;&equals; 18&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"You are eligible to vote"&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; System&period;out&period;println&lpar;"You are not eligible to vote"&rpar;&semi;&NewLine; &rcub;<&sol;pre>&NewLine;<h4 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>You are not eligible to vote<&sol;pre>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Example explained<&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma; when age is 16&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">16&gt&semi;&equals;18<&sol;span><&sol;strong> is checked&comma; yields false and <span style&equals;"color&colon; &num;008000&semi;"><b>&&num;8220&semi;You are not eligible to vote&&num;8221&semi;<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>when age is 20&colon;<&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">condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">20&gt&semi;&equals;18<&sol;span><&sol;strong> is checked&comma; yields true and <span style&equals;"color&colon; &num;008000&semi;"><b>&&num;8220&semi;You are eligible to vote&&num;8221&semi;<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>if-else-if ladder<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1">The if-else-if ladder statement executes one condition from multiple statements&period;<&sol;p>&NewLine;<h4 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>if &lpar;condition1&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; block of code to be executed if condition1 is true<&sol;span><&sol;strong>&NewLine; &rcub; else if &lpar;condition2&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; block of code to be executed if the condition1 is false and condition2 is true<&sol;span><&sol;strong>&NewLine; &rcub; else &lbrace;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; block of code to be executed if both condition1 and condition2 is false&NewLine;<&sol;span><&sol;strong> &rcub;<&sol;pre>&NewLine;<p class&equals;"p1">The expression is tested from the top &lpar;of the ladder&rpar; to downwards&period;<&sol;p>&NewLine;<p class&equals;"p1">As soon as one of the conditions controlling the if is <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&comma; the statement associated with that if is executed&comma; and the rest of the else-if ladder is bypassed&period;<&sol;p>&NewLine;<p class&equals;"p1">If none of the conditions is true&comma; then the final <strong><span style&equals;"color&colon; &num;008000&semi;">else<&sol;span><&sol;strong> statement will be executed&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Flowchart&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1778" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;ladder-if-exp-img&period;png" alt&equals;"ladder-if-exp-img" width&equals;"1416" height&equals;"1240" &sol;><&sol;p>&NewLine;<h4 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>int marks &equals; 65&semi;&NewLine;&NewLine;<strong>if &lpar;marks &gt&semi;&equals; 80&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"You got A&plus; grade"&rpar;&semi;&NewLine;&rcub; else if &lpar;marks &gt&semi;&equals; 75&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"You got A grade"&rpar;&semi;&NewLine;&rcub; else if &lpar;marks &gt&semi;&equals; 60&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"You got B grade"&rpar;&semi;&NewLine;&rcub; else if &lpar;marks &gt&semi;&equals; 45&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"You got C grade"&rpar;&semi;&NewLine;&rcub; else if &lpar;marks &gt&semi;&equals; 35&rpar; &lbrace;&NewLine; System&period;out&period;println&lpar;"You got D grade"&rpar;&semi;&NewLine;&rcub; else &lbrace;&NewLine; System&period;out&period;println&lpar;"You are fail"&rpar;&semi;&NewLine;&rcub;&NewLine;<&sol;strong>&NewLine;System&period;out&period;println&lpar;"Outside if-else-if"&rpar;&semi;<&sol;pre>&NewLine;<h4 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Output<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>You got B grade&NewLine;Outside if-else-if<&sol;pre>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Example explained<&sol;b><&sol;span><&sol;h4>&NewLine;<p>In the above example&comma; when marks is 65&colon;<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"p1">condition 1 is checked&period; <strong><span style&equals;"color&colon; &num;0000ff&semi;">65&gt&semi;&equals;80<&sol;span><&sol;strong>&comma; yields false&period;<&sol;li>&NewLine;<li class&equals;"p1">condition 2 is checked&period; <strong><span style&equals;"color&colon; &num;0000ff&semi;">65&gt&semi;&equals;75<&sol;span><&sol;strong>&comma; yields false&period;<&sol;li>&NewLine;<li class&equals;"p1">condition 3 is checked&period; <strong><span style&equals;"color&colon; &num;0000ff&semi;">65&gt&semi;&equals;60<&sol;span><&sol;strong>&comma; yields true and <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;You got B grade&&num;8221&semi;<&sol;span><&sol;strong> gets printed&period;<&sol;li>&NewLine;<li>the rest of the else-if ladder is just bypassed and  <strong><span style&equals;"color&colon; &num;008000&semi;">&&num;8220&semi;Outside if-else-if&&num;8221&semi;<&sol;span><&sol;strong> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 class&equals;"p1"><span style&equals;"color&colon; &num;000080&semi;"><strong>Nested if<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;008000&semi;"><b>if statement inside an if statement<&sol;b><&sol;span> is known as nested if&period; if statement&comma; in this case&comma; is the target of another if or else statement&period;<&sol;p>&NewLine;<h4 class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Syntax<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre class&equals;"p1">if &lpar;condition1&rpar;&NewLine;&lbrace;&NewLine;<span class&equals;"Apple-converted-space">    <&sol;span><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; code to be executed&NewLine;<span class&equals;"Apple-converted-space">    <&sol;span>&sol;&sol; if condition1 is true&NewLine;<&sol;strong><&sol;span>&NewLine;<span class&equals;"Apple-converted-space">    <&sol;span>if &lpar;condition2&rpar;&NewLine;<span class&equals;"Apple-converted-space">    <&sol;span>&lbrace;&NewLine;<span class&equals;"Apple-converted-space">    <span style&equals;"color&colon; &num;008000&semi;">    <&sol;span><&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; code to be executed&NewLine;<span class&equals;"Apple-converted-space">        <&sol;span>&sol;&sol; if condition2 is true<&sol;span><&sol;strong>&NewLine;<span class&equals;"Apple-converted-space">    <&sol;span>&rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>When more then one condition needs to be true and one of the condition is the sub-condition of parent condition&comma; nested if can be used&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Flowchart&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1779" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;04&sol;nested&lowbar;if&lowbar;new&period;png" alt&equals;"nested&lowbar;if&lowbar;new" width&equals;"1348" height&equals;"884" &sol;><&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example<&sol;span><&sol;strong><&sol;h4>&NewLine;<pre>int i &equals; 10&semi;&NewLine;&NewLine;if &lpar;i &equals;&equals; 10&rpar; &lbrace;&NewLine;&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Nested - if statement<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Will only be executed if statement<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; above it is true<&sol;strong><&sol;span>&NewLine;if &lpar;i &lt&semi; 12&rpar;&NewLine; System&period;out&period;println&lpar;"i is smaller than 12 too"&rpar;&semi;&NewLine;else&NewLine; System&period;out&period;println&lpar;"i is not smaller than 12"&rpar;&semi;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Output<&sol;strong><&sol;span><&sol;h4>&NewLine;<pre>i is smaller than 12 too<&sol;pre>&NewLine;<h4 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Example explained<&sol;b><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">In the above example&comma; when i is 10&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">&lpar;10&equals;&equals;10&rpar;<&sol;span><&sol;strong> is checked&comma; yields <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li class&equals;"li1">condition <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&lpar;10&lt&semi;12&rpar;<&sol;strong><&sol;span> is checked&comma; yields <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;008000&semi;"><b>&&num;8220&semi;i is smaller than 12 too&&num;8221&semi;<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>when i is 13&colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">&lpar;13&equals;&equals;13&rpar;<&sol;span><&sol;strong> is checked&comma; yields <span style&equals;"color&colon; &num;008000&semi;"><strong>true<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li class&equals;"li1">condition <strong><span style&equals;"color&colon; &num;0000ff&semi;">&lpar;13&lt&semi;12&rpar;<&sol;span><&sol;strong> is checked&comma; yields <strong><span style&equals;"color&colon; &num;008000&semi;">false<&sol;span><&sol;strong> and else statement <span style&equals;"color&colon; &num;008000&semi;"><b>&&num;8220&semi;i is not smaller than 12 &&num;8220&semi;<&sol;b><&sol;span> gets printed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;&NewLine;

Exit mobile version