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 the code of a condition involves many lines of code, you can delimite its body 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)

If a section of code is long, you can include its child code in curly brackets:

if( condition1 )
{    
    if( condition2 )
    {    
        if( condition3 )
		{
            statement(s)
        }
    }
}

Practical LearningPractical Learning: Introducing Boolean Conjunctions

  1. Start Microsoft Visual Studio. On the Visual Studio 2019 dialog box, click Create a New Project
  2. In the list of projects templates, click Windows Forms App (.NET Framework) (if Microsoft Visual Studio was already running, on the main menu, in the list of projects templates, click Windows Forms App (.NET Framework)
  3. Click Next
  4. Change the project Name to StockBrokerCompany1
  5. Press Enter
  6. In the Solution Explorer, right-click Form1 -> Rename
  7. Type StocksProcessing (to get StocksProcessing.cs) and press Enter
  8. Read the message box and click Yes
  9. Design the form as follows:

    Introduction to Logical Operations

    Control (Name) Text TextAlign
    Label   Number of Shares:  
    TextBox txtNumberOfShares 0 Right
    Label   Price Per Share:
    TextBox txtPricePerShare 0.00 Right
    Button btnCalculate Calculate  
    Label   Principal
    TextBox txtPrincipal Right
    Label   Commission
    TextBox txtCommission Right
    Label   Total Investment:
    TextBox txtTotalInvestment Right
  10. Double-click the Calculate button

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:

The conjunction operation can be resumed as follows:

Condition 1 Contition 2 Condition 1 AND Condition 2
False False False
False True False
True False False
True True True

Practical LearningPractical Learning: Introducing Boolean Conjunctions

  1. Implement the event as follows:
    using System;
    using System.Windows.Forms;
    
    namespace StockBrokerCompany1
    {
        public partial class StocksProcessing : Form
        {
            public StocksProcessing()
            {
                InitializeComponent();
            }
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                double commission     = 0.00;
                int    numberOfShares = int.Parse(txtNumberOfShares.Text);
                double pricePerShare  = double.Parse(txtPricePerShare.Text);
    
    
                double principal = numberOfShares * pricePerShare;
    
                if (principal >= 0 && principal <= 2500)
                    commission = 26.25 + (principal * 0.0014);
    
                if (principal > 2500 && principal <= 6000)
                    commission = 45 + (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);
    
                txtPrincipal.Text = principal.ToString("F");
                txtCommission.Text = commission.ToString("F");
                txtTotalInvestment.Text = (principal + commission).ToString("F");
            }
        }
    }
    
  2. To execute the application, on the main menu,, click Debug -> Start Without Debugging

    Introducing Boolean Conjunctions

  3. In the Number of Shares text box, type a natural number such as 64788
  4. In the Price Per Share text box, type a number such as 36.85

    Introducing Boolean Conjunctions

  5. Click the Calculate button:

    Introducing Boolean Conjunctions

  6. Close the form and return to your programming environment
  7. To make your code easier to read, it is a good idea to include each Boolean operation in its own parentheses.
    For a few examples, access the StocksProcessing.cshtml file and change its document as follows:
    using System;
    using System.Windows.Forms;
    
    namespace StockBrokerCompany1
    {
        public partial class StocksProcessing : Form
        {
            public StocksProcessing()
            {
                InitializeComponent();
            }
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                double commission     = 0.00;
                int    numberOfShares = int.Parse(txtNumberOfShares.Text);
                double pricePerShare  = double.Parse(txtPricePerShare.Text);
    
    
                double principal = numberOfShares * pricePerShare;
    
                if( (principal >= 0) && (principal <= 2500) )
                    commission = 26.25 + (principal * 0.0014);
    
                if( (principal > 2500) && (principal <= 6000) )
                    commission = 45 + (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);
    
                txtPrincipal.Text = principal.ToString("F");
                txtCommission.Text = commission.ToString("F");
                txtTotalInvestment.Text = (principal + commission).ToString("F");
            }
        }
    }
  8. To execute the project, on the main menu, click Debug -> Start Without Debugging
  9. Enter some values in the text boxes above the button
  10. Click the Calculate button
  11. After testing the webpage, close the form and return to your programming environment
  12. On the main menu of Microsoft Visual Studio, click File -> New -> Project...
  13. In the list of projects templates, click Windows Forms App (.NET Framework)
  14. Click Next
  15. Change the project Name to Chemistry06
  16. Click Create
  17. On the main menu, click Project -> Add Class...
  18. Replace the name with Element
  19. Click Add
  20. Change the code as follows:
    namespace Chemistry06
    {
        public enum Phase
        {
            Gas,
            Liquid,
            Solid,
            Unknown
        }
    
        public class Element
        {
            public string Symbol       { get; set; }
            public string ElementName  { get; set; }
            public int AtomicNumber    { get; set; }
            public double AtomicWeight { get; set; }
            public Phase Phase         { get; set; }
    
            public Element()
            {
            }
    
            public Element(int number)
            {
                AtomicNumber = number;
            }
    
            public Element(string symbol)
            {
                Symbol = symbol;
            }
    
            public Element(int number, string symbol, string name, double mass, Phase phase)
            {
                ElementName  = name;
                AtomicWeight = mass;
                Phase        = phase;
                Symbol       = symbol;
                AtomicNumber = number;
            }
        }
    }
  21. In the Solution Explorer, right-click Form1.cs -> Rename
  22. Type Chemistry (to get Chemistry.cs) and press Enter
  23. Read the message box and press Enter three times
  24. Change the design of the form as follows:

    Introduction to Logical Operations

    Control (Name) Text
    Label   Element Symbol:
    TextBox txtElementSymbol
    Button btnFind Find
    GroupBox   Element Detais
    Label   Symbol:
    TextBox txtSymbol
    Label   Atomic Number:
    TextBox txtAtomicNumber
    Label   Element Name:
    TextBox txtElementName
    Label   Atomic Weight:
    TextBox txtAtomicWeight
    Label   Phase:
    TextBox txtPhase
  25. Double-click the Find button
  26. Right-click inside the Code Editor and click Remove and Sort Usings

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

