# Lesson 11 - Activity 4 - What are ternary operators?

**URL:** <https://club.ministryoftesting.com/t/lesson-11-activity-4-what-are-ternary-operators/31723>\
**Category:** Discussions\
**Tags:** java-intro\
**Created:** [28 November 2019 18:04 UTC](https://club.ministryoftesting.com/t/lesson-11-activity-4-what-are-ternary-operators/31723 "2019-11-28T18:04:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike\_talks](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/mike_talks/32/11115_2.png) [@mike\_talks](https://club.ministryoftesting.com/u/mike_talks)\
**Post date:** [28 November 2019 18:04 UTC](https://club.ministryoftesting.com/t/lesson-11-activity-4-what-are-ternary-operators/31723/1 "2019-11-28T18:04:59Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![huib.schoots](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/huib.schoots/32/7014_2.png) [@huib.schoots](https://club.ministryoftesting.com/u/huib.schoots)\
**Post date:** [21 March 2020 18:10 UTC](https://club.ministryoftesting.com/t/lesson-11-activity-4-what-are-ternary-operators/31723/2 "2020-03-21T18:10:05Z")

</div>

```
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;
}
```

---

<div class="post-metadata">

**Author:** ![mike\_talks](https://sea2.discourse-cdn.com/flex020/user_avatar/club.ministryoftesting.com/mike_talks/32/11115_2.png) [@mike\_talks](https://club.ministryoftesting.com/u/mike_talks)\
**Post date:** [30 March 2020 03:17 UTC](https://club.ministryoftesting.com/t/lesson-11-activity-4-what-are-ternary-operators/31723/3 "2020-03-30T03:17:57Z")

</div>

😆

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!
