Home

Logical Conjunctions and Disjunctions

 

Logical Combinations

 

Introduction

To perform more advanced comparisons, you can combine two or more operations considered as one. This is done using logical NOT, AND, or OR operators.

 

The Logical Not Operator !

The six logical operators of C# clearly produce each a logical value based on the way the operation is presented. Another aspect that the review demonstrated was that

Operator Opposite
!= ==
< >=
<= >
> <=
>= <
== !=

Therefore, each operator has a clear opposite. If you may want to negate the result of a logical operation, you can use the Not operator. This operator is represented with !. Its formula is:

!Value

This operator is placed on the left side of a logical operation. Of course, to make its operand clear, the operation to be negated should be included in 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);
    bool NotBuiltTheSameYear = !(YearBuiltHouse1 == YearBuiltHouse2);
%>

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

</body>
</html>

This would produce:

Altair Real Estate

It is very important to understand how the NOT operator works: it negates its operand. This means that, without the ! operator, if the operand is supposed to produce a true result, it would produce a false result. On the other hand, without the ! operator, if the operand is supposed to produce a false result, it would produce a true result. The NOT operator doesn't have EITHER OR.

Logical Conjunction: AND

 

Introduction

Imagine a visitor who comes to your real estate web site to check the houses. A sample web page can be created as follows:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

private void btnCheckAvailabilityClick(object sender, EventArgs e)
{
    lblHouseType.Text = "House type selected: " + TypesOfHouses.Text;
    lblAffordability.Text = "Affordable amount: " + txtAmountAffordable.Text;
}
</script>

<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<form id="frmRealEstate" method="post" runat="server">
  <table>
    <tr>
      <td>What type of property do you want to purchase?</td>
      <td>
	<asp:DropDownList id="TypesOfHouses"
			  AutoPostBack="True"
			  runat="server">
    	  <asp:ListItem>Condominium</asp:ListItem>
    	  <asp:ListItem>Townhouse</asp:ListItem>
    	  <asp:ListItem>Single Family</asp:ListItem>
    	  <asp:ListItem>Unknown</asp:ListItem>
  	</asp:DropDownList>
      </td>
    </tr>
    <tr>
      <td>How much can you afford?</td>
      <td><asp:TextBox id="txtAmountAffordable"
                       Text="0.00" columns="10"
                       runat="server" /></td>
    </tr>
    <tr>
      <td colspan="2" align="right">
        <asp:Button id="btnCheckAvailability"
		    Text="Check Availability"
		    OnClick="btnCheckAvailabilityClick"
		    runat="server" /></td>
    </tr>
    <tr>
      <td colspan="2">
	<asp:Label id="lblHouseType" runat="server" /></td>
    </tr>
    <tr>
      <td colspan="2"><asp:Label id="lblAffordability" runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

Suppose a customer responds to these questions:

Real Estate

Imagine a visitor wants to purchase a single family that is up to 550000. We can create two statements as follows:

  • The house is single family. Its statement can be evaluated with the following:
     
    <script runat="server">
    
    private void btnCheckAvailabilityClick(object sender, EventArgs e)
    {
        bool MatchedHouseType = (TypesOfHouses.Text == "Single Family");
    }
    </script>
  • The house costs less than $550,000. Its statement can be evaluated with the following:
     
    <script runat="server">
    
    private void btnCheckAvailabilityClick(object sender, EventArgs e)
    {
        bool MatchedAffordability = 
    	(double.Parse(txtAmountAffordable.Text) <= 550000);
    }
    </script>

To combine two comparisons, you use the AND logical conjunction. In C#, it is represented with &&. To use it, you can write one condition, followed by the && operator, followed by the other condition. After that, write the desired statement. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

private void btnCheckAvailabilityClick(object sender, EventArgs e)
{
    bool MatchedHouseType = (TypesOfHouses.Text == "Single Family");
    bool MatchedAffordability = 
	(double.Parse(txtAmountAffordable.Text) <= 550000);
    
   lblMessage.Text = "Houe desired and price matched: " +
		      (MatchedHouseType &&  MatchedAffordability);    

}
</script>

