Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pearl
communitycreator

What is the ternary operator in Pearl?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Pearl, we use ternary operators so we don't have to use the if-else statement. The operator makes the codes look nice and clean, which makes them easy to understand.

One of the most apparent important use of a ternary operator is that it replaces the use of the if-else state in a more clean, readable, and short form.

Syntax

cond ? val if true : val if false
Syntax

Parameters

  • cond: This is the code/expression whose output defines the value that is used.
  • The value after the ? is returned if the condition returns true.
  • The value after the : is returned if the condition returns false.

Example

print(1==0 ? 'right' : 'wrong',"\n");
Ternary operator in Pearl

Explanation

  • Line 1: We use the ternary operator to check if 1 is equal to 0. This is definitely false, and that's why the wrong message displays.

RELATED TAGS

pearl
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring