Java If…Else

Conditional statements are an essential part of programming in any language. In Java, if…else statements are used to execute a block of code if a particular condition is true and another block of code if the condition is false. In this blog, we will explore if…else statements in Java and provide example code snippets to help you understand the concept better.

Syntax of If…Else Statements in Java:

The syntax of an if…else statement in Java is as follows:

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

The condition is a Boolean expression that can be either true or false. If the condition is true, the code inside the if block is executed. If the condition is false, the code inside the else block is executed.

Example:

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

Output: The number is greater than 5

In the above example, we declare and initialize a variable num with a value of 10. We use the if…else statement to check if the value of num is greater than 5. Since the value of num is 10, which is greater than 5, the code inside the if block is executed, which prints “The number is greater than 5” to the console.

Nested If…Else Statements:

You can also use nested if…else statements in Java to test multiple conditions. Nested if…else statements are if…else statements inside other if…else statements.

Example:

int num1 = 10;
int num2 = 20;
if (num1 > 5) {
   if (num2 > 15) {
      System.out.println("Both numbers are greater than their respective values");
   } else {
      System.out.println("Only the first number is greater than its value");
   }
} else {
   System.out.println("Both numbers are less than or equal to their respective values");
}

Output: Both numbers are greater than their respective values

In the above example, we declare and initialize two variables num1 and num2. We use nested if…else statements to check if the values of num1 and num2 are greater than their respective values. Since both values are greater, the code inside the nested if block is executed, which prints “Both numbers are greater than their respective values” to the console.

The Ternary Operator:

In Java, you can use the ternary operator to write if…else statements in a more concise way. The ternary operator is a shorthand way of writing an if…else statement.

Syntax:

(condition) ? statement1 : statement2;

Example:

int num1 = 10;
int num2 = 20;
String result = (num1 > num2) ? "num1 is greater" : "num2 is greater";
System.out.println(result);

Output: num2 is greater

In the above example, we declare and initialize two variables num1 and num2. We use the ternary operator to check if the value of num1 is greater than the value of num2. Since the value of num2 is greater, the statement “num2 is greater” is assigned to the variable result, which is printed to the console.

Conclusion:

In conclusion, if…else statements are an essential part of Java programming. They allow you to execute specific code based on the conditions you specify. With the examples provided, you can start using if…else statements in your Java programs