{"id":1160,"date":"2022-02-24T19:12:46","date_gmt":"2022-02-24T19:12:46","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1160"},"modified":"2022-02-24T19:12:46","modified_gmt":"2022-02-24T19:12:46","slug":"if-else-statement","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/24\/if-else-statement\/","title":{"rendered":"if&#8230;else Statement"},"content":{"rendered":"\n<p>In this tutorial, you will learn about control flow statements using Java if and if&#8230;else statements with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">In programming, we use the&nbsp;<code>if..else<\/code>&nbsp;statement to run a block of code among more than one alternatives.<\/p>\n\n\n\n<p>For example, assigning grades (A, B, C) based on the percentage obtained by a student.<\/p>\n\n\n\n<ul><li>if the percentage is above&nbsp;<strong>90<\/strong>, assign grade&nbsp;<strong>A<\/strong><\/li><li>if the percentage is above&nbsp;<strong>75<\/strong>, assign grade&nbsp;<strong>B<\/strong><\/li><li>if the percentage is above&nbsp;<strong>65<\/strong>, assign grade&nbsp;<strong>C<\/strong><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"if-then\">1. Java if (if-then) Statement<\/h2>\n\n\n\n<p>The syntax of an&nbsp;<strong>if-then<\/strong>&nbsp;statement is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {\n  \/\/ statements\n}<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>condition<\/var>&nbsp;is a boolean expression such as&nbsp;<code>age &gt;= 18<\/code>.<\/p>\n\n\n\n<ul><li>if&nbsp;<var>condition<\/var>&nbsp;evaluates to&nbsp;<code>true<\/code>, statements are executed<\/li><li>if&nbsp;<var>condition<\/var>&nbsp;evaluates to&nbsp;<code>false<\/code>, statements are skipped<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Working of if Statement<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-if-working.png\" alt=\"if the number is greater than 0, code inside if block is executed, otherwise code inside if block is skipped\" title=\"Working of Java if statement\"\/><figcaption>Working of Java if statement<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"if-example\">Example 1: Java if Statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class IfStatement {\n  public static void main(String&#91;] args) {\n\n    int number = 10;\n\n    \/\/ checks if number is less than 0\n    if (number &lt; 0) {\n      System.out.println(\"The number is negative.\");\n    }\n\n    System.out.println(\"Statement outside if block\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Statement outside if block<\/samp><\/code><\/pre>\n\n\n\n<p>In the program,&nbsp;<code>number &lt; 0<\/code>&nbsp;is&nbsp;<code>false<\/code>. Hence, the code inside the parenthesis is&nbsp;<strong>skipped<\/strong>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>\u00a0If you want to learn more about about test conditions, visit\u00a0Java Relational Operators\u00a0and\u00a0Java Logical Operators.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>We can also use\u00a0Java Strings\u00a0as the test condition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Java if with String<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    \/\/ create a string variable\n    String language = \"Java\";\n\n    \/\/ if statement\n    if (language == \"Java\") {\n      System.out.println(\"Best Programming Language\");\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Best Programming Language<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we are comparing two strings in the&nbsp;<code>if<\/code>&nbsp;block.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"if-else\">2. Java if&#8230;else (if-then-else) Statement<\/h2>\n\n\n\n<p>The&nbsp;<code>if<\/code>&nbsp;statement executes a certain section of code if the test expression is evaluated to&nbsp;<code>true<\/code>. However, if the test expression is evaluated to&nbsp;<code>false<\/code>, it does nothing.<\/p>\n\n\n\n<p>In this case, we can use an optional&nbsp;<code>else<\/code>&nbsp;block. Statements inside the body of&nbsp;<code>else<\/code>&nbsp;block are executed if the test expression is evaluated to&nbsp;<code>false<\/code>. This is known as the&nbsp;<strong>if-&#8230;else<\/strong>&nbsp;statement in Java.<\/p>\n\n\n\n<p>The syntax of the&nbsp;<strong>if&#8230;else<\/strong>&nbsp;statement is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition) {\n  \/\/ codes in if block\n}\nelse {\n  \/\/ codes in else block\n}<\/code><\/pre>\n\n\n\n<p>Here, the program will do one task (codes inside&nbsp;<code>if<\/code>&nbsp;block) if the condition is&nbsp;<code>true<\/code>&nbsp;and another task (codes inside&nbsp;<code>else<\/code>&nbsp;block) if the condition is&nbsp;<code>false<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">How the if&#8230;else statement works?<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-if-else-working.png\" alt=\"If the condition is true, the code inside the if block is executed, otherwise, code inside the else block is executed\" title=\"Working of Java if-else statements\"\/><figcaption>Working of Java if-else statements<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"if-else-example\">Example 3: Java if&#8230;else Statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n    int number = 10;\n\n    \/\/ checks if number is greater than 0\n    if (number &gt; 0) {\n      System.out.println(\"The number is positive.\");\n    }\n    \n    \/\/ execute this block\n    \/\/ if number is not greater than 0\n    else {\n      System.out.println(\"The number is not positive.\");\n    }\n\n    System.out.println(\"Statement outside if...else block\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>The number is positive.\nStatement outside if...else block<\/samp><\/pre>\n\n\n\n<p>In the above example, we have a variable named&nbsp;<var>number<\/var>. Here, the test expression&nbsp;<code>number &gt; 0<\/code>&nbsp;checks if&nbsp;<var>number<\/var>&nbsp;is greater than 0.<\/p>\n\n\n\n<p>Since the value of the&nbsp;<var>number<\/var>&nbsp;is&nbsp;<var>10<\/var>, the test expression evaluates to&nbsp;<code>true<\/code>. Hence code inside the body of&nbsp;<code>if<\/code>&nbsp;is executed.<\/p>\n\n\n\n<p>Now, change the value of the&nbsp;<var>number<\/var>&nbsp;to a negative integer. Let&#8217;s say&nbsp;<var>-5<\/var>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int number = -5;<\/code><\/pre>\n\n\n\n<p>If we run the program with the new value of&nbsp;<var>number<\/var>, the output will be:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>The number is not positive.\nStatement outside if...else block<\/samp><\/pre>\n\n\n\n<p>Here, the value of&nbsp;<var>number<\/var>&nbsp;is&nbsp;<var>-5<\/var>. So the test expression evaluates to&nbsp;<code>false<\/code>. Hence code inside the body of&nbsp;<code>else<\/code>&nbsp;is executed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"if-else-ladder\">3. Java if&#8230;else&#8230;if Statement<\/h2>\n\n\n\n<p>In Java, we have an&nbsp;<strong>if&#8230;else&#8230;if<\/strong>&nbsp;ladder, that can be used to execute one block of code among multiple other blocks.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (condition1) {\n  \/\/ codes\n}\nelse if(condition2) {\n  \/\/ codes\n}\nelse if (condition3) {\n  \/\/ codes\n}\n.\n.\nelse {\n  \/\/ codes\n}<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<code>if<\/code>&nbsp;statements are executed from the top towards the bottom. When the test condition is&nbsp;<code>true<\/code>, codes inside the body of that&nbsp;<code>if<\/code>&nbsp;block is executed. And, program control jumps outside the&nbsp;<strong>if&#8230;else&#8230;if<\/strong>&nbsp;ladder.<\/p>\n\n\n\n<p>If all test expressions are&nbsp;<code>false<\/code>, codes inside the body of&nbsp;<code>else<\/code>&nbsp;are executed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">How the if&#8230;else&#8230;if ladder works?<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/sites\/tutorial2program\/files\/java-if-else-if-statement.png\" alt=\"If the first test condition if true, code inside first if block is executed, if the second condition is true, block inside second if is executed, and if all conditions are false, the else block is executed\" title=\"Working of if...else...if ladder\"\/><figcaption>Working of if&#8230;else&#8230;if ladder<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"if-else-ladder-example\">Example 4: Java if&#8230;else&#8230;if Statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    int number = 0;\n\n    \/\/ checks if number is greater than 0\n    if (number &gt; 0) {\n      System.out.println(\"The number is positive.\");\n    }\n\n    \/\/ checks if number is less than 0\n    else if (number &lt; 0) {\n      System.out.println(\"The number is negative.\");\n    }\n    \n    \/\/ if both condition is false\n    else {\n      System.out.println(\"The number is 0.\");\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The number is 0.<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we are checking whether&nbsp;<var>number<\/var>&nbsp;is&nbsp;<strong>positive<\/strong>,&nbsp;<strong>negative<\/strong>, or&nbsp;<strong>zero<\/strong>. Here, we have two condition expressions:<\/p>\n\n\n\n<ul><li><code>number &gt; 0<\/code>&nbsp;&#8211; checks if&nbsp;<var>number<\/var>&nbsp;is greater than&nbsp;<var>0<\/var><\/li><li><code>number &lt; 0<\/code>&nbsp;&#8211; checks if&nbsp;<var>number<\/var>&nbsp;is less than&nbsp;<var>0<\/var><\/li><\/ul>\n\n\n\n<p>Here, the value of&nbsp;<var>number<\/var>&nbsp;is&nbsp;<var>0<\/var>. So both the conditions evaluate to&nbsp;<code>false<\/code>. Hence the statement inside the body of&nbsp;<code>else<\/code>&nbsp;is executed.<\/p>\n\n\n\n<p><strong>Note<\/strong>: Java provides a special operator called\u00a0<strong>ternary operator<\/strong>, which is a kind of shorthand notation of\u00a0<strong>if&#8230;else&#8230;if<\/strong>\u00a0statement. To learn about the ternary operator, visit\u00a0Java Ternary Operator.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"nested-if-else\">4. Java Nested if..else Statement<\/h2>\n\n\n\n<p>In Java, it is also possible to use&nbsp;<code>if..else<\/code>&nbsp;statements inside an&nbsp;<code>if...else<\/code>&nbsp;statement. It&#8217;s called the nested&nbsp;<code>if...else<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>Here&#8217;s a program to find the largest of&nbsp;<strong>3<\/strong>&nbsp;numbers using the nested&nbsp;<code>if...else<\/code>&nbsp;statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5: Nested if&#8230;else Statement<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ declaring double type variables\n    Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largest;\n\n    \/\/ checks if n1 is greater than or equal to n2\n    if (n1 &gt;= n2) {\n\n      \/\/ if...else statement inside the if block\n      \/\/ checks if n1 is greater than or equal to n3\n      if (n1 &gt;= n3) {\n        largest = n1;\n      }\n\n      else {\n        largest = n3;\n      }\n    } else {\n\n      \/\/ if..else statement inside else block\n      \/\/ checks if n2 is greater than or equal to n3\n      if (n2 &gt;= n3) {\n        largest = n2;\n      }\n\n      else {\n        largest = n3;\n      }\n    }\n\n    System.out.println(\"Largest Number: \" + largest);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Largest Number: 4.5<\/samp><\/code><\/pre>\n\n\n\n<p>In the above programs, we have assigned the value of variables ourselves to make this easier.<\/p>\n\n\n\n<p>However, in real-world applications, these values may come from user input data, log files, form submission, etc.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about control flow statements using Java if and if&#8230;else statements with the help of examples. In programming, we use the&nbsp;if..else&nbsp;statement to run a block of code among more than one alternatives. For example, assigning grades (A, B, C) based on the percentage obtained by a student. if the percentage [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[363],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1160"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1160"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1160\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}