In this lesson, we’ll cover a handy shortcut for writing an if() and else() statement called the ternary operator. This shortcut does the same kind of flow control as an if() statement, branching off and executing one of two possible conditions. If you need to evaluate more than two possible conditions, stick with using the if(), else if(), else() set of conditional statements. However, the ternary operator is a great shortcut for simple if/else conditional logic.
Step 1: Create a New Project
As always, the code associated with this lesson is available in the provided folder under CS-ASP_012. In the Before folder, open up the Project called CS-ASP_012 and skip ahead to Step 2. Otherwise, set up the Project manually as follows:
Create a new ASP.NET project called “CS-ASP_012” and set it up with the following Server Controls:
The programmatic IDs for these Controls are:
Step 2: Create a Statement using the Ternary (?) Operator
Now let’s write a statement in the okButton_Click event to evaluate whether or not the two TextBoxes are equal, then tell the user using the resultLabel. We previously learned to do this using an if() and else() statement. However, using the ternary operator, we can write this evaluation all in one line of code. and then run the application:
Step 3: Breaking Down the Ternary Operator
The ternary statement may look odd at first, but when you learn how to interpret it, you will find it’s no different than the conditional statements we’ve been writing thus far. Here is how you can read this
firstTextBox.Text is equivalent to secondTextBox.Text
then, resultLabel.Text equals
The question mark (1) is the same as saying "if", then what is inside the parentheses (2) is the conditional check. Is firstTextBox.Text equivalent to secondTextBox.Text? (3) resultLabel’s Text Property will be set depending on the result of the evaluation. If true, then set the Text equal to "Yes" (4). The colon (5) denotes the else statement, and the following string (6) is the value to be assigned if the evaluation is false.
Now, let’s create a ternary statement for the CheckBox control.
Tip: Your eyes are not playing tricks on you! Recall that whitespace is ignored by the compiler, leaving it up to you how to style code to make it easier to read. In this case, we chose to split the ternary statement on multiple lines simply because it was running off the screen. But make no mistake, this is still just one statement with a single semi-colon on the end.
Remember, the evaluation for oldCheckBox.Checked implies a Boolean value, so we do not have to write out the full expression of oldCheckBox.Checked == “true”. Running the application will produce the same branching nature as we have come to expect:
Step 4: A “game-like” Example of the Ternary in Action
Combining several things we’ve learned so far, let’s write out a ternary statement that demonstrates how we might go about using it for a game-like situation. In this scenario, different score values may be awarded depending on if an equivalence has been evaluated as true or false.
The beauty of the ternary operator is its simplicity; but it comes with a cost. You cannot have a complex, multi-conditional evaluation and use a ternary operator. However, for simple evaluations, it’s much shorter and simpler, as long as you remember the proper syntax for it. Good job in this lesson, keep it up!