{"id":81,"date":"2019-01-16T23:49:41","date_gmt":"2019-01-16T23:49:41","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/java\/?p=81"},"modified":"2020-08-06T09:53:15","modified_gmt":"2020-08-06T09:53:15","slug":"13-java-conditional-statements","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/","title":{"rendered":"Java &#8211; Conditional Statements"},"content":{"rendered":"<p>We would consider if statements in this lesson. Then we would also look at switch statement. Finally, we would examine the conditional operator.<\/p>\n<p>So Java provides decision-making constructs.<\/p>\n<ol>\n<li><a href=\"#t1\">if Statement.<\/a><\/li>\n<li><a href=\"#t2\">If..else Statement.<\/a><\/li>\n<li><a href=\"#t3\">Nested if Statement.<\/a><\/li>\n<li><a href=\"#t4\">switch Statement<\/a><\/li>\n<li><a href=\"#t5\">Conditional if Operator (? : )<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. If Statement<\/strong><\/h4>\n<p>Almost every program needs to make decisions at some point. We use if statement for making decisions. The if statement consist of a conditional statement. Then block of one or more statements enclosed in curly braces.<\/p>\n<p>&nbsp;<\/p>\n<p>The structure is given below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">     <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>condition<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   \t<span style=\"color: #888888;\">\/\/body of if statement<\/span>\r\n     <span style=\"color: #333333;\">}<\/span>\r\n     <span style=\"color: #888888;\">\/\/ other codes<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can see the flow diagram for the if statement in Figure 1.0.<\/p>\n<figure id=\"attachment_82\" aria-describedby=\"caption-attachment-82\" style=\"width: 277px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-82 \" src=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/If-STatement-in-Java.jpg\" alt=\"Diagram for the if statement in java\" width=\"277\" height=\"291\" srcset=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/If-STatement-in-Java.jpg 548w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/If-STatement-in-Java-286x300.jpg 286w\" sizes=\"auto, (max-width: 277px) 100vw, 277px\" \/><figcaption id=\"caption-attachment-82\" class=\"wp-caption-text\">Figure 1.0: Flow Diagram for the if Statement<\/figcaption><\/figure>\n<p>Let&#8217;s write a program to illustrate the if statement. The program would decide if we should take an umbrella or not. So the condition would be the weather. If the weather is rainy, then we take an umbrella.<\/p>\n<p>You can run copy and run the program yourself.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Tester<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    String weather <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"rainy\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t    \r\n\t    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>weather <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">\"rainy\"<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    \tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Take and umbrella\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t    <span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.0: Program to illustrate the if statement<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. if&#8230;else Statement<\/strong><\/h4>\n<p>Next, lets&#8217; consider the if&#8230;else statement. Unlike the if statement, the if&#8230;else statement has two parts: the if part and the else part.<\/p>\n<ul>\n<li>The if part contains the condition. Then followed by code to execute if the condition is true.<\/li>\n<li>The else part contains code to execute if condition is false<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>I present the syntax in below.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>condition<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t <span style=\"color: #888888;\">\/\/code to execute if condition is false<\/span>\r\n\t<span style=\"color: #333333;\">}<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t <span style=\"color: #888888;\">\/\/code to execute if condition is false<\/span>\r\n        <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now, also look at the flow diagram for the if..else statement. Notice that it has two blocks of statements. This is shown in Figure 1.1 below<\/p>\n<figure id=\"attachment_83\" aria-describedby=\"caption-attachment-83\" style=\"width: 308px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-83 \" src=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/if-else-statements-in-Java.jpg\" alt=\"Diagram for if...else Statement in Java\" width=\"308\" height=\"342\" srcset=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/if-else-statements-in-Java.jpg 594w, https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/if-else-statements-in-Java-271x300.jpg 271w\" sizes=\"auto, (max-width: 308px) 100vw, 308px\" \/><figcaption id=\"caption-attachment-83\" class=\"wp-caption-text\">Figure 1.1: Diagram for if&#8230;else Statement in Java<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s write a program to illustrate the if&#8230;else statement. So the program would have two parts. First code for if weather is sunny. Then code for if weather is rainy. For rainy weather, we say &#8216;take an umbrella&#8217;. Similarly, for sunny weather, we say &#8216;wear your sunglasses&#8217;.<\/p>\n<p>&nbsp;<\/p>\n<p>You can observe the code in Listing 1.0.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Tester<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    String weather <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"rainy\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t    \r\n\t    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>weather <span style=\"color: #333333;\">==<\/span> <span style=\"background-color: #fff0f0;\">\"rainy\"<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    \tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Take and umbrella\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t    <span style=\"color: #333333;\">}<\/span>\r\n\t    <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #0066bb; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>weather <span style=\"color: #333333;\">==<\/span><span style=\"background-color: #fff0f0;\">\"sunny\"<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    \tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Wear your sunglasses\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n\t    <span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.1: Code to Illustate if&#8230;else Statement<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Nested if Statement<\/strong><\/h4>\n<p>Sometimes, you may want to have an if statement inside an if statement. As such, you want to test two conditions. Take for example, you want to find Form 6 students that score above 70 in a test. You can use the code below to achieve that.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Tester<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    <span style=\"color: #333399; font-weight: bold;\">double<\/span> score <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">76<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t    <span style=\"color: #333399; font-weight: bold;\">int<\/span> form <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t    \r\n\t    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>form <span style=\"color: #333333;\">==<\/span> <span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    \tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"He is in form 6\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t    \t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>score <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">70<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t    \t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"His score is also above 70\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t    \t<span style=\"color: #333333;\">}<\/span>\r\n\t    <span style=\"color: #333333;\">}<\/span>\r\n   <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.2: Program to Illustrate Nested if Statements<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. switch Statement<\/strong><\/h4>\n<p>I&#8217;m sure you will find this interesting. Because switch statement makes for a more readable code. Its a better alternative to if statements sometimes.<\/p>\n<p>&nbsp;<\/p>\n<p>So I present the syntax below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">   <span style=\"color: #008800; font-weight: bold;\">switch<\/span><span style=\"color: #333333;\">(<\/span>condition<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   \t<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #997700; font-weight: bold;\">value1:<\/span>\r\n   \t\t<span style=\"color: #888888;\">\/\/statement<\/span>\r\n   \t\t<span style=\"color: #008800; font-weight: bold;\">break<\/span>\r\n   \t<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #997700; font-weight: bold;\">value2:<\/span>\r\n   \t\t<span style=\"color: #888888;\">\/\/statement<\/span>\r\n   \t\t<span style=\"color: #008800; font-weight: bold;\">break<\/span>\r\n   \t\t<span style=\"color: #333333;\">...<\/span>\r\n   \t\t<span style=\"color: #333333;\">...<\/span>\r\n   \t<span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #008800; font-weight: bold;\">default<\/span><span style=\"color: #333333;\">:<\/span>\r\n   \t\t<span style=\"color: #888888;\">\/\/default statment\t   <\/span>\r\n   <span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.3: Syntax for the Switch statement<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Rules for the switch Statement<\/strong><\/p>\n<p>Finally on the switch statement, here are the rules that apply:<\/p>\n<ul>\n<li>values must be either integer, string or enum<\/li>\n<li>Case statement if followed by a colon while statements comes under<\/li>\n<li>Also, break statement comes after all case blocks<\/li>\n<li>The default statement is optional<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Now, let&#8217;s write\u00a0 a program to prepare a grade comment using switch statement. It is going to work this way:<\/p>\n<ul>\n<li>Grade A: Comment = &#8220;Excellent&#8221;<\/li>\n<li>Grade B: Comment = &#8220;Good&#8221;<\/li>\n<li>But Grade C: Comment = &#8220;Fair&#8221;<\/li>\n<li>Then grade D: Comment = &#8220;Pass&#8221;<\/li>\n<li>Finally for grade E: Comment = &#8220;Poor&#8221;<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>I have written the code for you in Listing 1.4. I therefore recommend you make sure to try it yourself.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Tester<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String args<span style=\"color: #333333;\">[])<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">char<\/span> grade <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0044dd;\">'C'<\/span><span style=\"color: #333333;\">;<\/span>  \r\n   <span style=\"color: #008800; font-weight: bold;\">switch<\/span><span style=\"color: #333333;\">(<\/span>grade<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'A'<\/span><span style=\"color: #333333;\">:<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Excellent\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t  <span style=\"color: #008800; font-weight: bold;\">break<\/span><span style=\"color: #333333;\">;<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'B'<\/span><span style=\"color: #333333;\">:<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Good\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t  <span style=\"color: #008800; font-weight: bold;\">break<\/span><span style=\"color: #333333;\">;<\/span>   \r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'C'<\/span><span style=\"color: #333333;\">:<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Fair\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t  <span style=\"color: #008800; font-weight: bold;\">break<\/span><span style=\"color: #333333;\">;<\/span>   \r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'D'<\/span><span style=\"color: #333333;\">:<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Pass\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t  <span style=\"color: #008800; font-weight: bold;\">break<\/span><span style=\"color: #333333;\">;<\/span>   \r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'E'<\/span><span style=\"color: #333333;\">:<\/span>\r\n\t  System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Poor\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n\t  <span style=\"color: #008800; font-weight: bold;\">break<\/span><span style=\"color: #333333;\">;<\/span>   \r\n   <span style=\"color: #008800; font-weight: bold;\">default<\/span><span style=\"color: #333333;\">:<\/span>\r\n\t   System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Not a valid grade\"<\/span><span style=\"color: #333333;\">);<\/span>\t\t  \r\n   <span style=\"color: #333333;\">}<\/span>\r\n\t   \r\n   <span style=\"color: #333333;\">}<\/span> \r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Listing 1.4: Switch Statement<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. The Conditional Operator (? : )<\/strong><\/h4>\n<p>You can call this a one line if-statement. Although it has been covered in under <a href=\"https:\/\/www.kindsonthegenius.com\/java\/java-basic-operators\/\">Basic Java Operators,<\/a> we want to give a review.<\/p>\n<p>&nbsp;<\/p>\n<p>This operator is of the form:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">\t <span style=\"color: #333399; font-weight: bold;\">int<\/span> a <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span><span style=\"color: #333333;\">;<\/span>\r\n\t <span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> a<span style=\"color: #333333;\">&gt;<\/span><span style=\"color: #0000dd; font-weight: bold;\">20<\/span> <span style=\"color: #333333;\">?<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">20<\/span><span style=\"color: #333333;\">;<\/span> \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>What is means is\u00a0 this:<\/p>\n<ul>\n<li>if a is less than 20, then x =10<\/li>\n<li>Otherwise x = 20<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We would consider if statements in this lesson. Then we would also look at switch statement. Finally, we would examine the conditional operator. So Java &hellip; <\/p>\n","protected":false},"author":395,"featured_media":84,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-81","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java - Conditional Statements - Java Tutorials<\/title>\n<meta name=\"description\" content=\"In this lesson we cover the conditional statements in Java. They include: if statements, if\/else statements, nested if statement and switch statements\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java - Conditional Statements - Java Tutorials\" \/>\n<meta property=\"og:description\" content=\"In this lesson we cover the conditional statements in Java. They include: if statements, if\/else statements, nested if statement and switch statements\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/\" \/>\n<meta property=\"og:site_name\" content=\"Java Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-16T23:49:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-08-06T09:53:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Conditional-Statements-in-Java.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"958\" \/>\n\t<meta property=\"og:image:height\" content=\"518\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Java &#8211; Conditional Statements\",\"datePublished\":\"2019-01-16T23:49:41+00:00\",\"dateModified\":\"2020-08-06T09:53:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/\"},\"wordCount\":610,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Conditional-Statements-in-Java.jpg\",\"articleSection\":[\"Java Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/\",\"name\":\"Java - Conditional Statements - Java Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Conditional-Statements-in-Java.jpg\",\"datePublished\":\"2019-01-16T23:49:41+00:00\",\"dateModified\":\"2020-08-06T09:53:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"In this lesson we cover the conditional statements in Java. They include: if statements, if\\\/else statements, nested if statement and switch statements\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Conditional-Statements-in-Java.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/wp-content\\\/uploads\\\/sites\\\/9\\\/2019\\\/01\\\/Conditional-Statements-in-Java.jpg\",\"width\":958,\"height\":518},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/13-java-conditional-statements\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java &#8211; Conditional Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/\",\"name\":\"Java Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/java\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java - Conditional Statements - Java Tutorials","description":"In this lesson we cover the conditional statements in Java. They include: if statements, if\/else statements, nested if statement and switch statements","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/","og_locale":"en_US","og_type":"article","og_title":"Java - Conditional Statements - Java Tutorials","og_description":"In this lesson we cover the conditional statements in Java. They include: if statements, if\/else statements, nested if statement and switch statements","og_url":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/","og_site_name":"Java Tutorials","article_published_time":"2019-01-16T23:49:41+00:00","article_modified_time":"2020-08-06T09:53:15+00:00","og_image":[{"width":958,"height":518,"url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Conditional-Statements-in-Java.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Java &#8211; Conditional Statements","datePublished":"2019-01-16T23:49:41+00:00","dateModified":"2020-08-06T09:53:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/"},"wordCount":610,"commentCount":0,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Conditional-Statements-in-Java.jpg","articleSection":["Java Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/","url":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/","name":"Java - Conditional Statements - Java Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Conditional-Statements-in-Java.jpg","datePublished":"2019-01-16T23:49:41+00:00","dateModified":"2020-08-06T09:53:15+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"In this lesson we cover the conditional statements in Java. They include: if statements, if\/else statements, nested if statement and switch statements","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Conditional-Statements-in-Java.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/java\/wp-content\/uploads\/sites\/9\/2019\/01\/Conditional-Statements-in-Java.jpg","width":958,"height":518},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/java\/13-java-conditional-statements\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/java\/"},{"@type":"ListItem","position":2,"name":"Java &#8211; Conditional Statements"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/java\/#website","url":"https:\/\/www.kindsonthegenius.com\/java\/","name":"Java Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/java\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/java\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/java\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":6,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":202,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/posts\/81\/revisions\/202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media\/84"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/java\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}