Groovy Ternary Operator

Groovy Ternary Operator

Ternary Operator (?:) simplifies conditional logic.
Supports nested conditions like (condition) ? A : (anotherCondition) ? B : C.
Helps write shorter, cleaner Groovy code.

Reference

 

Example

An employee receives a bonus based on their years of service:

  • If years of service ≥ 5, the bonus is 20% of salary.

  • Otherwise, the bonus is 10% of salary.

We will use the ternary operator (?:) to determine the bonus.

def calculateBonus(double salary, int yearsOfService) { return (yearsOfService >= 5) ? salary * 0.20 : salary * 0.10 } // Example Calls return calculateBonus(5000, 6) // Expected: 1000.0 return calculateBonus(5000, 3) // Expected: 500.0
  1. The ternary operator replaces an if-else statement

  2. This avoids multiple if-else statements, making the code clean and concise.

 

Instructions

Task 1: Check Even or Odd

Write a function that returns "Even" if the number is even, otherwise return "Odd".

def checkEvenOdd(int num) { return /* Student's solution */ } // Example Calls return checkEvenOdd(4) // Expected: "Even" return checkEvenOdd(7) // Expected: "Odd"
def checkEvenOdd(int num) { return (num % 2 == 0) ? "Even" : "Odd" }

 

Task 2: Determine Age Group

Write a function that classifies a person as "Child" (age < 13), "Teen" (13-19), or "Adult" (20+).

def getAgeGroup(int age) { return /* Student's solution */ } // Example Calls return getAgeGroup(10) // Expected: "Child" return getAgeGroup(15) // Expected: "Teen" return getAgeGroup(25) // Expected: "Adult"
def getAgeGroup(int age) { return (age < 13) ? "Child" : (age <= 19) ? "Teen" : "Adult" }

 

Task 3: Find Maximum of Two Numbers

Write a function that returns the larger of two numbers.

def findMax(int a, int b) { return /* Student's solution */ } // Example Calls return findMax(10, 20) // Expected: 20 return findMax(50, 30) // Expected: 50
def findMax(int a, int b) { return (a > b) ? a : b }

 

Task 4: Check Voting Eligibility

Write a function that returns "Eligible" if age is 18 or above, otherwise "Not Eligible".

def canVote(int age) { return /* Student's solution */ } // Example Calls return canVote(17) // Expected: "Not Eligible" return canVote(18) // Expected: "Eligible" return canVote(25) // Expected: "Eligible"
def canVote(int age) { return (age >= 18) ? "Eligible" : "Not Eligible" }

 

Task 5: Check Positive, Negative, or Zero

Write a function that returns "Positive", "Negative", or "Zero" based on the input number.

def checkNumber(int num) { return /* Student's solution */ } // Example Calls return checkNumber(10) // Expected: "Positive" return checkNumber(-5) // Expected: "Negative" return checkNumber(0) // Expected: "Zero"
def checkNumber(int num) { return (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero" }

 

Task 6: Check if a String is Empty or Not

Write a function that returns "Not Empty" if the string has content, otherwise return "Empty".

def checkString(String str) { return /* Student's solution */ } // Example Calls return checkString("Hello") // Expected: "Not Empty" return checkString("") // Expected: "Empty"
def checkString(String str) { return (str?.trim()) ? "Not Empty" : "Empty" }

 

Looking for labels? They can now be found in the details panel on the floating action bar.

Related content

Found an issue in documentation? Write to us.