{"id":2197,"date":"2017-05-31T12:28:00","date_gmt":"2017-05-31T12:28:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2197"},"modified":"2024-11-16T14:55:31","modified_gmt":"2024-11-16T14:55:31","slug":"write-a-java-program-with-if-else-statement","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-with-if-else-statement\/","title":{"rendered":"Write a java program with if-else statement?"},"content":{"rendered":"<h3>Overview<\/h3>\r\n<p>If-else statement is a conditional statement. If the expression is true, then the true block is executed and if the expression is false, then the else block is executed. Let\u2019s write a simple java program making use of the if-else statement.<\/p>\r\n<h3>Java Program<\/h3>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner;\r\n\r\n\/\/********************************\r\n\/\/IfElseDemo.java\r\n\/\/********************************\r\npublic class IfElseDemo {\r\n\r\n  public static void main(String[] args) {\r\n    Scanner keyboard = new Scanner(System.in);\r\n    int a,b;\r\n    System.out.print(\"Enter a := \");\r\n    a = keyboard.nextInt();\r\n    System.out.print(\"Enter b := \");\r\n    b = keyboard.nextInt();\r\n\r\n    if(a&gt;b) {\r\n      System.out.println(\"a(\" + a + \") is bigger than b(\" + b + \")\");\r\n    }else {\r\n      System.out.println(\"b(\" + b + \") is bigger than a(\" + a + \")\");\r\n    }\r\n  }\r\n}\r\n<\/pre>\r\n<p>&nbsp;<\/p>\r\n<h3>Program Output<\/h3>\r\n<p>Enter a := 27<br \/>Enter b := 36<br \/>b(36) is bigger than a(27)<\/p>\r\n<h3>Screenshot<\/h3>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2206\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/if-else-statement-java-program.png\" alt=\"\" width=\"1736\" height=\"1029\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/if-else-statement-java-program.png 1736w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/if-else-statement-java-program-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/if-else-statement-java-program-1024x607.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/if-else-statement-java-program-768x455.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/if-else-statement-java-program-1536x910.png 1536w\" sizes=\"auto, (max-width: 1736px) 100vw, 1736px\" \/><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>Java Tutorial on this website: <strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\r\n<p>For more information on Java, visit the official website :<\/p>\r\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\r\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2197\" data-postid=\"2197\" class=\"themify_builder_content themify_builder_content-2197 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Overview If-else statement is a conditional statement. If the expression is true, then the true block is executed and if the expression is false, then the else block is executed. Let\u2019s write a simple java program making use of the if-else statement. Java Program import java.util.Scanner; \/\/******************************** \/\/IfElseDemo.java \/\/******************************** public class IfElseDemo { public static [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2197","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2197"}],"version-history":[{"count":7,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2197\/revisions"}],"predecessor-version":[{"id":21040,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2197\/revisions\/21040"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}