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.
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);