Skip Top Navigation Bar

Evaluating and Writing Expressions that Contain the Ternary Conditional Operator

Evaluating Expressions

Grab a Piece of Paper!

Determine the value of each expression.

Verify your answer by running the code in the Java Playground.

Writing Expressions

Your Turn

  • Write program code to complete the provided tasks.
  • Run the code in the Java Program to see if your code generates the desired result.
  • Make necessary changes until you have satisfied the tasks.

Practice 7: Greeting Based on Time

  • Write a ternary conditional operator statement that prints either Good morning if hour is less than 12 or Good afternoon otherwise.
  • Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
  • Try the code in the Java Playground.

Practice 8: Proper Display Based on Count

  • Write a ternary conditional operator statement that prints either item if count is 1 or items otherwise.
  • Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
  • Try the code in the Java Playground.

Practice 9: Proper Display Based on Count

  • Write a ternary conditional operator statement that calculates the price differently if you are a member or not. If isMember is true the price is 90% of the original price. If isMember is false the price is the original price.
  • Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
  • Try the code in the Java Playground.

Practice 10: Determine Tax Rate

  • Write a nested ternary conditional operator statement that assigns taxRate based on income and the following rules:
    • if income is less than 10000, the taxRate is 10%
    • if income is greater than or equal to 10000 and less than 20000, the taxRate is 15%
    • if income is greater than or equal to 20000 and less than 50000, the taxRate is 20%
    • in all other cases, the taxRate is 25%
  • Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
  • Try the code in the Java Playground.