Junior — Middle
Can you give an example of using a conditional expression in code?
sobes.tech AI
Answer from AI
Conditional expressions allow executing different actions depending on a condition. In Java, this is usually implemented using if-else or the ternary operator.
Example with if-else:
int number = 10;
if (number > 0) {
System.out.println("Positive number");
} else {
System.out.println("Non-positive number");
}
Example with the ternary operator:
String result = (number % 2 == 0) ? "Even" : "Odd";
System.out.println(result);