<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<form id="frmRealEstate" method="post" runat="server">
  <table>
    <tr>
      <td>What type of property do you want to purchase?</td>
      <td>
	<asp:DropDownList id="TypesOfHouses"
			  AutoPostBack="True"
			  runat="server">
    	  <asp:ListItem>Condominium</asp:ListItem>
    	  <asp:ListItem>Townhouse</asp:ListItem>
    	  <asp:ListItem>Single Family</asp:ListItem>
    	  <asp:ListItem>Unknown</asp:ListItem>
  	</asp:DropDownList>
      </td>
    </tr>
    <tr>
      <td>How much can you afford?</td>
      <td><asp:TextBox id="txtAmountAffordable"
                       Text="0.00" columns="10"
                       runat="server" /></td>
    </tr>
    <tr>
      <td colspan="2" align="right">
        <asp:Button id="btnCheckAvailability"
		    Text="Check Availability"
		    OnClick="btnCheckAvailabilityClick"
		    runat="server" /></td>
    </tr>
    <tr>
      <td colspan="2">
	<asp:Label id="lblMessage" runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

Here is an example of executing this code:

Real Estate

Here is another execution of the web page:

Real Estate

Here is one more example:

Real Estate

 
 
 
 

Combining Conjunctions

The logical conjunction is used to combine two conditions. In some cases, you will need to combine more than two conditions. Imagine a customer wants to purchase a single family house that costs up to $550,000 with an indoor garage. This means that the house must fulfill these three requirements:

  1. The house is single family
  2. The house costs less than 550,001
  3. The house has an indoor garage

We also saw that when two conditions are combined, the first condition is checked, followed by the second. In the same way, if three conditions need to be considered, the truthfulness of the first condition is checked. If the first condition (or any condition) is false, the whole condition is false, regardless of the outcome of the other(s). If the first condition is true, then the second condition is evaluated for its truthfulness. If the second condition is false, the whole combination is considered false. When evaluating three conditions, if either the first or the second is false, since the whole condition would become false, there is no reason to evaluate the third. If both the first and the second conditions are false, there is also no reason to evaluate the third condition. Only if the first two conditions are true will the third condition be evaluated.

The combination of these conditions in a logical conjunction can be written as A && B && C. If the third condition is false, the whole combination is considered false. 

Logical Disjunction: OR

 

Introduction

Our real estate company has single family homes, townhouses, and condominium. All of the condos have only one story. Some of the single family homes have one story, some have two or some others have three levels. All townhouses have three stories.

Another customer wants to buy a home. The customer says that he primarily wants a condo, but if our real estate company doesn't have a condominium, that is, if the company has only houses, whatever it is, whether a house or a condo, it must have only one level (story). When considering the properties of our company, we would proceed with these statements:

  1. The property is a condominium
     
    OR
  2. The property has one story

In Boolean algebra, this type of comparison is performed using the OR operator. The OR operator is performed using the || operator. To use it, write a logical condition, followed by ||, followed by the other condition. Then, write the statement. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

private void btnCheckAvailabilityClick(object sender, EventArgs e)
{
    bool MatchedHouseType = (TypesOfHouses.Text == "Condominium");
    bool MatchedStories   = (double.Parse(Stories.Text) == 1);
    
   lblMessage.Text = "Property type or stories matched: " +
		      (MatchedHouseType ||  MatchedStories);    

}
</script>

<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<form id="frmRealEstate" method="post" runat="server">
  <table>
    <tr>
      <td>What type of property do you want to purchase?</td>
      <td>
	<asp:DropDownList id="TypesOfHouses"
			  AutoPostBack="True"
			  runat="server">
    	  <asp:ListItem>Condominium</asp:ListItem>
    	  <asp:ListItem>Townhouse</asp:ListItem>
    	  <asp:ListItem>Single Family</asp:ListItem>
    	  <asp:ListItem>Unknown</asp:ListItem>
  	</asp:DropDownList>
      </td>
    </tr>
    <tr>
      <td>How many levels (stories) should be the house have?</td>
      <td><asp:DropDownList id="Stories"
			  AutoPostBack="True"
			  runat="server">
    	  <asp:ListItem>1</asp:ListItem>
    	  <asp:ListItem>2</asp:ListItem>
    	  <asp:ListItem>3</asp:ListItem>
  	</asp:DropDownList></td>
    </tr>
    <tr>
      <td colspan="2" align="right">
        <asp:Button id="btnCheckAvailability"
		    Text="Check Availability"
		    OnClick="btnCheckAvailabilityClick"
		    runat="server" /></td>
    </tr>
    <tr>
      <td colspan="2">
	<asp:Label id="lblMessage" runat="server" /></td>
    </tr>
  </table>
</form>

</body>
</html>

This would produce:

Real Estate

Here is another inquiry on the same web page:

Real Estate

Here is one more inquiry:

Real Estate

 

Combinations of Disjunctions

As opposed to evaluating only two conditions, you may face a situation that presents three of them and must consider a combination of more than two conditions.

 
 
   
 

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