Java Conditional Statements Learn about the different types of conditional statements in Java, including if, if-else, if-else-if, and nested if-else statements.

Conditional Statements in Java: A Comprehensive Guide

Java provides several ways to control the flow of a program based on conditions. In this article, we will explore the different types of conditional statements in Java, including if statements, if-else statements, if-else-if statements, and nested if-else statements.

Java if (if-then) Statement

The if statement is used to execute a block of code if a certain condition is true. The syntax of an if statement is:

java
if (condition) {
// code to be executed
}

Here, the condition is a boolean expression that evaluates to either true or false. If the condition is true, the code inside the if block is executed.

Example: Java if Statement

java
int number = -10;
if (number < 0) {
System.out.println("The number is negative");
}

In this example, the condition number < 0 is true, so the code inside the if block is executed.

Java if…else (if-then-else) Statement

The if-else statement is used to execute one block of code if a condition is true and another block of code if the condition is false. The syntax of an if-else statement is:

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

Here, the condition is a boolean expression that evaluates to 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: Java if else Statement

java
int number = 10;
if (number > 0) {
System.out.println("The number is positive");
} else {
System.out.println("The number is not positive");
}

In this example, the condition number > 0 is true, so the code inside the if block is executed.

Java if..else..if Statement

The if-else-if statement is used to execute one block of code among multiple blocks. The syntax of an if-else-if statement is:

java
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if all conditions are false
}

Here, the conditions are boolean expressions that evaluate to either true or false. If the first condition is true, the code inside the first if block is executed. If the first condition is false, the second condition is evaluated. If the second condition is true, the code inside the second if block is executed. If all conditions are false, the code inside the else block is executed.

Example: Java if..else..if Statement

java
int number = 0;
if (number > 0) {
System.out.println("The number is positive");
} else if (number < 0) {
System.out.println("The number is negative");
} else {
System.out.println("The number is zero");
}

In this example, the conditions number > 0 and number < 0 are false, so the code inside the else block is executed.

Java Nested if..else Statement

The nested if-else statement is used to execute one block of code among multiple blocks inside another if-else statement. The syntax of a nested if-else statement is:

java
if (condition1) {
if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if condition2 is false
}
} else {
// code to be executed if condition1 is false
}

Here, the conditions are boolean expressions that evaluate to either true or false. If the first condition is true, the second condition is evaluated. If the second condition is true, the code inside the inner if block is executed. If the second condition is false, the code inside the inner else block is executed. If the first condition is false, the code inside the outer else block is executed.

Example: Nested if..else Statement

java
int num1 = 10;
int num2 = 20;
int num3 = 30;
if (num1 > num2) {
if (num1 > num3) {
System.out.println("num1 is the largest");
} else {
System.out.println("num3 is the largest");
}
} else {
if (num2 > num3) {
System.out.println("num2 is the largest");
} else {
System.out.println("num3 is the largest");
}
}

In this example, the conditions are evaluated, and the largest number is printed.

Leave a Reply

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