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:
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.
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:
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: Here is another inquiry on the same web page:
Here is one more inquiry:
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. |
|
|||||||||
|