Java if-else Statement With Examples

In Java programming language there are two selection statements if-else and switch to control the execution flow. This post talks about if and if-else statements in detail with examples.

Java if-else statement

Java if-else statement is used to evaluate a condition and take the execution path based on whether the evaluated condition is true or false. Java if statement is very flexible and provides many combinations.

  • if statement
  • if-else statement
  • if-else-if statement
  • nested if statement

Java if statement

You can have just the if statement (else is optional).
Syntax of the Java if statement-

Here condition is a boolean expression. If condition is evaluated to true if block is executed, if condition evaluates to false then the if block is not executed. If there is only a single statement with in the if condition then the curly braces are optional.

Java if statement examples

Output

Here the condition (age > 18) is evaluated to true so the if block is executed.

In the same program if age is passed as less than 18.

Output

Here the condition (age > 18) is evaluated to false so the if block is not executed. Note that the curly braces are not used with the if statement as there is only a single statement.

Java if-else statement

In Java if-else statement condition is evaluated and the execution flow is routed based on whether the condition is true or false.
Syntax of the Java if-else statement-

In the if-else statement if block is executed when condition is evaluated to true, if condition evaluates to false then the else block is executed.
Java if-else

Java if-else statement examples

Output

Since condition evaluates to true so the if block is executed.

Output

Java if-else-if ladder

You can also have a series of if-else if statements where each if and else if statement has a condition and a particular block is executed if the condition associated with that block evaluates to true.
Java if-else-if syntax-

In if-else-if statement condition is evaluated from the top. Whichever condition evaluates to true statements associated with that block are executed and rest of the statements are bypassed. Last else statement acts as a default which is executed if none of the condition evaluates to true. But the lase else statement is optional if it is not present and all the conditions are false then no statement is executed and the control comes out of if-else-if.

Java if-else-if example

Output

In the code first if condition itself is evaluated to true so none of the other conditions are evaluated.

Java nested if-else

You can have a if-else statement inside a if-else statement in Java. It is known as a nested if-else statement.

Java nested if-else example

Output

That’s all for the topic Java if-else Statement With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.