Junior — Middle
Could you explain what a conditional operator is and how it is used in programming?
sobes.tech AI
Answer from AI
Conditional operator — is a construct in programming that allows executing different actions depending on whether a certain condition is true or false.
In Java, the most common conditional operator is if. Its syntax:
if (condition) {
// code to execute if the condition is true
} else {
// code to execute if the condition is false
}
Example:
int age = 18;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
There is also the switch operator, which is convenient for choosing from several options based on the value of a variable.