Introduction

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:

The operation can be resumed as follows:

Condition 1 Contition 2 Condition 1 AND Condition 2 Condition 1 OR Condition 2
False False False False
False True False True
True False False True
True True True True

Practical LearningPractical Learning: Introducing Logical Disjunctions

  1. Implement the event as follows:
    using System;
    using System.Windows.Forms;
    
    namespace Chemistry06
    {
        public partial class Chemistry : Form
        {
            public Chemistry()
            {
                InitializeComponent();
            }
    
            private void btnFind_Click(object sender, EventArgs e)
            {
                Element h = new Element(1, "H", "Hydrogen", 1.008, Phase.Gas);
                Element b = new Element(5, "B", "Boron", 10.81, Phase.Solid);
                Element c = new Element(name: "Carbon", mass: 12.011, symbol: "C", number: 6, phase: Phase.Solid);
                Element n = new Element(7);
                n.Symbol = "N";
                n.AtomicWeight = 14.007;
                n.ElementName = "Nitrogen";
                n.Phase = Phase.Gas;
                Element o = new Element("O")
                {
                    AtomicNumber = 8,
                    ElementName = "Oxygen",
                    AtomicWeight = 15.999,
                    Phase = Phase.Gas
                };
                Element f = new Element("F")
                {
                    AtomicNumber = 9,
                    ElementName = "Fluorine",
                    AtomicWeight = 15.999,
                    Phase = Phase.Gas
                };
                Element p = new Element()
                {
                    ElementName  = "Phosphorus",
                    AtomicWeight = 30.973761998,
                    Symbol       = "P",
                    AtomicNumber = 15,
                    Phase        = Phase.Solid
                };
                Element s = new Element(16, "S", "Sulfur", 32.06, Phase.Solid);
                Element k  = new Element() { ElementName = "Potassium", Symbol = "K", Phase = Phase.Solid, AtomicWeight = 39.0983, AtomicNumber = 19 };
    
                Element selected;
                string symbol = txtElementSymbol.Text;
                
                if     ( (symbol == "h")  || (symbol == "H") ) { selected = h; }
                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 == "P")  || (symbol == "p") ) { selected = p; }
                else if( (symbol == "S")  || (symbol == "s") ) { selected = s; }
                else if( (symbol == "k")  || (symbol == "K") ) { selected = k; }
    
                txtSymbol.Text = selected.Symbol;
                txtAtomicNumber.Text = selected.AtomicNumber.ToString();
                txtElementName.Text = selected.ElementName;
                txtAtomicWeight.Text = selected.AtomicWeight.ToString();
                txtPhase.Text = selected.Phase.ToString();
            }
        }
    }
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging:

    Introduction to Multiple Conditions

  3. In the top text box, type a chemical symbol that uses one letter such as B, O, C, F, K, H, N, P, or S:

    Introduction to Conditional Disjunctions

  4. Click the Find button:

    Introduction to Boolean Disjunctions

  5. Close the form and return to your programming environment

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 LearningPractical Learning: Introducing Logical Disjunctions

  1. Change the code as follows:
    using System;
    using System.Windows.Forms;
    
    namespace Chemistry06
    {
        public partial class Chemistry : Form
        {
            public Chemistry()
            {
                InitializeComponent();
            }
    
            private void btnFind_Click(object sender, EventArgs e)
            {
                Element h = new Element(1, "H", "Hydrogen", 1.008, Phase.Gas);
                Element he = new Element(2, "He", "Helium", 4.002602, Phase.Gas);
                Element li = new Element(3, "Li", "Lithium", 6.94, Phase.Solid);
                Element be = new Element(4, "Be", "Beryllium", 9.0121831, Phase.Solid);
                Element b = new Element(5, "B", "Boron", 10.81, Phase.Solid);
                Element c = new Element(name: "Carbon", mass: 12.011, symbol: "C", number: 6, phase: Phase.Solid);
                Element n = new Element(7);
                n.Symbol = "N";
                n.AtomicWeight = 14.007;
                n.ElementName = "Nitrogen";
                n.Phase = Phase.Gas;
                Element o = new Element("O")
                {
                    AtomicNumber = 8,
                    ElementName = "Oxygen",
                    AtomicWeight = 15.999,
                    Phase = Phase.Gas
                };
                Element f = new Element("F")
                {
                    AtomicNumber = 9,
                    ElementName = "Fluorine",
                    AtomicWeight = 15.999,
                    Phase = Phase.Gas
                };
                Element ne = new Element("Ne")
                {
                    AtomicNumber = 10,
                    ElementName = "Neon",
                    AtomicWeight = 20.1797,
                    Phase = Phase.Gas
                };
                Element na = new Element(11, "Na", "Sodium", 22.98976928, Phase.Solid);
                Element mg = new Element(12, "Mg", "Magnesium", 24.305, Phase.Solid);
                Element al = new Element(13, "Al", "Aluminium", 26.9815385, Phase.Solid);
                Element si = new Element() { ElementName = "Silicon", AtomicWeight = 28.085, Symbol = "Si", AtomicNumber = 14, Phase = Phase.Solid };
                Element p = new Element()
                {
                    ElementName  = "Phosphorus",
                    AtomicWeight = 30.973761998,
                    Symbol       = "P",
                    AtomicNumber = 15,
                    Phase        = Phase.Solid
                };
                Element s = new Element(16, "S", "Sulfur", 32.06, Phase.Solid);
                Element cl = new Element() { Phase = Phase.Gas, AtomicWeight = 35.446, ElementName = "Chlorine", AtomicNumber = 17, Symbol = "Cl" };
                Element ar = new Element() { AtomicWeight = 39.792, Symbol = "Ar", Phase = Phase.Gas, ElementName = "Argon", AtomicNumber = 18 };
                Element k  = new Element() { ElementName = "Potassium", AtomicWeight = 39.0983, Symbol = "K", Phase = Phase.Solid, AtomicNumber = 19 };
    
                Element selected = new Element();
                string symbol = txtElementSymbol.Text;
                
                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; }
                else if (symbol == "S"  || symbol == "s") { selected = s; }
                else if (symbol == "CL" || symbol == "Cl" || symbol == "cL" || symbol == "cl") { selected = cl; }
                else if (symbol == "aR" || symbol == "AR" || symbol == "ar" || symbol == "Ar") { selected = ar; }
                else if (symbol == "k"  || symbol == "K") { selected = k;  }
    
                txtSymbol.Text = selected.Symbol;
                txtAtomicNumber.Text = selected.AtomicNumber.ToString();
                txtElementName.Text = selected.ElementName;
                txtAtomicWeight.Text = selected.AtomicWeight.ToString();
                txtPhase.Text = selected.Phase.ToString();
            }
        }
    }
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging:
  3. In the top text box, type a chemical symbol such as Al, K, B, Be, C, Ar, F, Fl, H, Cl, He, Li, Mg, N, Na, Ne, Si, P, or S

    Introduction to Boolean Disjunctions

  4. Click the Find button:

    Introduction to Boolean Disjunctions

  5. Close the form and return to your programming environment
  6. As mentioned for the conjunction, it is a good idea to include each Boolean operation in its parentheses.
    Access the Chemistry.cshtml file and change the code as follows:
    using System;
    using System.Windows.Forms;
    
    namespace Chemistry06
    {
        public partial class Chemistry : Form
        {
            public Chemistry()
            {
                InitializeComponent();
            }
    
            private void btnFind_Click(object sender, EventArgs e)
            {
                Element h = new Element(1, "H", "Hydrogen", 1.008, Phase.Gas);
                Element he = new Element(2, "He", "Helium", 4.002602, Phase.Gas);
                Element li = new Element(3, "Li", "Lithium", 6.94, Phase.Solid);
                Element be = new Element(4, "Be", "Beryllium", 9.0121831, Phase.Solid);
                Element b = new Element(5, "B", "Boron", 10.81, Phase.Solid);
                Element c = new Element(name: "Carbon", mass: 12.011, symbol: "C", number: 6, phase: Phase.Solid);
                Element n = new Element(7);
                n.Symbol = "N";
                n.AtomicWeight = 14.007;
                n.ElementName = "Nitrogen";
                n.Phase = Phase.Gas;
                Element o = new Element("O")
                {
                    AtomicNumber = 8,
                    ElementName = "Oxygen",
                    AtomicWeight = 15.999,
                    Phase = Phase.Gas
                };
                Element f = new Element("F")
                {
                    AtomicNumber = 9,
                    ElementName = "Fluorine",
                    AtomicWeight = 15.999,
                    Phase = Phase.Gas
                };
                Element ne = new Element("Ne")
                {
                    AtomicNumber = 10,
                    ElementName = "Neon",
                    AtomicWeight = 20.1797,
                    Phase = Phase.Gas
                };
                Element na = new Element(11, "Na", "Sodium", 22.98976928, Phase.Solid);
                Element mg = new Element(12, "Mg", "Magnesium", 24.305, Phase.Solid);
                Element al = new Element(13, "Al", "Aluminium", 26.9815385, Phase.Solid);
                Element si = new Element() { ElementName = "Silicon", AtomicWeight = 28.085, Symbol = "Si", AtomicNumber = 14, Phase = Phase.Solid };
                Element p = new Element()
                {
                    ElementName  = "Phosphorus",
                    AtomicWeight = 30.973761998,
                    Symbol       = "P",
                    AtomicNumber = 15,
                    Phase        = Phase.Solid
                };
                Element s = new Element(16, "S", "Sulfur", 32.06, Phase.Solid);
                Element cl = new Element() { ElementName = "Chlorine", AtomicWeight = 35.446, Symbol = "Cl", AtomicNumber = 17, Phase = Phase.Gas };
                Element ar = new Element() { AtomicWeight = 39.792, Symbol = "Ar", Phase = Phase.Gas, ElementName = "Argon", AtomicNumber = 18 };
                Element k  = new Element() { ElementName = "Potassium", Phase = Phase.Solid, AtomicNumber = 19, AtomicWeight = 39.0983, Symbol = "K" };
                Element ca = new Element() { Symbol = "Ca", Phase = Phase.Solid, AtomicNumber = 20, ElementName = "Calcium", AtomicWeight = 40.078 };
    
                Element selected = new Element();
                string symbol = txtElementSymbol.Text;
                
                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;  }
                else if( (symbol == "S")  || (symbol == "s") )                                          { selected = s;  }
                else if( (symbol == "CL") || (symbol == "Cl") || (symbol == "cL") || (symbol == "cl") ) { selected = cl; }
                else if( (symbol == "aR") || (symbol == "AR") || (symbol == "ar") || (symbol == "Ar") ) { selected = ar; }
                else if( (symbol == "k")  || (symbol == "K") )                                          { selected = k;  }
                else if ((symbol == "Ca") || (symbol == "cA") || (symbol == "CA") || (symbol == "ca")) { selected = ca; }
    
                txtSymbol.Text = selected.Symbol;
                txtAtomicNumber.Text = selected.AtomicNumber.ToString();
                txtElementName.Text = selected.ElementName;
                txtAtomicWeight.Text = selected.AtomicWeight.ToString();
                txtPhase.Text = selected.Phase.ToString();
            }
        }
    }
  7. To execute the project, on the main menu, click Debug -> Start Without Debugging
  8. In the top text box, type a chemical symbol such as S, Al, B, O, Be, C, F, Ca, Fl, H, Ar, He, Li, Mg, N, P, Na, K, Ne, Si, or Cl

    Introduction to Boolean Disjunctions

  9. Click the Find button:

    Introduction to Boolean Disjunctions

  10. Close the form and return to your programming environment

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 LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2021, FunctionX Next