If Else Program in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

In the Java programming language, an if-else statement is used to check a condition and execute a block of statements if the condition is true. If the condition is not true, the else block of statements will be executed. This allows for the execution of different code depending on whether a certain condition is met or not. The if statement is a fundamental control flow construct in Java and is used frequently in programming to make decisions based on certain conditions.

Java If-else Statement (Introduction)

In java programming language, when we need to execute a block of statements depending on the condition, we need to use conditional statements. For example, if we want to display a “positive number” when the number is greater than equal to zero, and we want to display a “negative number” when the number is less than zero. In this case, although we have two different print statements, only one will execute depending on the number. Here we will use an if-else statement, in the if statement, we check a condition, and if the condition is true, it will execute the set of statements written inside the if block, and if the condition is false, then it will execute the set of statements written inside the else block.

Syntax:

INTRODUCTION OF IF ELSE STATEMENT

Example:

Output of this if else program in java will be:

Java If Statement

We use the if statement in java when we want to execute a block of code when a condition is true.

Syntax:

The statement will execute only if the condition is true, if the condition is false the code written inside the if block will be ignored, and the else block if present will be executed.

Example:

Output of this if else program in java will be:

Java If with String

We can use string in if condition.

Example:

Output of this if else program in java will be:

Note: In java, we use equals() to compare values instead of == because == only checks if both objects refer to the same memory location, whereas the equals() method compares the values in the object.

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

We can write an if-else program in java, in which if the condition is true, the statement inside the if block will execute, and if the condition is false, the else block will execute.

Syntax:

IF THEN ELSE

Example:

The above if-else program in java uses an if-else statement to print Even or odd after checking it. Output of this if else program in java will be:

How if else Statement Works?

In java, if else statements are conditional statements that work depending on some condition. The condition is evaluated to a boolean value, i.e, either true or false (1 or 0, respectively). If the condition is true(1), then if block will be executed, if the condition is false then else block will be executed.

Java Nested if Statement

In java programming, if there is an if statement written inside another if block, then it is called a nested if statement.

Note: The inner if statement will be checked only if the outer if statement is true.

Syntax:

NESTED IF

Example:

Output of this if else program in java will be:

If else If Ladder Statement

In java programming language, we use the if-else-if statement when we want to check multiple conditions and want to execute a different statement depending on these conditions. In this, we have one if statement, one else statement and we can have multiple else if statements. This is known as the if-else-if ladder.

Syntax:

Note: In an if-else-if statement as soon as the condition is true the set of code inside that block will get executed and every other else if and else block will be ignored, if no condition is true then the else block will get executed.

LADDER STATEMENT

Example:

Here in this example, the third condition is true, therefore it will print “Good” and everything else will be ignored.

Output of this if else program in java will be:

Using Ternary Operator

Java programming language also supports a ternary operator. The ternary operator can work as an alternative for an if-else statement. Using the ternary operator, we can do the same work that we do with the help of an if-else statement, but it helps to shorten the codebase. The ternary operator is represented using “?:”

Syntax:

Here if Expression1 is true then Statement1 will be executed and the resultant value will be stored in the result variable, and if Expression1 is false then Statement2 is executed and the resultant value will be stored in the result variable.

Example:

Output of this if else program in java will be:

Here in this example, it checks if num1 > num2 so as num1 is less than num2 and the condition evaluates to false therefore num2 value will be stored in variable largest. Hence Largest is = 20 is printed on the screen.

Conclusion

  • In java, when we want to execute some statement(s) depending on some condition, then we use the if else statement.
  • If the condition is true if block will execute, otherwise else block will execute.
  • There are different variations of if else statements in java. if, if-else, if-else-if ladder, Nested if
  • In java, we have a ternary operator, which works similar to an if else statement.