Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java If...Else

Java is a popular programming language that is widely used for developing various applications. One of the most important features of Java is the If...Else statement. The If...Else statement is used to execute a block of code based on a certain condition. In this article, we will discuss the If...Else statement in Java and how it can be used in programming.

What is If...Else statement in Java?

The If...Else statement is a conditional statement in Java that is used to execute a block of code based on a certain condition. The If...Else statement consists of two parts: the If statement and the Else statement. The If statement is used to check a condition and execute a block of code if the condition is true. The Else statement is used to execute a block of code if the condition is false.

The If...Else statement is used in situations where we want to execute a certain block of code if a condition is true and a different block of code if the condition is false. For example, we can use the If...Else statement to check if a number is even or odd and execute different code based on the result.

How to use If...Else statement in Java?

The If...Else statement in Java is used in the following syntax:

if (condition) {
    // code to be executed if the condition is true
} else {
    // code to be executed if the condition is false
}

The If...Else statement starts with the If keyword followed by the condition in parentheses. The code to be executed if the condition is true is enclosed in curly braces. The Else keyword is followed by the code to be executed if the condition is false, which is also enclosed in curly braces.

Let's take an example to understand how the If...Else statement works in Java:

int num = 10;

if (num % 2 == 0) {
    System.out.println("The number is even");
} else {
    System.out.println("The number is odd");
}

In this example, we have declared a variable num and assigned it a value of 10. We have used the If...Else statement to check if the number is even or odd. The condition in the If statement checks if the remainder of num divided by 2 is equal to 0. If the condition is true, the code inside the If block is executed, which prints "The number is even". If the condition is false, the code inside the Else block is executed, which prints "The number is odd".

Nested If...Else statement in Java

The If...Else statement can also be nested inside another If...Else statement. This is useful when we want to check multiple conditions and execute different code based on the results. Let's take an example to understand how nested If...Else statement works in Java:

int num1 = 10;
int num2 = 20;

if (num1 > num2) {
    System.out.println("num1 is greater than num2");
} else if (num1 < num2) {
    System.out.println("num1 is less than num2");
} else {
    System.out.println("num1 is equal to num2");
}

In this example, we have declared two variables num1 and num2 and assigned them values of 10 and 20 respectively. We have used the If...Else statement to check if num1 is greater than, less than or equal to num2. The first If statement checks if num1 is greater than num2. If the condition is true, the code inside the If block is executed, which prints "num1 is greater than num2". If the condition is false, the code inside the Else If block is executed, which checks if num1 is less than num2. If the condition is true, the code inside the Else If block is executed, which prints "num1 is less than num2". If both conditions are false, the code inside the Else block is executed, which prints "num1 is equal to num2".

Conclusion

The If...Else statement is an important feature of Java that is used to execute a block of code based on a certain condition. It is used in situations where we want to execute different code based on the result of a condition. The If...Else statement can also be nested inside another If...Else statement to check multiple conditions and execute different code based on the results.

References

Activity