Java If-else

The if-else statement in Java is the most basic of all the flow control statements. An if-else statement tells the program to execute a certain block only if a particular test evaluates to true, else execute the alternate block if the condition is false.

The if and else are reserved keywords in Java, and cannot be used as other identifiers.

1. Syntax

A simple if-else statement is written as follows. It starts with a mandatory if statement, followed by an optional else part.

if (condition) {

        //statement-1
} else {

        //statement-2
}

The condition must be a boolean expression and must evaluate to either true or false. If the condition evaluates to true, statement-1 is executed. Otherwise, statement-2 is executed.

Note that the else block is optional. We may write a statement as :

if (condition) {
        //statement-1
}

2. The If-else Example

Let’s see an example of an if-else statement. In the following program, we check whether the employee’s age is greater than 18. On both bases, we print that the employee is a minor or an adult.

int age = employee.getAge();

if(age > 18) {
  System.out.println("Employee is adult");
}
else {
  System.out.println("Employee is minor");
}

3. Nested If-else Statements

The if-else statements can be nested as well. The inner if-else statements will be executed based on the evaluation results of the outer conditional statements.

In the following program, we are using else-if statement to add an additional conditional that will be evaluated only when the first condition in if block is evaluated as false.

int age = employee.getAge();

if(age > 60) {

  System.out.println("Employee is retired");
} else if(age > 18) {  //Executes only when if condition is false i.e. age is less than 60

  System.out.println("Employee is adult");
} else {

  System.out.println("Employee is minor");
}

4. Use Curly Braces to Group Multiple Statements

Consider the following program:

int num1, num2, num3 = 10;

if (num1 > 40)
        num2 = num2 + 10;
        num3 = num3 + 10;
else
        num2 = num2 - 10;
        num3 = num3 - 10;

The above program will not compile. What’s wrong with the above program? The answer is that we can place only one statement between if and else, in any if-else statement without using the curly braces.

In case of multiple statements, we must bundle the statements into one block statement using curly braces, like so:

if (num1 > 40) {
        num2 = num2 + 10;
        num3 = num3 + 10;
} else {
        num2 = num2 - 10;
        num3 = num3 - 10;
}

5. Using Ternary Operator to Replace Simple if-else

We can also use the ternary operator instead of a simple if-else statement to make the code more concise and readable.

Consider a simple if-else statement that checks if a number is greater than or less than a value, and stores the value in a variable.

boolean isAdult;

if (age > 18) {
  isAdult = true;
} else {
  isAdult = false;
}

We can write similar instructions using the ternary operator as follows. Look how clean it is.

boolean isAdult = age > 18 ? true : false;

Happy Learning !!

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial