Conditional Conjunctions and Disjunctions
Conditional Conjunctions and Disjunctions
Conditional Conjunctions
Nesting a Conditional Statement
A conditional statement has a body, which is from where the condition is defined to where its behavior ends. In the body of the conditional statement, you can create another conditional statement. This is referred to as nesting the condition. In a method of a class, the condition nesting can be formulated as follows:
if( condition1 ) // The nesting, main, parent, first, or external condition if( condition2 ) // The nested, child, second, or internal condition statement(s)
If you are writing your code in a webpage, each condition must be delimitad by curly brackets:
if( condition1 ) { if( condition2 ) { statement(s) } }
In the same way, you can nest one conditional statement in one, then nest that new one in another conditional statement, and so on. In a method, this can be formulated as follows:
if( condition1 ) if( condition2 ) if( condition3 ) statement(s)
Remember, in the code of a webpage, each statement must be included in curly brackets:
if( condition1 ) { if( condition2 ) { if( condition3 ) { statement(s) } } }
Practical Learning: Introducing Boolean Conjunctions
.container { width: 300px; margin: auto; } .boldness { font-weight: bold } .col-format { width: 150px; } .col-second { width: 120px; }
Remember that you can nest one condition in another condition as in:
if( condition1 ) if( condition2 ) statement(s)
Or:
if( condition1 ) { if( condition2 ) { statement(s) } }
When you nest a condition, you are in fact indicating that "if condition1 verifies, then if condition2 verifies, do this...". The external condition must be verified first as being true or false (depending on how you wrote the conditional statement). If that first (the external) condition is true, the second (the internal) condition is checked. If the first (or external) condition is not valid, the second (the internal) condition is not evaluated. To support a simplified technique to apply this description, the C# language provides the Boolean "AND" operator, represented as &&. Its primary formula is:
condition1 && condition2
You must formulate each condition to produce a true or a false result. The result is as follows:
Practical Learning: Introducing Boolean Conjunctions
@helper Trade(decimal numberOfShares, decimal pricePerShare) { decimal principal = 0.00m; decimal commission = 0.00m; string strCommission = ""; string strPrincipal = "0.00"; string strTotalInvestment = ""; principal = numberOfShares * pricePerShare; if (principal >= 0m && principal <= 2500m) { commission = 26.25m + (principal * 0.0014m); } if (principal > 2500m && principal <= 6000m) { commission = 45.00m + (principal * 0.0054m); } if (principal > 6000m && principal <= 20000m) { commission = 60.00m + (principal * 0.0028m); } if (principal > 20000m && principal <= 50000m) { commission = 75.00m + (principal * 0.001875m); } if (principal > 50000m && principal <= 500000m) { commission = 131.25m + (principal * 0.0009m); } if (principal > 500000m) { commission = 206.25m + (principal * 0.000075m); } strPrincipal = principal.ToString("F"); strCommission = commission.ToString("F"); strTotalInvestment = (principal + commission).ToString("F"); <form name="frmStocks" method="post"> <table> <tr> <td class="col-format boldness">Principal:</td> <td><input type="text" name="txtPrincipal" value="@strPrincipal" /></td> </tr> <tr> <td class="boldness">Commission:</td> <td><input type="text" name="txtCommission" value="@strCommission" /></td> </tr> <tr> <td class="boldness">Total Investment:</td> <td><input type="text" name="txtTotalInvestment" value="@strTotalInvestment" /></td> </tr> </table> </form> }
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="~/Content/Site.css" /> <title>Stock Broker Company</title> </head> <body> @{ int numberOfShares = 0; decimal pricePerShare = 0.00m; if (IsPost) { numberOfShares = Request["txtNumberOfShares"].AsInt(); pricePerShare = Request["txtPricePerShare"].AsDecimal(); } } <div class="container"> <h2>Stock Broker Company</h2> <form name="frmStocks" method="post"> <table> <tr> <td class="col-format boldness">Number of Shares:</td> <td><input type="text" name="txtNumberOfShares" value="@numberOfShares" /></td> </tr> <tr> <td class="col-second boldness">Price Per Share:</td> <td><input type="text" name="txtPricePerShare" value="@pricePerShare" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="txtSubmit" class="col-format" value=Calculate /></td> </tr> </table> </form> @StocksProcessing.Trade(@numberOfShares, @pricePerShare) </div> </body> </html>
@helper Trade(decimal numberOfShares, decimal pricePerShare) { decimal principal = 0.00m; decimal commission = 0.00m; string strCommission = ""; string strPrincipal = "0.00"; string strTotalInvestment = ""; principal = numberOfShares * pricePerShare; if( (principal >= 0m) && (principal <= 2500m) ) { commission = 26.25m + (principal * 0.0014m); } if( (principal > 2500m) && (principal <= 6000m) ) { commission = 45.00m + (principal * 0.0054m); } if( (principal > 6000m) && (principal <= 20000m) ) { commission = 60.00m + (principal * 0.0028m); } if( (principal > 20000m) && (principal <= 50000m) ) { commission = 75.00m + (principal * 0.001875m); } if( (principal > 50000m) && (principal <= 500000m) ) { commission = 131.25m + (principal * 0.0009m); } if (principal > 500000m) { commission = 206.25m + (principal * 0.000075m); } strPrincipal = principal.ToString("F"); strCommission = commission.ToString("F"); strTotalInvestment = (principal + commission).ToString("F"); <form name="frmStocks" method="post"> <table> <tr> <td class="col-format boldness">Principal:</td> <td><input type="text" name="txtPrincipal" value="@strPrincipal" /></td> </tr> <tr> <td class="boldness">Commission:</td> <td><input type="text" name="txtCommission" value="@strCommission" /></td> </tr> <tr> <td class="boldness">Total Investment:</td> <td><input type="text" name="txtTotalInvestment" value="@strTotalInvestment" /></td> </tr> </table> </form> }
Combining Various Conjunctions
Depending on your goal, if two conditions are not enough, you can create as many conjunctions as you want. The formula to follow is:
condition1 && condition2 && condition3 && . . . && condition_n
When the expression is checked, if any of the operations is false, the whole operation is false. The only time the whole operation is true is if all of the operations are true.
Of course, you can nest a Boolean condition inside another conditional statement.
Boolean Disjunctions
A Boolean disjunction is a conditional statement where you combine more than one condition but only one of the conditions needs to be true for the whole operation to be true. This operation is performed using the Boolean "OR" operator represented as ||. The primary formula to follow is:
condition1 || condition2
The operation works as follows:
Practical Learning: Introducing Logical Disjunctions
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Chemistry07.App_Code { public enum Phase { Gas, Liquid, Solid, Unknown } public class Element { public string Symbol { get; set; } = "H"; public string ElementName { get; set; } = "Hydrogen"; public int AtomicNumber { get; set; } = 1; public decimal AtomicWeight { get; set; } = 1.008M; public Phase Phase { get; set; } = Phase.Gas; public Element() { } public Element(int number) { AtomicNumber = number; } public Element(string symbol) { Symbol = symbol; } public Element(int number, string symbol, string name, decimal mass) { Symbol = symbol; ElementName = name; AtomicWeight = mass; AtomicNumber = number; } } }
body { margin: 0; background-color: #FFFFFF; } #top-banner { top: 0; width: 100%; height: 60px; position: fixed; background-color: #003366; border-bottom: 5px solid black; } .title-holder { top: 65px; width: 100%; height: 34px; position: fixed; } .container { width: 300px; margin: auto; padding-top: 10px; } .item-selection { width: 350px; margin: auto; padding-top: 120px; } #main-title { font-weight: 800; margin-top: 16px; font-size: 2.12em; text-align: center; color: yellow; font-family: Garamond, 'Times New Roman', Georgia, serif } #sub-title { color: navy; font-weight: 600; padding-top: 2px; font-size: 1.68em; text-align: center; font-family: Garamond, 'Times New Roman', Georgia, serif } .small-text { width: 40px; } .emphasize { font-weight: bold; } .left-col { width: 120px; }
@helper SelectElement(string symbol) { Chemistry07.App_Code.Element selected = new Chemistry07.App_Code.Element(); Chemistry07.App_Code.Element h = new Chemistry07.App_Code.Element(1, "H", "Hydrogen", 1.008M) { Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element he = new Chemistry07.App_Code.Element(2, "He", "Helium", 4.002602M) { Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element li = new Chemistry07.App_Code.Element(3, "Li", "Lithium", 6.94M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element be = new Chemistry07.App_Code.Element(4, "Be", "Beryllium", 9.0121831M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element b = new Chemistry07.App_Code.Element(5, "B", "Boron", 10.81M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element c = new Chemistry07.App_Code.Element(name: "Carbon", mass: 12.011M, symbol: "C", number: 6) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element n = new Chemistry07.App_Code.Element(7); n.Symbol = "N"; n.AtomicWeight = 14.007M; n.ElementName = "Nitrogen"; n.Phase = Chemistry07.App_Code.Phase.Gas; Chemistry07.App_Code.Element o = new Chemistry07.App_Code.Element("O") { AtomicNumber = 8, ElementName = "Oxygen", AtomicWeight = 15.999M, Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element f = new Chemistry07.App_Code.Element("F") { AtomicNumber = 9, ElementName = "Fluorine", AtomicWeight = 15.999M, Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element ne = new Chemistry07.App_Code.Element("Ne") { AtomicNumber = 10, ElementName = "Neon", AtomicWeight = 20.1797M, Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element na = new Chemistry07.App_Code.Element(11, "Na", "Sodium", 22.98976928M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element mg = new Chemistry07.App_Code.Element(12, "Mg", "Magnesium", 24.305M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element al = new Chemistry07.App_Code.Element(13, "Al", "Aluminium", 26.9815385M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element si = new Chemistry07.App_Code.Element() { ElementName = "Silicon", AtomicWeight = 28.085M, Symbol = "Si", AtomicNumber = 14, Phase = Chemistry07.App_Code.Phase.Solid }; if ( symbol == "h" || symbol == "H") { selected = h; } else if( symbol == "he" || symbol == "He" || symbol == "HE" || symbol == "hE") { selected = he; } else if( symbol == "li" || symbol == "Li" || symbol == "LI" || symbol == "lI") { selected = li; } else if( symbol == "be" || symbol == "Be" || symbol == "BE" || symbol == "bE") { selected = be; } else if( symbol == "b" || symbol == "B") { selected = b; } else if( symbol == "c" || symbol == "C") { selected = c; } else if( symbol == "n" || symbol == "N") { selected = n; } else if( symbol == "o" || symbol == "O") { selected = o; } else if( symbol == "F" || symbol == "f") { selected = f; } else if( symbol == "ne" || symbol == "Ne" || symbol == "NE" || symbol == "nE") { selected = ne; } else if( symbol == "na" || symbol == "NA" || symbol == "Na" || symbol == "nA") { selected = na; } else if( symbol == "mg" || symbol == "Mg" || symbol == "MG" || symbol == "mG") { selected = mg; } else if( symbol == "al" || symbol == "Al" || symbol == "AL" || symbol == "aL") { selected = al; } else if( symbol == "si" || symbol == "Si" || symbol == "SI" || symbol == "sI") { selected = si; } <form name="frmChemistry" method="post"> <table> <tr> <td class="left-col emphasize">Atomic Number:</td> <td><input type="text" name="txtAtomicNumber" value=@selected.AtomicNumber /></td> </tr> <tr> <td class="emphasize">Symbol:</td> <td><input type="text" name="txtSymbol" value=@selected.Symbol /></td> </tr> <tr> <td class="emphasize">Element Name:</td> <td><input type="text" name="txtElementName" value=@selected.ElementName /></td> </tr> <tr> <td class="emphasize">Atomic Weight:</td> <td><input type="text" name="txtAtomicWeight" value=@selected.AtomicWeight /></td> </tr> <tr> <td class="emphasize">Phase:</td> <td><input type="text" name="txtAtomicWeight" value=@selected.Phase /></td> </tr> </table> </form> }
<!DOCTYPE html> <html> <head> <title>Chemistry</title> <link rel="stylesheet" type="text/css" href="~/Content/Site.css" /> </head> <body> @{ string valueEntered = ""; if(IsPost) { valueEntered = Request["txtValueEntered"]; } } <div id="top-banner"> <p id="main-title">Chemistry</p> </div> <div class="title-holder"> <div id="sub-title"></div> <hr /> </div> <div class="item-selection"> <form name="frmChemistry" method="post"> <table> <tr> <td class="emphasize">Enter Element Symbol:</td> <td> <input type="text" name="txtValueEntered" class="small-text" value="@valueEntered" /> <input type="submit" name="btnFind" value="Find" style="width: 70px;" /> </td> </tr> </table> </form> @ElementsProcessing.SelectElement(@valueEntered) </div> </body> </html>
@helper SelectElement(string symbol) { Chemistry07.App_Code.Element selected = new Chemistry07.App_Code.Element(); Chemistry07.App_Code.Element h = new Chemistry07.App_Code.Element(1, "H", "Hydrogen", 1.008M) { Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element he = new Chemistry07.App_Code.Element(2, "He", "Helium", 4.002602M) { Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element li = new Chemistry07.App_Code.Element(3, "Li", "Lithium", 6.94M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element be = new Chemistry07.App_Code.Element(4, "Be", "Beryllium", 9.0121831M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element b = new Chemistry07.App_Code.Element(5, "B", "Boron", 10.81M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element c = new Chemistry07.App_Code.Element(name: "Carbon", mass: 12.011M, symbol: "C", number: 6) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element n = new Chemistry07.App_Code.Element(7); n.Symbol = "N"; n.AtomicWeight = 14.007M; n.ElementName = "Nitrogen"; n.Phase = Chemistry07.App_Code.Phase.Gas; Chemistry07.App_Code.Element o = new Chemistry07.App_Code.Element("O") { AtomicNumber = 8, ElementName = "Oxygen", AtomicWeight = 15.999M, Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element f = new Chemistry07.App_Code.Element("F") { AtomicNumber = 9, ElementName = "Fluorine", AtomicWeight = 15.999M, Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element ne = new Chemistry07.App_Code.Element("Ne") { AtomicNumber = 10, ElementName = "Neon", AtomicWeight = 20.1797M, Phase = Chemistry07.App_Code.Phase.Gas }; Chemistry07.App_Code.Element na = new Chemistry07.App_Code.Element(11, "Na", "Sodium", 22.98976928M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element mg = new Chemistry07.App_Code.Element(12, "Mg", "Magnesium", 24.305M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element al = new Chemistry07.App_Code.Element(13, "Al", "Aluminium", 26.9815385M) { Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element si = new Chemistry07.App_Code.Element() { ElementName = "Silicon", AtomicWeight = 28.085M, Symbol = "Si", AtomicNumber = 14, Phase = Chemistry07.App_Code.Phase.Solid }; Chemistry07.App_Code.Element p = new Chemistry07.App_Code.Element() { ElementName = "Phosphorus", AtomicWeight = 30.973761998M, Symbol = "P", AtomicNumber = 15, Phase = Chemistry07.App_Code.Phase.Solid }; if ( (symbol == "h") || (symbol == "H") ) { selected = h; } else if( (symbol == "he") || (symbol == "He") || (symbol == "HE") || (symbol == "hE") ) { selected = he; } else if( (symbol == "li") || (symbol == "Li") || (symbol == "LI") || (symbol == "lI") ) { selected = li; } else if( (symbol == "be") || (symbol == "Be") || (symbol == "BE") || (symbol == "bE") ) { selected = be; } else if( (symbol == "b") || (symbol == "B") ) { selected = b; } else if( (symbol == "c") || (symbol == "C") ) { selected = c; } else if( (symbol == "n") || (symbol == "N") ) { selected = n; } else if( (symbol == "o") || (symbol == "O") ) { selected = o; } else if( (symbol == "F") || (symbol == "f") ) { selected = f; } else if( (symbol == "ne") || (symbol == "Ne") || (symbol == "NE") || (symbol == "nE") ) { selected = ne; } else if( (symbol == "na") || (symbol == "NA") || (symbol == "Na") || (symbol == "nA") ) { selected = na; } else if( (symbol == "mg") || (symbol == "Mg") || (symbol == "MG") || (symbol == "mG") ) { selected = mg; } else if( (symbol == "al") || (symbol == "Al") || (symbol == "AL") || (symbol == "aL") ) { selected = al; } else if( (symbol == "si") || (symbol == "Si") || (symbol == "SI") || (symbol == "sI") ) { selected = si; } else if( (symbol == "p") || (symbol == "P") ) { selected = p; } <form name="frmChemistry" method="post"> <table> <tr> <td class="left-col emphasize">Atomic Number:</td> <td><input type="text" name="txtAtomicNumber" value=@selected.AtomicNumber /></td> </tr> <tr> <td class="emphasize">Symbol:</td> <td><input type="text" name="txtSymbol" value=@selected.Symbol /></td> </tr> <tr> <td class="emphasize">Element Name:</td> <td><input type="text" name="txtElementName" value=@selected.ElementName /></td> </tr> <tr> <td class="emphasize">Atomic Weight:</td> <td><input type="text" name="txtAtomicWeight" value=@selected.AtomicWeight /></td> </tr> <tr> <td class="emphasize">Phase:</td> <td><input type="text" name="txtAtomicWeight" value=@selected.Phase /></td> </tr> </table> </form> }
Combining Various Disjunctions
You can create a conditional statement that includes as many disjunctions as you want. The formula to follow is:
condition1 || condition2 || . . . || condition_n
The rule is the same: If any one of the individual operations is true, the whole operation is true. The whole operation is false only if all of the operations are false.
Combining Conjunctions and Disjunctions
Conjunctions and disjunctions can be used in the same expression. A conjunction (or disjunction) can be used to evaluate one sub-expression while a disjunction (or conjunction) can be used to evaluate another sub-expression.
Practical Learning: Ending the Lesson
|
||
Previous | Copyright © 2001-2019, FunctionX | Next |
|