In this lesson, we're going to delve deeper into comparison operators. We've already looked at the equivalence operator (==), which just tests whether or not two sides are equivalent to one another (whether that's two variables, two literal strings, two numbers, and so on). These other operators can also be used within a conditional’s parentheses for evaluation, but the rest of the evaluation process remains the same: it checks to see if the entire evaluated statement is true, or false.
To begin this Project, you may either create the Project from scratch or use the Before code in the provided code folder. If you choose to use the provided code, move ahead to Step 2, otherwise follow the steps below:
To demonstrate this, set up an ASP.NET project called “CS-ASP_013” and include the following Server Controls:
The programmatic IDs for these Controls are:
This time we’ll write some code in the Page_Load event, as well as the okButton_Click event:
When you run the application, notice how the comparisonTypeLabel is set as soon as the page loads:
Tip: Page_Load is an event, just like the okButton_Click is. Think of it this way: When the okButton is clicked an event occurs, and the code inside the code block runs. In the same way, as soon as the page loads, an event occurs and the code inside its code block executes. We'll discuss Page_Load in detail in a later lesson.
Now turning to comparison operators, here is a list that includes the comparison being done as well as the types they operate on:
Tip: Comparison operators imply that you are comparing two different things. As such, these are also referred to as binary operators, because they always operate on two different operands; one on each side of the operator.
Let’s test out these different comparison operators by modifying the code and running the application:
Now, try using the “greater-than” operator in the same way, and you will see that an error prevents us from moving forward. The problem is that we are attempting to use a mathematical comparison operator on string values:
The solution to this problem goes back to the conversion lesson that showed you how to convert a string to an int with the int.Parse() method:
When you run the application, you will now see that the comparison operator works as expected, provided that you only enter integer values:
Tip: It should be clear, at this point, how the rest of the mathematical comparison operators work. Just be sure to use >= and <= rather than =< or =>, when performing a greater-than-or-equal-to, or less-than-or-equal-to check. It’s easy to reverse them. And in the case of =>, it’s a totally different operator which is used for something called a lambda expression.
So far we’ve been looking at the comparison operators that are said to be binary (meaning “two”) because they evaluate two different operands on opposite sides of the operator. However, there are also unary operators (meaning “one”) which are used to place an evaluation on a single operand. The most common unary operator, used within conditional expressions (although, not exclusively), is the single-exclamation operator, which can be read as saying “not.” You can place this operator on anything that evaluates to a bool, as follows:
This now evaluates as true only when it is not the case that checkedCheckBox.Checked is true. For this reason, we had to flip the resulting strings to accommodate this change and work as it did before:
You can also group expressions together to form more complex evaluations, using the “and” and “or” logical operators, which are also binary operators:
Here the && operator (representing “and”) is used to combine expressions to be evaluated within a single if() statement. This evaluation will only evaluate as true if:
checkedCheckBox.Checked is true
firstTextBox.Text == “Bob” is true
secondTextBox.Text == “Tabor” is true
If any of those expressions individually evaluate as false, then the entire conditional statement evaluates as false. And, once again, don’t let the formatting fool you: these expressions were placed on separate lines only for the sake of visual clarity. Run the application and experiment with different combinations of bool evaluations:
We can be more flexible with the conditional evaluation by using the logical “or” operator, which is denoted by "||". Instead of requiring all of the code withing the conditional to evaluate as true, your code can execute if any of your conditions are met:
In this case, the conditional statement will evaluate as true if any of the three individual expressions evaluate as true:
Tip: It should be noted that more than one of these three expressions could evaluate true and the entire statement will evaluate as true. This is useful in cases where only certain conditions out of a set need to be met in order for your application to work. Think of optional input in a website's signup form. Your submission will go through whether you input only the required information or the required and optional information.
You can also combine the || and && operators together for more complex evaluations:
However, the && operator has precedence (just as multiplication/division has precedence in math) thereby grouping the comparison between firstTextBox.Text and secondTextBox.Text:
This is not the result we’re looking for, so let’s create our own precedence by wrapping the evaluation in parentheses, grouping the first comparison between checkedCheckBox.Checked and firstTextBox.Text. Now the entire statement evaluates as true only when, either, checkedCheckBox.Checked or firstTextBox.Text are true, and secondTextBox.Text is also true:
You can see from these examples the usefulness of comparison operators in your code. It allows you to further specify conditions that need to be met for your code to execute in order for the flow of your code to change. This will be very important and useful moving forward in this course, so do your best to review and make use of these concepts. You're doing great, keep it up!
Lesson 13 - Comparison and Logical Operators