Learn Java If and If Else If statement with examples in this tutorial. If you want to test the condition and execute the code when the condition is true, you use Java If and Else If conditional statement. When we want to test the input values based on the given conditions, we print the result when the condition matches with the input value.

Check below conditional statement and learn each one with the given examples. There are four types of conditional statements given below.

  • If Statement
  • If Else Statement
  • If Else If Statement
  • Nested If Statement

Below are the description of each statements if the if and If else if conditional statement.

Java If Statement

When you want to test only the single condition and print he result only when the condition is true. If the condition is false, the given statement in the if condition will not gets executed.

Below is the syntax of the if statement.

Syntax

The syntax contains the one single condition and the statement inside the if statement.

Example of Java If Statement

Here, in the example given below contains the class ‘JavaIfExample’ with the main class contains the if condition. The defined variable ‘i’ is a numeric variable declared with ‘num’.

Output

The number is greater than 5

The if condition checks if the value of variable ‘i’ is greater than 5. If the value of ‘i’ is greate than 5, it will print the given statement. Otherwise, it will completely ignore the statement.

In the above example, the condition is true and prints the statement in output.

Java If Else Statement

The if else statement checks a condition , if it is true. If the statement is not true, it will execute the statement under the else statement.

Syntax

There are two statements in this conditional statement. The first statement is under the if and the second is under the else. The first condition executes when the if condition is true. Otherwise it will execute the second statement.

Example of Java If Else Statement

The example contains the one condition statement and one else statement.It checks the condition if it is true or false.

Output

The number is less than 5

The example contains the class in which the main statement contains the if else statement. It first checks the if condition which is false and the output prints the else statement.

Java If Else If Statement

The if else if condition contains the number of conditions to check multiple times. It matches each condition if it is true. If any condition is true, it executes the statement inside that if statement.

If no condition is true, it will execute the else statement code. You can add multiple number of else if statement with it.

Syntax

The else statement will execute only if all the condition is false.

Example of Java If Else If Statement

Output

I am greater than 30 but less than 40

Java Nested If Statement

The nested if statement contains the if statement inside the another if statement. It check the condition of the first if condition and execute the code if it is true and then check the another if statement. If the another if condition is true, it execute the statement. You can use many if condition inside the another if conditional statement.

The below syntax contains the two if statement with one condition inside the another condition.

Syntax

Example of Java Nested If Statement

Output

The number is greater than 5
The number is less than 10

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.