{"id":5222,"date":"2021-03-23T12:48:16","date_gmt":"2021-03-23T12:48:16","guid":{"rendered":"https:\/\/ded9.com\/?p=5222"},"modified":"2025-12-09T11:41:32","modified_gmt":"2025-12-09T11:41:32","slug":"complete-tutorial-on-if-and-if-commands-else-commands-in-java","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/complete-tutorial-on-if-and-if-commands-else-commands-in-java\/","title":{"rendered":"Understanding if, else and Conditional Logic in Java \u2014 A Clear Guide"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn to use two selections of If Commands: if and if\u2026 else to control the program execution flow. <\/span><span style=\"font-size: 12pt;\">In programming, executing a particular <a href=\"https:\/\/ded9.com\/basic-command-codes-and-linux-commands-in-cpanel\/\">code<\/a> is often desirable based on whether the specified condition is true or false (determined only at runtime).\u00a0<\/span><\/p>\n<p>In such cases, control-flow commands are used.<span id=\"more-50068\"><\/span><\/p>\n<h2><span style=\"font-size: 18pt;\"><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-258031 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/java-if-else-if-control-statements-syntax.jpg\" alt=\"In this tutorial, you will learn to use two selections of If Commands: if and if\u2026 else to control the program execution flow. In programming, executing a particular code is often desirable based on whether the specified condition is true or false (determined only at runtime).\u00a0\" width=\"800\" height=\"460\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/java-if-else-if-control-statements-syntax.jpg 800w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/java-if-else-if-control-statements-syntax-300x173.jpg 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/java-if-else-if-control-statements-syntax-768x442.jpg 768w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/span><\/h2>\n<h2><strong>If Commands (if-then in Java)<\/strong><\/h2>\n<p>The syntax of the if-then Command in Java is as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">if (expression) {\r\n\r\n\/\/ statements\r\n\r\n}<\/pre>\n<\/div>\n<p>Here, the expression is a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Boolean_data_type#:~:text=In%20computer%20science%2C%20the%20Boolean,of%20logic%20and%20Boolean%20algebra.\" target=\"_blank\" rel=\"noopener\">Boolean <\/a>expression (true or false).<\/p>\n<p>If the expression is evaluated correctly, the statements inside the if body (in parentheses) are executed.<\/p>\n<p>If the expression is incorrectly assessed, the statements inside the if body will not be executed.<\/p>\n<h2><span style=\"font-size: 18pt;\">How does the if statement work?<\/span><\/h2>\n<p><strong><img decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50070\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-if-jpg.jpeg\" alt=\"How does the if statement work?\" width=\"559\" height=\"217\" data-ll-status=\"loaded\" \/><\/strong><\/p>\n<p><strong>Example 1: If statement in Java<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class IfStatement {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int number = 10;<\/li>\n<li dir=\"ltr\">if (number&gt; 0) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cNumber is positive.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;This statement is always executed.&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Number is positive.<\/p>\n<p dir=\"ltr\">This statement is always executed.<\/p>\n<\/blockquote>\n<p>When the number equals 10, the expression&gt; 0 is considered correct. Hence, the code inside the if body is executed.<\/p>\n<p>Now, change the number value to a negative integer. For example -5. The output in this case will be equal to:<\/p>\n<blockquote>\n<p dir=\"ltr\">This statement is always executed.<\/p>\n<\/blockquote>\n<p>When the number is equal to -5, the expression number&gt; 0 is considered incorrect. Therefore, the Java compiler optimizes away the if body.<\/p>\n<h2><span style=\"font-size: 18pt;\"><strong>Command (if\u2026 else (if-then-else in Java)<\/strong><\/span><\/h2>\n<p>A specific part of the code will execute if the condition evaluates to true. The if statement may also have an else block. If the condition is incorrectly assessed, the commands in the else block are executed.<\/p>\n<p>The syntax of the if-then-else Command is as follows:<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">if (expression) {\r\n\r\n\/\/ codes\r\n\r\n}\r\n\r\nelse {\r\n\r\n\/\/ some other code\r\n\r\n}<\/pre>\n<\/div>\n<h2>How does the if\u2026 else command work?<\/h2>\n<p><img decoding=\"async\" class=\"lazyloaded aligncenter wp-image-50071\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2025\/02\/c-users-mr-desktop-ifelse-jpg.jpeg\" alt=\"How does the if\u2026 else command work?\" width=\"555\" height=\"292\" data-ll-status=\"loaded\" \/><\/p>\n<p><strong>Example 2: The if\u2026 else Command in Java<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class IfElse {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int number = 10;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">if (number&gt; 0) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cNumber is positive.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">else {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cNumber is not positive.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">System.out.println (&#8220;This statement is always executed.&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Number is positive.<\/p>\n<p dir=\"ltr\">This statement is always executed.<\/p>\n<\/blockquote>\n<p>When a number equals 10, the expression&gt; 0 is evaluated correctly. In this case, the code in the if block will be executed, and the code in the else block will not.<\/p>\n<p>Now, change the number&#8217;s value to a negative number. For example -5. The output in this case will be equal to:<\/p>\n<blockquote>\n<p dir=\"ltr\">Number is not positive.<\/p>\n<p dir=\"ltr\">This statement is always executed.<\/p>\n<\/blockquote>\n<p>When the number is -5, the expression &gt; 0 evaluates to false. In this case, the code in the else block will be executed, and the code in the if block will not.<\/p>\n<h2><span style=\"font-size: 18pt;\">The Command if else\u2026 if in Java<\/span><\/h2>\n<p>In Java, executing one piece of code among many is possible. You can use if\u2026 else\u2026 if to do this.<\/p>\n<div class=\"wp-block-codemirror-blocks code-block \">\n<pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;lineWrapping&quot;:false,&quot;styleActiveLine&quot;:false,&quot;readOnly&quot;:true,&quot;align&quot;:&quot;&quot;}\">if (expression1)\r\n\r\n{\r\n\r\n\/\/ codes\r\n\r\n}\r\n\r\nelse if (expression2)\r\n\r\n{\r\n\r\n\/\/ codes\r\n\r\n}\r\n\r\nelse if (expression3)\r\n\r\n{\r\n\r\n\/\/ codes\r\n\r\n}\r\n\r\n.\r\n\r\n.\r\n\r\nelse\r\n\r\n{\r\n\r\n\/\/ codes\r\n\r\n}<\/pre>\n<\/div>\n<p>The if statements are executed from top to bottom. Once the condition is met, the corresponding code is executed. After that, the program control jumps out of the if\u2026 else\u2026 if.<\/p>\n<p>If all test statements evaluate to false, the code in the else block will be executed.<\/p>\n<p><strong>Example 3: If-else\u2026 if statement in Java<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Ladder {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int number = 0;<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">if (number&gt; 0) {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cNumber is positive.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">else if (number &lt;0) {<\/li>\n<li dir=\"ltr\">System.out.println (&#8220;Number is negative.&#8221;);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">else {<\/li>\n<li dir=\"ltr\">System.out.println (\u201cNumber is 0.\u201d);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Number is 0.<\/p>\n<\/blockquote>\n<p>When a number is 0, both number&gt; 0 and number &lt;0 are considered incorrect. Hence, the code executes within another body.<\/p>\n<p>The above program checks whether the number is positive, negative, or 0.<\/p>\n<h2><span style=\"font-size: 18pt;\">The if\u2026 else Command nested in Java<\/span><\/h2>\n<p>It is possible to have nested if..else commands in Java.<\/p>\n<p>Here is a program to find the most significant number out of 3:<\/p>\n<p><strong>Example 4: If-else nested Command<\/strong><\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class Number {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber;<\/li>\n<li dir=\"ltr\">if (n1&gt; = n2) {<\/li>\n<li dir=\"ltr\">if (n1&gt; = n3) {<\/li>\n<li dir=\"ltr\">largestNumber = n1;<\/li>\n<li dir=\"ltr\">} else {<\/li>\n<li dir=\"ltr\">largestNumber = n3;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">} else {<\/li>\n<li dir=\"ltr\">if (n2&gt; = n3) {<\/li>\n<li dir=\"ltr\">largestNumber = n2;<\/li>\n<li dir=\"ltr\">} else {<\/li>\n<li dir=\"ltr\">largestNumber = n3;<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">System.out.println (\u201cLargest number is\u201d + largestNumber);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>Output<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">Largest number is 4.5<\/p>\n<\/blockquote>\n<p>Note: In the programs above, we assigned values to the variables ourselves to simplify the example. However, in real-world applications, these values \u200b\u200bmay be derived from user input data, log files, submitted forms, and similar sources.<\/p>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What does the if statement do in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The if statement evaluates a Boolean condition \u2014 if it\u2019s true, Java executes the block of code inside the if braces; if false, it skips it.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do if...else if...else chains work?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Java checks each condition in order: if the first if is true, its block runs and the rest are skipped; if not, it tries the next else if, and if none are true, the optional else block runs.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Can if-else statements be nested inside each other?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes \u2014 you can place an if...else (or another if) inside any block, allowing complex decision logic based on multiple layers of conditions.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn to use two selections of If Commands: if and if\u2026 else to control the program execution flow. In programming, executing a particular code is often desirable based on whether the specified condition is true or false (determined only at runtime).\u00a0 In such cases, control-flow commands are used. If Commands [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5223,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5222","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5222"}],"version-history":[{"count":6,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5222\/revisions"}],"predecessor-version":[{"id":265975,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5222\/revisions\/265975"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5223"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}