Title: Simple if else Java Example - Java Code Geeks
Open Graph Title: Simple if else Java Example - Java Code Geeks
Description: In this post, we feature a simple if else Java Example. The Java if statement is used to test a boolean condition i.e. true or false. 1. Introduction The
Open Graph Description: In this post, we feature a simple if else Java Example. The Java if statement is used to test a boolean condition i.e. true or false. 1. Introduction The
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/if-else-java-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/if-else-java-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/if-else-java-example/"},"author":{"name":"Yatin","@id":"https://examples.javacodegeeks.com/#/schema/person/9874407a37b028e8be3276e2b5960d13"},"headline":"Simple if else Java Example","datePublished":"2012-11-11T19:19:57+00:00","dateModified":"2020-04-06T10:59:07+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/if-else-java-example/"},"wordCount":473,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/if-else-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","keywords":["if else statement","java basics"],"articleSection":["if/else statement"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/if-else-java-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/if-else-java-example/","url":"https://examples.javacodegeeks.com/if-else-java-example/","name":"Simple if else Java Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/if-else-java-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/if-else-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2012-11-11T19:19:57+00:00","dateModified":"2020-04-06T10:59:07+00:00","description":"In this post, we feature a simple if else Java Example. The Java if statement is used to test a boolean condition i.e. true or false. 1. Introduction The","breadcrumb":{"@id":"https://examples.javacodegeeks.com/if-else-java-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/if-else-java-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/if-else-java-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/if-else-java-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Java Basics","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/"},{"@type":"ListItem","position":4,"name":"if/else statement","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/if-else-statement/"},{"@type":"ListItem","position":5,"name":"Simple if else Java Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/9874407a37b028e8be3276e2b5960d13","name":"Yatin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2023/09/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2023/09/cropped-Yatin-Batra_avatar_1515758148-96x96.jpg","caption":"Yatin"},"description":"An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https://www.javacodegeeks.com"],"url":"https://examples.javacodegeeks.com/author/yatin-batra/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Java Basics","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/"}},{"@type":"ListItem","position":4,"item":{"name":"if\/else statement","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/if-else-statement\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2012-11-11T19:19:57+02:00","datePublished":"2012-11-11T19:19:57+02:00","dateModified":"2020-04-06T13:59:07+03:00","headline":"Simple if else Java Example","name":"Simple if else Java Example","keywords":"if else statement,java basics","url":"https:\/\/examples.javacodegeeks.com\/if-else-java-example\/","description":"In this post, we feature a simple if else Java Example. The Java if statement is used to test a boolean condition i.e. true or false. 1. Introduction The logic inside the if condition executes when th","copyrightYear":"2012","articleSection":"if\/else statement","articleBody":"\nIn this post, we feature a simple if else Java Example. The Java if statement is used to test a boolean condition i.e. true or false.\n\n\n\n1. Introduction\n\n\n\nThe logic inside the if condition executes when the condition is true otherwise the else block is executed. Java supports various types of if statements.\n\n\n\nif statementif else statementif else if statementnested if statement\n\n\n\n\n\n\n\nTo start with this tutorial, we are hoping that users at present have their preferred IDE and JDK 1.8 installed on their machines. For easy usage, I am using Eclipse IDE.\n\n\n\n2. Java If else Example\n\n\n\nNow let us start with different code snippets to under the if else example in Java programming language.\n\n\n\n2.1 Java if Statement\n\n\n\nThe if statement tests the condition and executes the conditional block only if the condition evaluates to true. Let us understand this with a simple code snippet.\n\n\n\nSnippet\n\n\n\npackage com.jcg.ifelse;\npublic class Example1 {\n public static void main(String[] args) {\n \/\/ Defining a salary variable for the employee.\n int salary= 2000; \n \/\/ Checking the employee salary. \n if(salary > 1000) { \n System.out.println(\"Salary is greater than 1000.\"); \n } \n }\n}\n\n\n\n\nIn this snippet, the output will only be printed if the employee salary is greater than 1000.\n\n\n\n2.2 Java if else Statement\n\n\n\nThe if else statement tests the condition and executes the if block if the condition evaluates to true otherwise the else block, is executed. Let us understand this with a simple code snippet.\n\n\n\nSnippet\n\n\n\npackage com.jcg.ifelse;\npublic class Example2 {\n public static void main(String[] args) {\n boolean b = false;\n if (b) {\n System.out.println(\"Variable value is true.\");\n } else {\n System.out.println(\"Variable value is NOT true.\");\n }\n }\n}\n\n\n\n\nIn this snippet, the output will be printed based on the boolean value.\n\n\n\n2.3 Java if else if Statement\n\n\n\nThe if else if statement test one condition from multiple statements. Let us understand this with a simple code snippet.\n\n\n\nSnippet\n\n\n\npackage com.jcg.ifelse;\npublic class Example3 {\n public static void main(String[] args) {\n \/\/ Defining a salary variable for the employee.\n int salary= 1250;\n \/\/ Checking the employee salary.\n if(salary < 500) { \n System.out.println(\"Salary is less than 500.\");\n } else if (salary >= 500 && salary < 1000) {\n System.out.println(\"Salary is greater than 500 but less than 1000.\");\n } else if (salary >= 1000 && salary < 2000) {\n System.out.println(\"Salary is greater than 1000 but less than 2000.\");\n } else if (salary > 2000) {\n System.out.println(\"Salary is greater than 2000.\");\n } else {\n System.out.println(\"Invalid!\");\n }\n }\n}\n\n\n\n\nIn this snippet, the output will be printed based on the employee salary.\n\n\n\n2.4 Java nested-if Statement\n\n\n\nThe nested-if statement consists of if block within another if block. Here the inner if condition executes only when the outer if the condition evaluates to true. Let us understand this with a simple code snippet.\n\n\n\nSnippet\n\n\n\npackage com.jcg.ifelse;\npublic class Example4 {\n public static void main(String[] args) {\n \/\/ Defining a salary and designation variable for the employee.\n int salary= 1250;\n String department= \"Technology\";\n \/\/ Checking the employee salary.\n if(salary > 1000) { \n if(department.equalsIgnoreCase(\"technology\")) {\n System.out.println(\"Employee is in technology department.\");\n } else {\n System.out.println(\"Employee is not in technology department.\");\n }\n } else {\n System.out.println(\"Invalid!\");\n }\n }\n}\n\n\n\n\nIn this snippet, the inner block will execute only when the outer if condition evaluates to true.\n\n\n\n3. Switch Case\n\n\n\nThe switch case in Java language executes one statement from multiple conditions. It is like an if else if statement. Make note,\n\n\n\nDuplicate case values are not allowedThe default case is optionalThe break keyword is used to end the case statement sequence. If omitted, the execution goes onto the next case\n\n\n\nLet us understand this with a simple code snippet.\n\n\n\nSnippet\n\n\n\npackage com.jcg.ifelse;\npublic class Example5 {\n public static void main(String[] args) {\n int number= 5;\n switch(number) {\n case 2: \n System.out.println(\"2\");\n break;\n case 5:\n System.out.println(\"5\");\n break;\n case 8:\n System.out.println(\"8\");\n break;\n default:\n System.out.println(\"Invalid choice!\");\n break;\n }\n }\n}\n\n\n\n\nIn this snippet, the output will be printed based on the passed switch case condition. That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!\n\n\n\n4. Conclusion\n\n\n\nIn this tutorial, we had an in-depth look at the if else statement. Developers can download the sample application as an Eclipse project in the Downloads section.\n\n\n\n5. Download the Eclipse Project\n\n\n\nThis was an example of the if else statement.\n\n\n\nDownloadYou can download the full source code of this example here: Simple if-else Java Example\n\n\n\nLast updated on Aug. 22, 2019\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/if-else-java-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Yatin","url":"https:\/\/examples.javacodegeeks.com\/author\/yatin-batra\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2012-11-11T19:19:57+00:00 |
| article:modified_time | 2020-04-06T10:59:07+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Yatin |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Yatin |
| twitter:label2 | Est. reading time |
| twitter:data2 | 4 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1