Conditional statements in Java allow you to execute different blocks of code based on certain conditions. The primary conditional statements are if
, else if
, and else
. Here, we’ll explore these statements with detailed examples to demonstrate their usage in Java.
The if
Statement
Executes a block of code if its condition evaluates to true
.
Example:
1 2 3 4 5 |
int number = 10; if (number > 0) { System.out.println("The number is positive."); } // Output: The number is positive. |
The else
Statement
Executes a block of code if the condition in the if
statement evaluates to false
.
Example:
1 2 3 4 5 6 7 |
int number = -10; if (number > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); } // Output: The number is not positive. |
The else if
Statement
Allows checking of multiple conditions. If if
is false
, it checks the else if
condition. If both are false
, else
is executed.
Example:
1 2 3 4 5 6 7 8 9 |
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."); } // Output: The number is zero. |
Nested if
Statements
You can nest if
statements within each other for multiple checks.
Example:
1 2 3 4 5 6 7 8 9 |
int number = 100; if (number > 0) { if (number < 100) { System.out.println("The number is positive and less than 100."); } else { System.out.println("The number is positive and 100 or more."); } } // Output: The number is positive and 100 or more. |
Using Logical Operators with if
Statements
Combine conditions using logical operators.
Example with Logical AND (&&
):
1 2 3 4 5 6 7 8 |
int age = 25; boolean hasLicense = true; if (age >= 18 && hasLicense) { System.out.println("You can drive."); } else { System.out.println("You cannot drive."); } // Output: You can drive. |
Example with Logical OR (||
):
1 2 3 4 5 |
int temperature = 30; if (temperature < 0 || temperature > 35) { System.out.println("Extreme temperature!"); } // No output as the condition is false. |
Example with Logical NOT (!
):
1 2 3 4 5 |
boolean raining = false; if (!raining) { System.out.println("Let's go for a walk!"); } // Output: Let's go for a walk! |
The Ternary Operator
A shorthand for if-else
that returns a value.
Example:
1 2 3 4 |
int number = 10; String result = (number > 0) ? "Positive" : "Not Positive"; System.out.println(result); // Output: Positive |
The switch
Statement
The switch
statement executes one block of code out of many based on the condition. It’s a cleaner alternative to multiple if-else
statements for fixed data values.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
int day = 4; String dayString; switch (day) { case 1: dayString = "Monday"; break; case 2: dayString = "Tuesday"; break; case 3: dayString = "Wednesday"; break; case 4: dayString = "Thursday"; break; case 5: dayString = "Friday"; break; case 6: dayString = "Saturday"; break; case 7: dayString = "Sunday"; break; default: dayString = "Invalid day"; break; } System.out.println("Day of the week: " + dayString); // Output: Day of the week: Thursday |
The switch
statement checks the value of day
and matches it with a case. If a match is found, it executes the code associated with that case until a break
statement is reached, which exits the switch. If no case matches, the default
code block is executed.
These examples with detailed explanations and outputs should provide a clearer understanding of how to use conditional statements and the switch
statement in Java to control the flow of your programs.