Time: 15 minutes
Purpose: This activity helps you get familiar with terniary operators
Introduction: You’ve created if statements and switches, look at terniary operators
Task: A terniary operator allows you to create a whole if/else command on a single line.
They are not extensively used, but you might come across them. Read up on them.
Have a go at turning a previous piece of logic into a terniary operator.
public static Boolean eligibleByCountryTernaryOperator (String country) {
Boolean result;
result = (country == "NZ") ?
Boolean.TRUE : (country == "AU") ?
Boolean.TRUE : (country == "CA") ?
Boolean.TRUE : (country == "UK") ?
Boolean.TRUE : (country == "US");
return result;
}

Oh that is terrifying, but it looks like it would work!
Remember a ternary operator is there to reduce statements to be simpler. Nested ternaries like that are a bit unreadable, but I know you did that to see what you could push.
Like Dr Malcolm in Jurassic Park, remember that just because you CAN do a thing does not mean you should actually do that thing!
1 Like