Logical Operators and Boolean Expressions
You are already familiar with these. Recall that in the preceding exercise, you did the following comparison:
var now = DateTime.Now.Date == DateTime.UtcNow.Date;
This output assigns the value true
to now
if the dates are equal. But as you know, they might not necessarily be the same. Therefore, if the dates are different, a false
value will be assigned. These two values are the result of such Boolean expressions and are called Boolean values. That is why the now
variable has the type of bool
.
Boolean expressions are the base for every logical comparison in every program. Based on these comparisons, a computer can execute a certain behavior in a program. Here are some other examples of Boolean expressions and variable assignments:
- Assigning the result of a comparison that checks whether
a
is greater thanb
:var basicComparison = a > b;
- Assigning the result of a comparison that checks whether
b
is greater than or...