ASP.NET Basics (part 3): Hard Choices - Friday The 13th (Page 6 of 8 )
If you take a close look at the "if-else if-else" statement example a few pages back, you'll notice that the conditional expression
temp
< 25 && temp >= 10
is slightly different from the ones you've been used to thus far. This is because C# also allows you to combine multiple conditions into a single expression, with the help of an animal called a "logical operator".
The following table should make this clearer.
assume delta = 12, gamma = 12 and omega = 9
Operator What It Means Expression Result
--------------------------------------------------------------------------
&& AND delta == gamma && delta > omega True
&& AND delta == gamma && delta < omega False
|| OR delta == gamma || delta < omega True
|| OR delta > gamma || delta < omega False
! NOT !delta False
Logical operators are useful because they allow you to logically combine or group different conditional tests together, thereby making it possible to enforce complex business rules in your application logic without too much effort.
So, instead of something as ugly as this
<%
if (day == "Friday")
{
if (date == "13")
{
if (time == "1300 hours")
{
fortune = "Brace yourself for some bad luck, it's Friday the 13th";
}
}
}
%>
you could have something as elegant as this.
<%
if (day == "Friday" && date == "13" && time == "1300 hours")
{
fortune = "Brace yourself for some bad luck, it's Friday the 13th"; }
%>
Next: Switching Things Around >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire