Ternary Operator

Ternary Operator image
Testers who don’t have an automation background might be unsure about the term ternary operator, which in many programming languages is a logical operator that evaluates a condition and returns the result of one of two expressions (depending on whether the boolean expression evaluates to true or false). It follows the format:

variable = (condition) ? expressionIfTrue : expressionIfFalse;


A ternary operator is often used to replace simple if else statements, replacing multiple lines of code with a single line. For example, this section of code returns “good day” if the hour is less than 18:00 and good evening between 18:00 and midnight.

if (time < 18) 
{
  Console.WriteLine("Good day.");
} 
else 
{
  Console.WriteLine("Good evening.");
}


A ternary operator can be used, with the exact same functionality:

string result = (time < 18) ? "Good day." : "Good evening.";
Console.WriteLine(result);
Explore MoT
Don’t automate everything, review everything image
Software Testing Live: Episode 06
MoT Software Testing Essentials Certificate image
Boost your career in software testing with the MoT Software Testing Essentials Certificate. Learn essential skills, from basic testing techniques to advanced risk analysis, crafted by industry experts.
Into The Motaverse image
Into the MoTaverse is a podcast by Ministry of Testing, hosted by Rosie Sherry, exploring the people, insights, and systems shaping quality in modern software teams.
Subscribe to our newsletter
We'll keep you up to date on all the testing trends.