Questions And Answers

More Tutorials

Java If-else Statement

Sometimes we want to drink coffee when we feel sleepy. Sometimes, we order junk food if it is our friend’s birthday. You might want to buy an umbrella if it’s raining. All these decisions depend on a certain condition being met. Similar to real life, we can execute some instructions only when a condition is met in programming also. If-else block is used to check conditions and execute a particular section of code for a specific condition.
Flow control of if-else in Java :

Decision-making instructions in Java
  • If-Else Statement

  • Switch Statement

  • If-Else Statement
    Syntax of If-else statement in Java :

    /* if (condition-to-be-checked) {
    else {
    } */


    int a = 29;
    if (a>18) {
    	System.out.println(“You can drive”);
           System.out.println(“You are underage!");


    You can drive

    If-else ladder :

  • Instead of using multiple if statements, we can also use else if along with if thus forming an if-else-if-else ladder.

  • Using such kind of logic reduces indents.

  • Last else is executed only if all the conditions fail

  • /* if (condition1) {
    else if {
                // Statements;
    else {
    } */


    In this page (written and validated by ) you learned about Java If-else Statement . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Relational and Logical Operators.

    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

    Share On:

    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.