Conditional statements in Java allow you to control the flow of execution based on certain conditions. These statements make decisions within your program by evaluating boolean expressions. There are mainly three types of conditional statements in Java: `if`, `if-else`, and `switch-case`. Here’s a detailed look at each:
### 1. `if` Statement:
The `if` statement executes a block of code if a specified condition is true. It can be followed optionally by an `else` block, which executes when the condition is false.
**Syntax:**
```java
if (condition) {
// Code to be executed if condition is true
}
```
**Example:**
```java
int number = 10;
if (number > 0) {
System.out.println("Number is positive");
}
```
### 2. `if-else` Statement:
The `if-else` statement executes one block of code if the condition is true and another block if the condition is false.
**Syntax:**
```java
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
```
**Example:**
```java
int number = -5;
if (number > 0) {
System.out.println("Number is positive");
} else {
System.out.println("Number is non-positive");
}
```
### 3. `else-if` Ladder:
You can use multiple `else-if` statements to test multiple conditions.
**Syntax:**
```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
}
```
**Example:**
```java
int score = 75;
if (score >= 90) {
System.out.println("Grade is A");
} else if (score >= 80) {
System.out.println("Grade is B");
} else if (score >= 70) {
System.out.println("Grade is C");
} else {
System.out.println("Grade is F");
}
```
### 4. `switch-case` Statement:
The `switch-case` statement evaluates a variable/expression against multiple possible values (cases). It executes the block of code corresponding to the matching case.
**Syntax:**
```java
switch (expression) {
case value1:
// Code to be executed if expression matches value1
break;
case value2:
// Code to be executed if expression matches value2
break;
// More cases as needed
default:
// Code to be executed if expression doesn't match any case
}
```
**Example:**
```java
int dayOfWeek = 3;
switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
}
```
### Notes:
- You can nest `if` statements inside other `if` statements (nested if-else).
- `if-else` and `switch-case` are used based on the complexity and number of conditions you need to evaluate.
- The `switch-case` statement works with byte, short, int, char, Enum types, and String (since Java SE 7).
Conditional statements are essential for implementing logic in Java programs, allowing them to make decisions based on dynamic inputs and conditions. They are fundamental for controlling program flow and implementing logic based on varying circumstances.