Java Conditionals
Overview
In Java programming, conditional statements allow you to control the flow of your program by making decisions based on specific conditions. By evaluating expressions, you can execute different blocks of code depending on whether the condition is true or false. This ability is fundamental to creating dynamic and flexible applications.
Java Conditional Constructs
The key conditional constructs in Java include:
ifStatements: Used to execute a block of code if a condition is true.elseStatements: Provides an alternative code block that runs if the condition in theifstatement is false.else ifStatements: Used to evaluate multiple conditions, providing additional decision-making layers.- Ternary Operator (Shorthand
if-else): A concise way to write simpleif-elsestatements, offering a more compact syntax for straightforward decisions. switchStatements: Useful for evaluating a single expression against multiple possible values, providing a cleaner and more readable way to manage several potential outcomes.
if Statement
The if statement allows you to execute a block of code only if a specific condition evaluates to true.
Syntax
if (condition) {
// Code to execute if the condition is true
}
Example
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
}
else Statement
The else statement works with the if statement to provide an alternative block of code to execute if the if condition is false.
Syntax
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are not an adult.");
}
else if Statement
The else if statement allows you to check multiple conditions in sequence.
Syntax
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if all conditions are false
}
Example
int score = 75;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
Ternary Operator (Shorthand if-else)
The ternary operator is a shorthand way of writing simple if-else statements.
Syntax
result = (condition) ? value_if_true : value_if_false;
Example
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status);
switch Statement
The switch statement allows you to evaluate a single expression against multiple possible values.
Syntax
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
default:
// Code to execute if no case matches
}
Example
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
Nested if Statements
In Java, you can nest if statements within other if or else blocks for complex logical decisions.
Syntax
if (condition1) {
if (condition2) {
// Code to execute if both conditions are true
}
}
Example
int age = 25;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("You can enter the club.");
} else {
System.out.println("You need an ID to enter.");
}
} else {
System.out.println("You are not old enough to enter.");
}
switch with Strings
Starting from Java 7, the switch statement supports String values, useful for comparing text data.
Syntax
switch (stringExpression) {
case "String1":
// Code to execute if stringExpression equals "String1"
break;
case "String2":
// Code to execute if stringExpression equals "String2"
break;
// More cases...
default:
// Code to execute if no case matches
}
Example
String season = "Summer";
switch (season) {
case "Spring":
System.out.println("It's spring!");
break;
case "Summer":
System.out.println("It's summer!");
break;
case "Fall":
System.out.println("It's fall!");
break;
case "Winter":
System.out.println("It's winter!");
break;
default:
System.out.println("Invalid season");
}
switch Expressions (Java 14+)
In Java 14, switch expressions allow you to return values directly from the switch block, making it more concise.
Syntax
int result = switch (expression) {
case value1 -> result1;
case value2 -> result2;
default -> defaultResult;
};
Example
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Invalid day";
};
System.out.println(dayName);
Summary
ifStatements: Evaluate a condition and execute code if the condition is true.elseStatements: Provide an alternative block of code if theifcondition is false.else ifStatements: Allow checking of multiple conditions in sequence.- Ternary Operator: A shorthand way to write simple
if-elsestatements. switchStatements: Used for handling multiple cases based on a single expression.- Nested
ifStatements: Enable complex decision-making by nesting conditions. switchwith Strings: Allows switching based onStringvalues (Java 7+).
Semantic portal