When to use if, if…else Statements in Java

When to use if, if...else Statements in Java

Like all other programming languages, Java is equipped with certain statements that allow us to check a condition and run certain parts of code depending on whether the condition is true or false. These statements are known as conditional statements.

Java if (if-then) Statement

The syntax of if-then statement in Java is:

Here expression is a boolean expression (returns either true or false).

If the expression is evaluated to true, statement(s) inside the body of if (statements inside parenthesis) are executed.

If the expression is evaluated to false, statement(s) inside the body of if are skipped from execution.

How if statement works?

Example 1: Java if Statement

When you run the program, the output will be:

When number is 10, the test expression number > 0 is evaluated to true. Hence, codes inside the body of if statements are executed.

Now, change the value of number to a negative integer. Let’s say -5. The output in this case will be:

When number is -5, the test expression number > 0 is evaluated to false. Hence, Java compiler skips the execution of body of if statement.

Java if…else (if-then-else) Statement

The if statement executes a certain section of code if the test expression is evaluated to true. The if statement can have optional else statement. Codes inside the body of else statement are executed if the test expression is false.

The syntax of if-then-else statement is:

How if…else statement works?

Example 2: Java if else Statement

When you run the program, the output will be:

When number is 10, the test expression number > 0 is evaluated to true. In this case, codes inside the body of if are executed, and codes inside the body of else statements are skipped from execution.

Now, change the value of number to a negative number. Let’s say -5. The output in this case will be:

When number is -5, the test expression number > 0 is evaluated to false. In this case, codes inside the body of else are executed, and codes inside the body of if statements are skipped from execution.

Java if..else..if Statement

It’s often asserted that Java has an additional else…if statement, which isn’t technically true. You can nest if statements inside of an else block, and the code to do so puts the else and if statements right next to each other, but that’s just happenstance. The else…if statement isn’t a special Java construct in itself.

In Java, it’s possible to execute one block of code among many. For that, you can use if..else…if ladder.

The if statements are executed from the top towards the bottom. As soon as the test expression is true, codes inside the body of that if statement is executed. Then, the control of program jumps outside if-else-if ladder.

If all test expressions are false, codes inside the body of else is executed.

Example 3: Java if..else..if Statement

When you run the program, the output will be:

When number is 0, both test expression number > 0 and number < 0 is evaluated to false. Hence, the statement inside the body of else is executed.

The above program checks whether number is positive, negative or 0.

Java Nested if..else Statement

It’s possible to have if..else statements inside a if..else statement in Java. It’s called nested if...else statement.

Here’s a program to find largest of 3 numbers:

Example 4: Nested if…else Statement


When you run the program, the output will be:

 

You may also like...