 # Beginning C#: Boolean Logic

by Peter Shaw Learn the logic behind making Boolean choices.

In our last installment, I showed you how to use a basic IF/Then construct to make decisions in C#. This time, we're going to take that one step further and take a look at Boolean logic, helping you to make some very advanced choices in your code.

## Boolean What?

To many, Boolean logic is quite a foreign concept, but to a computer it's truly fundamental to the way they work.

In a previous post, I briefly mentioned about bit size, and how it affects the precision of an integer. If you recall, bits relate to electrical lines inside the computer.

Boolean logic is an extension to this idea, and deals with what happens when bits in the same position are combined with other values. The science behind this process (and the governance of Boolean logic) is known as Boolean arithmetic.

Just as with normal arithmetic, there are rules governing how addition and subtraction are performed, but more importantly, there also are rules that define the result of the combinations mentioned above. Understanding these combinations are the key to understanding how to combine Boolean logic when making advanced decisions.

### The Rules…

There are five main rules when it comes to Boolean logic. These are:

• AND
• OR
• NAND (Not And)
• NOR (Not Or)
• NOT

There are others, but for the purposes of this article, the base five are all we need to know.

The best way to describe these rules is as a process of two inputs (Usually A and B) and one output. Starting with AND, we describe this as follows:

AND: Output equals one where both inputs also equal one; otherwise, it equals 0.

A better way to understand this is usually by way of a table, something like the following:

 A B Output 0 0 0 0 1 0 1 0 0 1 1 1

When applied to an IF/Then statement, it can easily be seen that condition A must be truthy AND condition B must also be truthy before the entire IF/Then check can be considered truthy.

Moving on, OR is described with the following rule:

OR: Output equals one where either input is equal to one; otherwise, it will equal zero.

In the form of a table:

 A B Output 0 0 0 0 1 1 1 0 1 1 1 1

When applied to an IF/Then statement, it can be seen that as long as any of the inputs are truthy, the output will also be truthy.

Next is the rule for NOT:

NOT: Output always equals the opposite of the input.

NOT is different from the others, in that it only has one input; the output is simply the opposite of the input.

 A Output 0 1 1 0

In terms of an IF/Then, a NOT switches the meaning of a truthy test; more on this shortly.

The remaining two rules, NAND and NOR, are simply AND/OR but inverted using a NOT, meaning NOT AND and NOT OR.

The tables are as follows:

NAND

 A B Output 0 0 1 0 1 0 1 0 0 1 1 0

NOR

 A B Output 0 0 1 0 1 1 1 0 1 1 1 0

Now that we have the theory out of the way, let's put things into practice.

AND, OR, and NOT all have their own representative symbols in C#, as follows:

• &&     AND
• ||     OR
• !     NOT

Using them in an IF/Then clause means you now can add multiple inputs into the decision logic; for example:

```using System;

namespace dataTester
{
class Program
{
static void Main()
{

string Name = "Peter";
int Age = 21;

if (Name == "Peter" && Age == 21)
{
Console.WriteLine("Hello peter who is 21");
}
else
{
Console.WriteLine("You're not peter who is 21");
}

}

}
}
```

In this case, the decision is exactly as it reads, 'Name = "Peter"' is input A and 'Age = 21' is input B. Both conditions MUST resolve to a truthy result for output (in this case, the code inside the IF statement) to also be true.

You don't have to stop at two inputs, either. You can add as many as you like; for example:

```using System;

namespace dataTester
{
class Program
{
static void Main()
{

string Name = "Peter";
int Age = 21;
string Occupation = "developer";
int someOtherValue = 30;

if (Name == "Peter" && Age == 21 &&
Occupation == "developer" &&
someOtherValue == 30)
{
Console.WriteLine("Hello peter who is 21");
}
else
{
Console.WriteLine("You're not peter who is 21");
}

}

}
}
```

Here we have four conditions, and ALL four of them must be truthy before the IF statement is considered truthy.

OR is used in exactly the same way:

```using System;

namespace dataTester
{
class Program
{
static void Main()
{

int Age = 21;
string Occupation = "developer";
int someOtherValue = 30;

if (Name == "Peter" || Age == 21)
{
Console.WriteLine("Hello peter who is 21");
}
else
{
Console.WriteLine("You're not peter who is 21");
}

}

}
}
```

Just like the Boolean rule, for this to be truthy, name has to equal "Peter" or Age has to be equal to 21. As long as one of the inputs are truthy, the output will be truthy.

Things can get even more creative when you start to combine things.

```using System;

namespace dataTester
{
class Program
{
static void Main()
{

string Name = "Peter";
int Age = 21;
string Occupation = "developer";
int someOtherValue = 30;

if (Name == "Peter" && Age == 21
|| Occupation == "developer")
{
Console.WriteLine("Hello peter who is 21");
}
else
{
Console.WriteLine("You're not peter who is 21");
}

}

}
}
```

Reading through this, you might think the following:

Name must be equal to "Peter" AND Age must be equal to 21, OR Occupation must be equal to "developer."

Surprisingly enough, however, it won't work as you expect. In many cases, you need to use brackets to silo parts of the decision, so that they are resolved before others.  In the previous example, re-writing it as follows:

```using System;

namespace dataTester
{
class Program
{
static void Main()
{

string Name = "Peter"
int Age = 21;
string Occupation = "developer";
int someOtherValue = 30;

if ((Name == "Peter" && Age == 21)
|| Occupation == "developer")
{
Console.WriteLine("Hello peter who is 21");
}

else
{
Console.WriteLine("You're not peter who is 21");
}

}

}
}
```

Will work as expected.

Note that the difference is that we've siloed the "AND" decision, so that this now becomes two conditions, one on either side of the OR decision. Very often, we need to do this to help our PCs understand our actual intention.

Using NOT is very simple. Once you understand this, allowing you to do, for example:

```using System;

namespace dataTester
{
class Program
{
static void Main()
{

string Name = "Peter";
int Age = 21;
string Occupation = "developer";
int someOtherValue = 30;

if (!(Name == "Peter" && Age == 21))
{
Console.WriteLine("You're not peter who is 21");
}
else
{
Console.WriteLine("Hello peter who is 21");
}

}

}
}
```

As you can see, we've siloed the AND condition, but we've then prefixed this with a NOT condition, making this now read:

If it's not true that Name is equal to "peter" and age is equal to peter, then the output is true.

In English terms, we're saying if Name is not equal to peter and age is not equal to 21, we accept that the output is true.

Essentially, by combining the NOT operation with the AND operation, we've now created our NAND (Not And) and, similarly, we also can use the same process to create a NOR.

In this post, you've seen how we can take our decision logic to the next level, and start to make our C# applications perform some very powerful choices. In the next post, we'll step away from IF into other control structures, and learn how to loop over things.