Sobes.tech
Intern

What is a ternary operator?

sobes.tech AI

Answer from AI

This is a conditional operator that takes three operands. It serves as a shorthand form of if-else.

// Syntax:
// condition ? expression_if_true : expression_if_false;

int x = 10;
int y = (x > 5) ? 20 : 30; // If x > 5, then y = 20, otherwise y = 30
// In this case, y will be 20

Example with if-else:

int x = 10;
int y;
if (x > 5) {
    y = 20;
} else {
    y = 30;
}
// In this case, y will be 20