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)
Here is an example:
using static System.Console;
public class VaccinationCampaign
{
public static int Main()
{
int ageRange = 0;
string gender = "u";
string patientName = null;
WriteLine("Department of Health");
WriteLine("--------------------------------------------------");
WriteLine("Patient's Diagnosis");
Write("Patient's Name: ");
patientName = ReadLine();
Write("Patient's Gender (m - Male, f - Female): ");
gender = ReadLine();
Write("Is the patient a toddler/kid or a teen/adult (0 - Unknown, 1 - Toddler/Kid, 2 - Teen/Adult): ");
ageRange = int.Parse(ReadLine());
Clear();
WriteLine("Department of Health");
WriteLine("--------------------------------------------------");
WriteLine("Patient's Diagnosis");
WriteLine("Name: " + patientName);
WriteLine("Gender: " + gender);
if (gender == "f")
if (ageRange == 2)
WriteLine("Age Range: Young Adult or Lady");
WriteLine("==================================================");
return 0;
}
}
Here is an example of running the program:
Department of Health -------------------------------------------------- Patient's Diagnosis Patient's Name: Joan Swanson Patient's Gender (m - Male, f - Female): f Is the patient a toddler/kid or a teen/adult (0 - Unknown, 1 - Toddler/Kid, 2 - Teen/Adult): 2
The resulting summary would be:
Department of Health -------------------------------------------------- Patient's Diagnosis Name: Joan Swanson Gender: f Age Range: Young Adult or Lady ================================================== Press any key to continue . . .
If the condition must include many statements, you should delimit their section with 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)
Once again, any condition that includes more than one statement must have its statements included in curly brackets:
if( condition1 ) { if( condition2 ) { if( condition3 ) { statement(s) } } }
Here is an example:
using static System.Console; public class VaccinationCampaign { private static void InCaseOfPregnancy() { WriteLine("The following vaccinations are recommended:"); WriteLine("1. Influenza (Inactivated)"); WriteLine("2. Tdap"); WriteLine("3. Hepatitis B: The patient should consult her physician"); } public static int Main() { int age = 0; string range = null; string gender = "u"; string pregnancy = null; string patientName = null; WriteLine("Department of Health"); WriteLine("--------------------------------------------------"); WriteLine("Patient's Diagnosis"); Write("Patient's Name: "); patientName = ReadLine(); Write("Patient's Gender (m - Male, f - Female): "); gender = ReadLine(); Write("Is the patient a toddler/kid or a teen/adult (0 - Unknown, 1 - Toddler/Kid, 2 - Teen/Adult): "); age = int.Parse(ReadLine()); if (age == 2) range = "Young Adult or Lady"; Clear(); WriteLine("Department of Health"); WriteLine("--------------------------------------------------"); WriteLine("Patient's Diagnosis"); WriteLine("Name: " + patientName); WriteLine("Gender: " + gender); WriteLine("Age Range: " + range); if (gender == "f") { if (age == 2) { Write("Is the patient pregnant (y/n)? "); pregnancy = ReadLine(); if (pregnancy == "y") InCaseOfPregnancy(); } } WriteLine("=================================================="); return 0; } }
Here is an example of running the program:
epartment of Health ------------------------------------------------- atient's Diagnosis atient's Name: John Critenden atient's Gender (m - Male, f - Female): m s the patient a toddler/kid or a teen/adult (0 - Unknown, 1 - Toddler/Kid, 2 - een/Adult): 1
The second part would be:
Department of Health -------------------------------------------------- Patient's Diagnosis Name: John Critenden Gender: m Age Range: Toddle or kid ================================================== Press any key to continue . . .
Here is another example of running the program:
Department of Health -------------------------------------------------- Patient's Diagnosis Patient's Name: Joan Swanson Patient's Gender (m - Male, f - Female): f Is the patient a toddler/kid or a teen/adult (0 - Unknown, 1 - Toddler/Kid, 2 - Teen/Adult): 2
The second part is:
epartment of Health ------------------------------------------------- atient's Diagnosis ame: Veronica Clarenden ender: f ge Range: Young Adult or Lady s the patient pregnant (y/n)? y
The summary result is:
Department of Health -------------------------------------------------- Patient's Diagnosis Name: Joan Swanson Gender: f Age Range: Young Adult or Lady Is the patient pregnant (y/n)? y The following vaccinations are recommended: 1. Influenza (Inactivated) 2. Tdap 3. Hepatitis B: The patient should consult her physician ================================================== Press any key to continue . . .
Practical Learning: Introducing Boolean Conjunctions
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
using static System.Console; namespace StockBrokerCompany1 { public class StocksProcessing { private static int Main() { int numberOfShares = 0; double pricePerShare = 0.00; WriteLine("Stock Broker Company - Stock Evaluation"); WriteLine("------------------------"); WriteLine("Enter the following values"); Write("Number of Shares: "); numberOfShares = int.Parse(ReadLine()); Write("Price Per Share: "); pricePerShare = double.Parse(ReadLine()); double principal = 0.00; double commission = 0.00; double totalInvestment = 0.00; principal = numberOfShares * pricePerShare; if (principal >= 0 && principal <= 2500) { commission = 26.25 + (principal * 0.0014); } if (principal > 2500 && principal <= 6000) { commission = 45.00 + (principal * 0.0054); } if (principal > 6000 && principal <= 20000) { commission = 60.00 + (principal * 0.0028); } if (principal > 20000 && principal <= 50000) { commission = 75.00 + (principal * 0.001875); } if (principal > 50000 && principal <= 500000) { commission = 131.25 + (principal * 0.0009); } if (principal > 500000) { commission = 206.25 + (principal * 0.000075); } totalInvestment = principal + commission; Clear(); WriteLine("Stock Broker Company - Stock Evaluation"); WriteLine("---------------------------------------"); WriteLine("Number of Shares: {0}", numberOfShares); WriteLine("Price Per Share: {0:c}", pricePerShare); WriteLine("Principal: {0:C}", principal); WriteLine("Commission: {0:c}", commission); WriteLine("Total Investment: {0:C}", totalInvestment); WriteLine("======================================"); return 0; } } }
Stock Broker Company - Stock Evaluation ------------------------ Enter the following values Number of Shares:
tock Broker Company - Stock Evaluation ----------------------- nter the following values umber of Shares: 64788 rice Per Share: 36.85
Stock Broker Company - Stock Evaluation --------------------------------------- Number of Shares: 64788 Price Per Share: $36.85 Principal: $2,387,437.80 Commission: $385.31 Total Investment: $2,387,823.11 ====================================== Press any key to continue . . .
using static System.Console; namespace StockBrokerCompany1 { public class StocksProcessing { private static int Main() { int numberOfShares = 0; double pricePerShare = 0.00; WriteLine("Stock Broker Company - Stock Evaluation"); WriteLine("------------------------"); WriteLine("Enter the following values"); Write("Number of Shares: "); numberOfShares = int.Parse(ReadLine()); Write("Price Per Share: "); pricePerShare = double.Parse(ReadLine()); double principal = 0.00; double commission = 0.00; double totalInvestment = 0.00; principal = numberOfShares * pricePerShare; if( (principal >= 0) && (principal <= 2500) ) { commission = 26.25 + (principal * 0.0014); } if( (principal > 2500) && (principal <= 6000) ) { commission = 45.00 + (principal * 0.0054); } if( (principal > 6000) && (principal <= 20000) ) { commission = 60.00 + (principal * 0.0028); } if( (principal > 20000) && (principal <= 50000) ) { commission = 75.00 + (principal * 0.001875); } if( (principal > 50000) && (principal <= 500000) ) { commission = 131.25 + (principal * 0.0009); } if (principal > 500000) { commission = 206.25 + (principal * 0.000075); } totalInvestment = principal + commission; Clear(); WriteLine("Stock Broker Company - Stock Evaluation"); WriteLine("---------------------------------------"); WriteLine("Number of Shares: {0}", numberOfShares); WriteLine("Price Per Share: {0:c}", pricePerShare); WriteLine("Principal: {0:C}", principal); WriteLine("Commission: {0:c}", commission); WriteLine("Total Investment: {0:C}", totalInvestment); WriteLine("======================================"); return 0; } } }
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:
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.
Practical Learning: Introducing Logical Disjunctions
namespace Chemistry04 { public class Element { public string Symbol; public string ElementName; public int AtomicNumber; public double AtomicWeight; public Element() { Symbol = "H"; ElementName = "Hydrogen"; AtomicWeight = 1.008; AtomicNumber = 1; } public Element(int number) { AtomicNumber = number; } public Element(string symbol) { Symbol = symbol; } public Element(int number, string symbol, string name, double mass) { Symbol = symbol; ElementName = name; AtomicWeight = mass; AtomicNumber = number; } } }
using static System.Console; namespace Chemistry01 { public class Chemistry { private static void Describe(Element e) { WriteLine("Chemistry - " + e.ElementName); WriteLine("------------------------"); WriteLine("Symbol: " + e.Symbol); WriteLine("Element Name: " + e.ElementName); WriteLine("Atomic Number: " + e.AtomicNumber); WriteLine("Atomic Weight: " + e.AtomicWeight); } public static int Main() { Element elm = null; Element h = new Element(1, "H", "Hydrogen", 1.008); Element he = new Element(2, "He", "Helium", 4.002602); Element li = new Element(3, "Li", "Lithium", 6.94); Element be = new Element(4, "Be", "Beryllium", 9.0121831); Element b = new Element(5, "B", "Boron", 10.81); Element c = new Element(name: "Carbon", mass: 12.011, symbol: "C", number: 6); Element n = new Element(number: 7, symbol: "N", name: "Nitrogen", mass: 14.007); Element o = new Element(number: 8, symbol: "O", name: "Oxygen", mass: 15.999); Element f = new Element(number: 9, symbol: "F", name: "Fluorine", mass: 15.999); Element ne = new Element(number: 10, symbol: "Ne", name: "Neon", mass: 20.1797); Element na = new Element(11, "Na", "Sodium", mass: 22.98976928); Element mg = new Element(12, "Mg", "Magnesium", 24.305); Element al = new Element(13, "Al", "Aluminium", 26.9815385); Element si = new Element() { ElementName = "Silicon", AtomicWeight = 28.085, Symbol = "Si", AtomicNumber = 14 }; WriteLine("Chemistry - Periodic Table"); WriteLine("------------------------------------------------"); Write("Type the atomic symbol of an element you want to review (H to Si): "); string symbol = ReadLine(); if (symbol == "h" || symbol == "H") { elm = h; } else if (symbol == "he" || symbol == "He" || symbol == "HE" || symbol == "hE") { elm = he; } else if (symbol == "li" || symbol == "Li" || symbol == "LI" || symbol == "lI") { elm = li; } else if (symbol == "be" || symbol == "Be" || symbol == "BE" || symbol == "bE") { elm = be; } else if (symbol == "b" || symbol == "B") { elm = b; } else if (symbol == "c" || symbol == "C") { elm = c; } else if (symbol == "n" || symbol == "N") { elm = n; } else if (symbol == "o" || symbol == "O") { elm = o; } else if (symbol == "F" || symbol == "f") { elm = f; } else if (symbol == "ne" || symbol == "Ne" || symbol == "NE" || symbol == "nE") { elm = ne; } else if (symbol == "na" || symbol == "NA" || symbol == "Na" || symbol == "nA") { elm = na; } else if (symbol == "mg" || symbol == "Mg" || symbol == "MG" || symbol == "mG") { elm = mg; } else if (symbol == "al" || symbol == "Al" || symbol == "AL" || symbol == "aL") { elm = al; } else if (symbol == "si" || symbol == "Si" || symbol == "SI" || symbol == "sI") { elm = si; } Clear(); Describe(elm); WriteLine("================================="); return 0; } } }
Chemistry - Periodic Table ------------------------------------------------ Type the atomic symbol of an element you want to review (H to Si):
Chemistry - Periodic Table ------------------------------------------------ Type the atomic symbol of an element you want to review (H to Si): Na
Chemistry - Sodium ------------------------ Symbol: Na Element Name: Sodium Atomic Number: 11 Atomic Weight: 22.98976928 ================================= Press any key to continue . . .
using static System.Console; namespace Chemistry01 { public class Chemistry { private static void Describe(Element e) { WriteLine("Chemistry - " + e.ElementName); WriteLine("------------------------"); WriteLine("Symbol: " + e.Symbol); WriteLine("Element Name: " + e.ElementName); WriteLine("Atomic Number: " + e.AtomicNumber); WriteLine("Atomic Weight: " + e.AtomicWeight); } public static int Main() { Element elm = null; Element h = new Element(1, "H", "Hydrogen", 1.008); Element he = new Element(2, "He", "Helium", 4.002602); Element li = new Element(3, "Li", "Lithium", 6.94); Element be = new Element(4, "Be", "Beryllium", 9.0121831); Element b = new Element(5, "B", "Boron", 10.81); Element c = new Element(name: "Carbon", mass: 12.011, symbol: "C", number: 6); Element n = new Element(number: 7, symbol: "N", name: "Nitrogen", mass: 14.007); Element o = new Element(number: 8, symbol: "O", name: "Oxygen", mass: 15.999); Element f = new Element(number: 9, symbol: "F", name: "Fluorine", mass: 15.999); Element ne = new Element(number: 10, symbol: "Ne", name: "Neon", mass: 20.1797); Element na = new Element(11, "Na", "Sodium", mass: 22.98976928); Element mg = new Element(12, "Mg", "Magnesium", 24.305); Element al = new Element(13, "Al", "Aluminium", 26.9815385); Element si = new Element() { ElementName = "Silicon", AtomicWeight = 28.085, Symbol = "Si", AtomicNumber = 14 }; Element p = new Element() { ElementName = "Phosphorus", AtomicWeight = 30.973761998, Symbol = "P", AtomicNumber = 15 }; WriteLine("Chemistry - Periodic Table"); WriteLine("------------------------------------------------"); Write("Type the atomic symbol of an element you want to review (H to P): "); string symbol = ReadLine(); if( (symbol == "h") || (symbol == "H") ) { elm = h; } else if( (symbol == "he") || (symbol == "He") || (symbol == "HE") || (symbol == "hE") ) { elm = he; } else if( (symbol == "li") || (symbol == "Li") || (symbol == "LI") || (symbol == "lI") ) { elm = li; } else if( (symbol == "be") || (symbol == "Be") || (symbol == "BE") || (symbol == "bE") ) { elm = be; } else if( (symbol == "b") || (symbol == "B") ) { elm = b; } else if( (symbol == "c") || (symbol == "C") ) { elm = c; } else if( (symbol == "n") || (symbol == "N") ) { elm = n; } else if( (symbol == "o") || (symbol == "O") ) { elm = o; } else if( (symbol == "F") || (symbol == "f") ) { elm = f; } else if( (symbol == "ne") || (symbol == "Ne") || (symbol == "NE") || (symbol == "nE") ) { elm = ne; } else if( (symbol == "na") || (symbol == "NA") || (symbol == "Na") || (symbol == "nA") ) { elm = na; } else if( (symbol == "mg") || (symbol == "Mg") || (symbol == "MG") || (symbol == "mG") ) { elm = mg; } else if( (symbol == "al") || (symbol == "Al") || (symbol == "AL") || (symbol == "aL") ) { elm = al; } else if( (symbol == "si") || (symbol == "Si") || (symbol == "SI") || (symbol == "sI") ) { elm = si; } Clear(); Describe(elm); WriteLine("================================="); return 0; } } }
Chemistry - Periodic Table ------------------------------------------------ Type the atomic symbol of an element you want to review (H to Si):
Chemistry - Periodic Table ------------------------------------------------ Type the atomic symbol of an element you want to review (H to Si): Na
Chemistry - Magnesium ------------------------ Symbol: Mg Element Name: Magnesium Atomic Number: 12 Atomic Weight: 24.305 ================================= Press any key to continue . . .
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 |
|