{"id":1648,"date":"2017-04-27T14:03:55","date_gmt":"2017-04-27T08:33:55","guid":{"rendered":"http:\/\/www.java2blog.com\/?p=1648"},"modified":"2023-11-09T22:47:35","modified_gmt":"2023-11-09T17:17:35","slug":"if-else-statement-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/if-else-statement-java\/","title":{"rendered":"if else statement in java"},"content":{"rendered":"<p>If else statements in java are used to test some conditions. It evaluates a condition to be <code>true<\/code> or <code>false<\/code>.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Simple_If_statement\">Simple If statement<\/a><\/li><li><a href=\"#If_else_statement\">If else statement<\/a><\/li><li><a href=\"#If_else_if_ladder_statement\">If else if ladder statement<\/a><\/li><li><a href=\"#Conditional_Operators\">Conditional Operators<\/a><\/li><\/ul><\/div>\n\nThere are three types of if statements.<\/p>\n<ol>\n<li>If statement<\/li>\n<li>If else statement<\/li>\n<li>If\u00a0else if ladder statement<\/li>\n<\/ol>\n<h2><span id=\"Simple_If_statement\">Simple If statement<\/span><\/h2>\n<pre name=\"code\" class=\"\">if(condition)\n{\n      \/\/ If condition is true, this block will be executed\n}\n<\/pre>\n<p><strong>For example:<\/strong><\/p>\n<pre name=\"code\" class=\"java\">public class IfMain {\n\n public static void main(String[] args)\n {\n    int price=20;\n    if(price &gt; 10)\n         System.out.println(\"Price is greater than 10\");\n  }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-purple\">Price is greater than 10<\/div>\n<blockquote><p>You can write single line followed by semi color after if or you can use curly braces to write block of code.<\/p><\/blockquote>\n<p>You can rewrite above code as below.<\/p>\n<pre name=\"code\" class=\"java\">public class IfMain {\n\n    public static void main(String[] args)\n    {\n        int price=20;\n        if(price &gt; 10)\n        {\n                      System.out.println(\"Price is greater than 10\");\n                }\n    }\n}\n<\/pre>\n<h2><span id=\"If_else_statement\">If else statement<\/span><\/h2>\n<pre name=\"code\" class=\"\">if(condition)\n{\n       \/\/ If condition is true, this block will be executed\n}\nelse\n{\n       \/\/ If condition is false, this block will be executed\n}\n<\/pre>\n<p><strong>For example:<\/strong><br \/>\nCheck if number is odd or even.<\/p>\n<pre name=\"code\" class=\"java\">package org.arpit.java2blog;\n\npublic class IfElseMain {\n\n    public static void main(String[] args)\n    {\n        IfElseMain ieMain=new IfElseMain();\n        ieMain.checkOddOrEven(20);\n    }\n\n    public void checkOddOrEven(int number)\n    {\n            if(number%2==0)\n            {\n                         System.out.println(number+\" is even\");\n            }\n            else\n            {\n                     System.out.println(number+\" is odd\");\n            }\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-gray\">20 is even<\/div>\n<h2><span id=\"If_else_if_ladder_statement\">If else if ladder statement<\/span><\/h2>\n<p>Before we learn about If else if ladder statement, let&#8217;s first see some conditional operator.<\/p>\n<h2><span id=\"Conditional_Operators\">Conditional Operators<\/span><\/h2>\n<p>Java provides many conditional operators.Some of them are:<\/p>\n<ul>\n<li><strong>==<\/strong> \u00a0: To check if two variables are equal<\/li>\n<li><strong>!=<\/strong> \u00a0 : To check if two variables are not equal<\/li>\n<li><strong>&lt; \u00a0<\/strong> \u00a0:\u00a0To check if first\u00a0variable is less than other<\/li>\n<li><strong>&lt;=<\/strong>\u00a0:\u00a0To check if first\u00a0variable is less than or equal to other<\/li>\n<li><strong>&gt;<\/strong> \u00a0 : \u00a0To check if first\u00a0variable is greater\u00a0than other<\/li>\n<li><strong>&gt;=<\/strong>\u00a0: \u00a0To check if first\u00a0variable is greater\u00a0than or equal to other<\/li>\n<li><strong>&amp;&amp;<\/strong> : \u00a0And operation is used to check if both conditions,used with &amp;&amp; operator, are true<\/li>\n<li><strong>||<\/strong> \u00a0 \u00a0: \u00a0or\u00a0 operation is used to check if one of the\u00a0conditions,used with || operator, are true<\/li>\n<\/ul>\n<pre name=\"code\" class=\"\">if(condition1)\n{\n     \/\/ If condition1 is true, this block will be executed.\n}\nelse if(condition2)\n{\n     \/\/ If condition2 is true, this block will be executed.\n}\nelse if(condition3)\n{\n     \/\/ If condition3 is true, this block will be executed.\n}\nelse \n{ \n      \/\/ If all above conditions are false, this block will be executed. \n}\n<\/pre>\n<p><strong>For example:<\/strong><\/p>\n<pre name=\"code\" class=\"\">package org.arpit.java2blog;\n\npublic class IfElseIfLadderMain {\n\n    public static void main(String[] args)\n    {\n        int age=28;\n        if(age &gt;= 18 &amp;&amp; age &lt;= 25) { \n            System.out.println(\"Age is between 18 and 25\"); \n        } \n        else if(age &gt;= 26 &amp;&amp; age &lt;= 35) { \n            System.out.println(\"Age is between 26 and 35\"); \n        } \n        else if(age &gt;= 36 &amp;&amp; age &lt;= 60){\n            System.out.println(\"Age is between 35 and 60\");\n        }\n        else{\n            System.out.println(\"Age is greater than 60\");\n        }\n    }\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">Age is between 26 and 35<\/div>\n<p>That&#8217;s all about if else statement in java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsSimple If statementIf else statementIf else if ladder statementConditional Operators If else statements in java are used to test some conditions. It evaluates a condition to be true or false. There are three types of if statements. If statement If else statement If\u00a0else if ladder statement Simple If statement if(condition) { \/\/ If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[129],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/1648"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=1648"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/1648\/revisions"}],"predecessor-version":[{"id":25360,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/1648\/revisions\/25360"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=1648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=1648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=1648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}