Determining a Value with a Ternary Statement

When dealing with conditional logic, an IF…ELSE is used to perform separate code blocks dependent on the outcome of a condition check. Those code blocks can include as many code statements as needed to perform the related actions for the condition.

IF…ELSE logic can be used to set the value for a single variable, as in the following code example.

 string votingMessage;
 int userAge = int.Parse(Console.ReadLine());
 
 if (userAge < 19)
 {
 votingMessage = "You are too young to vote.";
 }
 else
 {
 votingMessage = "You are old enough to vote.";
 }
 Console.WriteLine(votingMessage);

In this example, the message to be displayed is dependent on the age of the user.

There is a streamlined way to convert the eight lines of the IF…ELSE into a single line of code. You do this by using a “ternary” statement. A ternary statement consists of three parts: a condition check, a value to set if the check is TRUE, and a value to set if the value is FALSE.


Variable Value = Condition Check ? Value if TRUE: Value is FALSE


The following statement demonstrates how you would write the early IF…ELSE logic into a ternary operation.

votingMessage = userAge < 90 ? "You are too young to vote." : "You are old enough to vote.";

As you can see, a ternary statement is more concise when you are only using the condition check to set a value.

Ternary Return Types

A ternary operation can return any data type. The variable receiving the value of the operation must have the correct data type. If the data types don’t match, your application will not compile.


Boolean Example

Instead of using this code to set the getsOvertime flag:

 bool getsOvertime;
 string jobTitle = Console.ReadLine();
 
 if (jobTitle == "Manager")
 {
 getsOvertime = false;
 }
 else
 {
 getsOvertime = true;
 }

Use this:

 bool getsOvertime;
 string jobTitle = Console.ReadLine();
 
 getsOvertime = jobTitle == "Manager" ? false : true;

Numeric Example

Instead of using this code to provide a discount to ticket orders greater than 3:

 int numberOfTickets = int.Parse(Console.ReadLine());
 int totalPrice;
 
 if (numberOfTickets > 3)
 {
 totalPrice = numberOfTickets * 10;
 }
 else
 {
 totalPrice = numberOfTickets * 15;
 }

Use This:

 int numberOfTickets = int.Parse(Console.ReadLine());
 int totalPrice;
 
 totalPrice = numberOfTickets > 3 ? numberOfTickets * 10 : numberOfTickets * 15;

As this example demonstrates, you don’t need to use literals to set the value of the variable; you can use an expression. In this example, the variable will be set to the result of a calculation.


DateTime Examples

The expressions you use in the ternary statements can be just as involved as you would use in an IF…ELSE statement.


Instead of using this code to determine the first day of the week:

bool doesWeekStartWithSunday = bool.Parse(Console.ReadLine());
DateTime firstDayOfThisWeek;
DateTime today = DateTime.Today;
 
if (doesWeekStartWithSunday == true)
{
firstDayOfThisWeek = today.AddDays(-(int)today.DayOfWeek);
}
else
{
firstDayOfThisWeek = today.AddDays(-(int)today.DayOfWeek + (int) DayOfWeek.Monday);
}

Use this:

bool doesWeekStartWithSunday = bool.Parse(Console.ReadLine());
DateTime firstDayOfThisWeek;
DateTime today = DateTime.Today;
 
firstDayOfThisWeek = doesWeekStartWithSunday == true ? today.AddDays(-(int)today.DayOfWeek) : today.AddDays(-(int)today.DayOfWeek + (int)DayOfWeek.Monday);

22 views0 comments

Recent Posts

See All