Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Java if-else Statements

    Java Programming Tutorials


    If else statements in Java is also used to control the program flow based on some condition, only the difference is: it's used to execute some statement code block if the expression is evaluated to true, otherwise executes else statement code block.

    The basic format of if else statement is:

    Syntax:

    if(test_expression)
    {
       //execute your code
    }
    else
    {
       //execute your code
    }

    Figure - Flowchart of if-else Statement:

    java-if-else

    Example of a Java Program to Demonstrate If else statements

    Example:

    public class Sample {
    
     public static void main(String args[]) {
      int a = 80, b = 30;
    
      if (b > a) {
       System.out.println("b is greater");
      } else {
       System.out.println("a is greater");
      }
     }
    }

    Program Output:

    java-if-else-statements



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.