if else in java

if else in java

If you have worked with any programming language, you must be knowing that conditional statements are a part of every programming language.

Conditional or decision-making statements include if-else in Java as well. The usage of conditional statements makes our code more readable and easier to understand.

if else in java

They also save us from repeating the same set of codes again and again for various conditions. Conditional statements are used to control the flow of program execution based on certain conditions.

If else in java statement is one such conditional statement in Java which comes in handy when we need to execute a particular block of code based on some condition.

In this article, we will learn what is the ‘if-else’ statement in Java, its different types, syntax, and example use case of if else in java statement in Java programs.

What is the ‘if-else‘ statement in Java?

if else in java statements are used when we have to execute a block of code based on some condition. It is a logical statement, which can be used to execute a block of code when the condition is true and a different block of code when the condition is false.

It can be used to avoid repeated code and make our code more readable and understandable. If-else statements are one of the most common control flow statements used to execute a block of code based on some condition.

If else in java statement can be used to check if a certain condition is true or false. When the condition is true, it executes one block of code and when the condition is false, it executes the other block of code.

The basic syntax of if-else statement includes a condition followed by two blocks of code: one block of code to execute when the condition is true and another block of code to execute when the condition is false. The ‘else’ clause is optional.

If-else in Java – syntax

The syntax of if-else statement in Java is as shown below – if (condition) { // code to execute when condition is true } else { // code to execute when condition is false } Here, ‘if’ and ‘else’ are mandatory.

The condition is enclosed in square brackets and it can be any valid Java expression. The code to be executed when the condition is true is placed inside curly braces and the code to be executed when the condition is false is placed inside the curly braces.

In the first example, we will check if a number is odd or even. In the second example, we will check if a number is below a certain value. In the third example, we will check if a number is greater than a certain value.

Types of If-else in Java

There are three types of if-else statements in Java –

1. If else without else – This type of if-else statement executes a block of code when the condition is true and skips the block of code when the condition is false.

2. If else with else – This type of if-else statement executes a block of code when the condition is true and a different block of code when the condition is false. ‘Else’ clause is optional in this type of if-else statement.

3. If else nested in if – This type of if-else statement executes a block of code when one condition is true and a different block of code when another condition is true.

Else only in if statement

If you are using an if statement, you most likely have an else clause. But what if you don’t want an else clause? You could just ditch it, but that’s not very nice.

Thankfully, the designers of Java thought of this issue, and gave us else only as a solution. else only is a reserved keyword in Java. It is used in if statements when you don’t want an else clause.

The difference between a regular if statement and an else only if statement is that the else clause disappears. If you want to use else only in Java, you should know that it is only allowed when the if statement is used in a method.

This means that you can’t use else only in a standalone if statement, but you can use it in a method that has an if statement.

Nested If else in Java

Nested if statements are statements that are placed inside another statement. In the if-else statements, nested if statements are used when we have to check for multiple conditions using if-else statements.

In the first example, we will check if a number is odd or even and if it is odd, we will use nested if-else statements to check if the number is divisible by 3 or not.

In the second example, we will check if a number is below 20 and if it is below 20, we will check if it is divisible by 3 or not, and if it is, we will display “Number is divisible by 3”.

Conclusion

If-else statements are one of the most common control flow statements used to execute a block of code based on some condition.

The if-else statement can be used to check if a certain condition is true or false. When the condition is true, it executes one block of code and when the condition is false, it executes the other block of code.

The basic syntax of if-else statement includes a condition followed by two blocks of code: one block of code to execute when the condition is true and another block of code to execute when the condition is false.

The ‘else’ clause is optional. There are three types of if-else statements in Java – if else without else, if else with else, and if else nested in if. Nested if statements are statements that are placed inside another statement.

Add a Comment

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