Java - If Else Condition

In Java, the if statement is a control flow statement that is used to execute a block of code based on a certain condition. The basic syntax of an if statement is as follows:

if (condition) {
  // block of code to be executed if the condition is true
}

The condition in the if statement can be any expression that returns a boolean value, which is either true or false. If the condition is true, the block of code inside the curly braces is executed; otherwise, it is skipped and the program moves on to the next statement.

int x = 10;
if (x > 5) {
  System.out.println("x is greater than 5");
}

In this example, the condition is x > 5, which evaluates to true because x is greater than 5. Therefore, the block of code inside the curly braces is executed, which prints the message "x is greater than 5" to the console.

In addition to the basic if statement, there are several other variations that you can use to perform more complex conditional logic:

if-else statement

The if-else statement is used when you want to execute one block of code if the condition is true, and a different block of code if it is false. The basic syntax is as follows:

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

Here is an example of an if-else statement:

int x = 10;
if (x > 5) {
  System.out.println("x is greater than 5");
} else {
  System.out.println("x is less than or equal to 5");
}

In this example, the condition is x > 5, which evaluates to true. Therefore, the first block of code is executed, which prints the message "x is greater than 5" to the console.

if-else if-else statement

The if-else if-else statement is used when you want to test multiple conditions and execute different blocks of code depending on which condition is true. The basic syntax is as follows:

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if condition2 is true
} else {
  // block of code to be executed if neither condition1 nor condition2 is true
}

Here is an example of an if-else if-else statement:

int x = 10;
if (x < 0) {
  System.out.println("x is negative");
} else if (x == 0) {
  System.out.println("x is zero");
} else {
  System.out.println("x is positive");
}

In this example, the condition1 is x < 0, which is false. The condition2 is x == 0, which is also false. Therefore, the last block of code is executed, which prints the message "x is positive" to the console.

Nested if statement is an if statement that is contained inside another if statement. It is used when we need to check multiple conditions, and each condition may have further sub-conditions that need to be evaluated.

The basic syntax for a nested if statement is as follows:

if (condition1) {
  // statements to execute when condition1 is true
  if (condition2) {
    // statements to execute when both condition1 and condition2 are true
  }
}

In the above example, if condition1 is true, the statements inside the first if block will be executed. If condition2 is also true, the statements inside the nested if block will be executed.

Here's an example of a nested if statement:

int age = 25;
String gender = "female";
if (age >= 18) {
  System.out.println("You are an adult");
  if (gender.equals("male")) {
    System.out.println("You are a male adult");
  } else {
    System.out.println("You are a female adult");
  }
}

In the above example, the outer if statement checks if the age is greater than or equal to 18. If it is true, the statement "You are an adult" will be printed. The inner if statement checks the gender, and if it is "male", it will print "You are a male adult". If the gender is not "male", it will print "You are a female adult".