Home

Logical Operations

 

Introduction

 

A logical operation is used to compare two values. The values must be of the same types. To support comparisons, the C# language provides 6 operators that are used in the form:

Operand1 Operator Operand2

After the comparison has been performed, it produces a result recognized as being true, or as being false.

Equality ==

To compare two variables for equality, you can use the == operator. Its formula is:

Value1 == Value2

It can be illustrated as follows:

Logical Operators

Most of the comparisons performed will be applied to conditional statements. Most of the time, you will not need to display the result on a web page but you can. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<%
    int YearBuilt = 1962;
%>

<%
    Response.Write("Comparison of YearBuilt == 1998 produces ");
    Response.Write(YearBuilt == 1998);
%>
</body>
</html>

This would produce:

Real Estate

Because a Boolean operation produces a true or a false value, the result of its comparison can also be assigned to a Boolean variable. To store the result of a comparison, you should include the comparison operation between parentheses. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<%
    int YearBuiltHouse1 = 1962;
    int YearBuiltHouse2 = 1998;
    bool BuiltTheSameYear = (YearBuiltHouse1 == YearBuiltHouse2);
%>

<%
    Response.Write("<pre>House1 Year Built: " + YearBuiltHouse1);
    Response.Write("<br>House2 Year Built: " + YearBuiltHouse2);
    Response.Write("<br>Built the same year? " + BuiltTheSameYear + "<pre>");
%>
</body>
</html>

This would produce:

Real Estate

Inequality !=

Instead of two values being equal, you may be interested to know whether they are not equal. To perform this comparison, you can use the != operator. The formula used is:

Value1 != Value2

This can be illustrated as follows:

Not Equal

Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<%
    int YearBuiltHouse1 = 1962;
    int YearBuiltHouse2 = 1998;
    bool BuiltTheSameYear = (YearBuiltHouse1 != YearBuiltHouse2);
%>

<%
    Response.Write("<pre>House1 Year Built:        " + YearBuiltHouse1);
    Response.Write("<br>House2 Year Built:        " + YearBuiltHouse2);
    Response.Write("<br>Built in different years? " + BuiltTheSameYear + "<pre>");
%>
</body>
</html>

This would produce:

Real Estate

Less Than <

Instead of strict equality or strict inequality, you may want to know whether one value is lower than another. To perform this comparison, you can use the < operator. Its formula is:

Value1 < Value2

This can be illustrated as follows:

Less Than

Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<%
    int YearBuiltHouse1 = 1962;
    int YearBuiltHouse2 = 1998;
    bool House1BuiltBeforeHouse2 = (YearBuiltHouse1 < YearBuiltHouse2);
%>

<%
    Response.Write("<pre>House1 Year Built:          " + YearBuiltHouse1);
    Response.Write("<br>House2 Year Built:          " + YearBuiltHouse2);
    Response.Write("<br>House1 built before House2? " +
	 House1BuiltBeforeHouse2 + "<pre>");
%>
</body>
</html>

This would produce:

Real Estate

 

Less Than or Equal to <=

The Less Than operator is used to find out if one value is strictly lower than another. An alternative to being so restrictive is to find if one value, instead of being absolutely less than another, whether both values could be equal instead. This type of comparison can be performed using the <= operator. Its formula is:

Value1 <= Value2

The <= operation performs a comparison as any of the last two. If both Value1 and Value2 hold the same value, the result is true. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true:

Less Than Or Equal

Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<%
    int YearBuiltHouse1 = 1962;
    int YearBuiltHouse2 = 1998;
    bool House1EitherBuiltBeforeHouse2OrTheSameYear = 
		(YearBuiltHouse1 <= YearBuiltHouse2);
%>

<%
    Response.Write("<pre>House1 Year Built: " + YearBuiltHouse1);
    Response.Write("<br>House2 Year Built: " + YearBuiltHouse2);
    Response.Write("<br>House1 built before House2 or the same year? " + 
		House1EitherBuiltBeforeHouse2OrTheSameYear + "<pre>");
%>
</body>
</html>

This would produce:

Real Estate

Greater Than >

If you have two values of the same type that are different, one of them is usually higher than the other. To get the result of this type of comparison, you can use the Greater Than operator >. The formula to use it is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the > operator is greater than the value on the right side or a constant, the comparison produces a true or positive value. Otherwise, the comparison renders false:

Greater Than

This is an example of using the > operator:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<%
    int YearBuiltHouse1 = 1962;
    int YearBuiltHouse2 = 1998;
    bool House2BuiltAfterHouse1 = (YearBuiltHouse2 > YearBuiltHouse1);
%>

<%
    Response.Write("<pre>House1 Year Built: " + YearBuiltHouse1);
    Response.Write("<br>House2 Year Built: " + YearBuiltHouse2);
    Response.Write("<br>House2 built after House1? " + 
		House2BuiltAfterHouse1 + "<pre>");
%>
</body>
</html>

This would produce:

Real Estate

Greater Than or Equal to >=

To find out whether one value is greater than another, you can use the >= operator. This is the "greater than or equal to" operator. Its formula is:

Value1 >= Value2

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a true or positive value. If the value of the left operand is greater than that of the right operand,, the comparison produces true or positive also. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false or null result.

Greater Than or Equal

 

Here is an example that performs this comparison:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<%
    int YearBuiltHouse1 = 1962;
    int YearBuiltHouse2 = 1998;
    bool House1AndHouse2PossiblyBuiltSameYear = 
		(YearBuiltHouse2 >= YearBuiltHouse1);
%>

<%
    Response.Write("<pre>House1 Year Built: " + YearBuiltHouse1);
    Response.Write("<br>House2 Year Built: " + YearBuiltHouse2);
    Response.Write("<br>House1 and House2 possibly built the same year ? " + 
		House1AndHouse2PossiblyBuiltSameYear + "<pre>");
%>
</body>
</html>

This would produce:

Real Estate

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next