Java If … Else

In Java, the if...else statement is used to execute a block of code based on a certain condition. It allows us to make decisions in our code, and execute different code depending on the outcome of the condition.

The basic syntax of an if...else statement in Java is:

if (condition) {
  // code to execute if condition is true
} else {
  // code to execute if condition is false
}

Here’s an example to illustrate how if...else works in Java. Let’s say we want to write a program that checks whether a given number is even or odd. We can use the if...else statement to achieve this as follows:

int num = 6;

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

In this code, we have initialized a variable num to be 6. The if statement checks if the remainder of num divided by 2 is equal to 0. If the condition is true, the program prints "6 is even". If the condition is false, the program prints "6 is odd".

The output of this code will be:

6 is even

int num = 10;

if (num > 0) {
    System.out.println("The number is positive.");
} else {
    System.out.println("The number is negative or zero.");
}

In this example, we have a variable num with a value of 10. We then use the if...else statement to check whether num is greater than 0. If it is, we print the message “The number is positive.” to the console. If it is not, we print the message “The number is negative or zero.” to the console.

The output of this code will be “The number is positive.”, since 10 is greater than 0.

Another example can be to check if a user is eligible to vote based on their age:

int age = 17;

if (age >= 18) {
    System.out.println("You are eligible to vote.");
} else {
    System.out.println("You are not eligible to vote yet.");
}

Nested if else

Nested if-else statements are used when we need to execute a set of instructions only if a certain condition is true, and then check for another condition inside that set of instructions, and so on.

The basic syntax for a nested if-else statement in Java is as follows:

if (condition1) {
   // code to be executed if condition1 is true
   if (condition2) {
      // code to be executed if both condition1 and condition2 are true
   } else {
      // code to be executed if condition1 is true but condition2 is false
   }
} else {
   // code to be executed if condition1 is false
}

Here, we have an if statement that checks for condition1. If condition1 is true, the code inside the if statement’s block is executed. Then, we have another if statement inside the first if statement’s block that checks for condition2. If condition2 is also true, the code inside the inner if statement’s block is executed. If condition2 is false, the code inside the inner else statement’s block is executed.

If condition1 is false, the code inside the outer else statement’s block is executed.

You can have as many nested if-else statements as you need to handle your logic. However, too many nested if-else statements can make your code difficult to read and maintain, so it’s important to keep your code organized and concise.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial