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
RBCN 2026 image
Tue, 10 Feb
Where the Robot Framework community shines brightest.
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.
This Week in Quality image
Debrief the week in Quality via a community radio show hosted by Simon Tomes and members of the community
Subscribe to our newsletter
We'll keep you up to date on all the testing trends.