Back
Close

Ternary Operator

[CG]Nick
21.4K views

Discover the Ternary Operator

In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator, inline if (iif), or ternary if.

Ternary Operator

In Java this expression evaluates to:

If foo is selected, assign selected foo to bar. If not, assign baz to bar.

Example:

Object bar = foo.isSelected() ? foo : baz;
Check the options where result equals "foo"

Note that Java, in a manner similar to C#, only evaluates the used expression and will not evaluate the unused expression.

Complete the getNearestEnemy method using a ternary operator
class Solution {
public static String getNearestEnemy(String enemy1, int dist1, String enemy2, int dist2) {
return "?";
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants