# Introduction to C programming/Lectures/BooleanLogic

## What is Boolean logic?

Boolean logic is a method for telling if an expression is true or false. Computers can use boolean logic to make decisions- it can do one thing if an expression is true, and another if its false. This is the only way that a computer can make a decision- any time the computer makes a choice, it does so off complex boolean logic expressions. Along with variables, boolean logic is the basis of programming.

### A world of black and white

As I said, a program can do one thing if an expression is true, and another if its false. What happens if an expression is neither? Nothing- its not possible. The world of Boolean logic is black and white- something is either true or false. There is no maybe. This is the biggest transition a programmer needs to make when thinking in boolean logic. Erase the words "maybe" and "not sure" from your vocabulary now, they no longer exist.

## Comparisons

If boolean logic can tell if a statement is true, what exactly is an "expression"? The only type of expression a computer can understand is a comparison. A computer can compare two numbers, and see if the first number is equal to, less than, or greater than the second number. The comparisons that are usable in C are as follows:

• x==y is true if x and y are equal*
• x!=y is true if x and y are not equal
• x<y is true if x is less than y
• x<=y is true if x is less than or equal to y
• x>y is true if x is greater than y
• x>=y is true if x is greater than or equal to y

* Note the difference between this and the single equals sign ( = ). The single equals is used for assignment and will not give you the desired results.

## Combining expressions

Frequently, you won't want to just know if two values are equal- you want to know if these two values are equal and if another two are equal. Or if another two are not equal. Or some other combination. Well what do you know- you can do that! There are 3 operators you can use to combine expressions. Any combination of truth values you want to test for can be made by a combination of these operators.

### AND

The AND operator works just like you'd think it does- it takes two expressions and checks if both of them are true. If they are, the AND of the two is true. If not, the AND is false. In C, an AND can be written as two ampersands (&&) and looks like this:

```expression1 && expression2
```

### OR

The OR operator is a little bit trickier. It becomes true if either of the expressions is true, or if both of them are true. Its only false if both of the expressions are false. This is a bit different from english, where or usually means one of the two, not both. In C, an OR is represented by two pipe symbols (||) and looks like this:

```expression1 || expression2
```

### NOT

NOT is the only operator that works on one expression instead of two. Its an inverse operator- it returns the opposite of the expression. If the expression is true it returns false, if its false it returns true. In C, a NOT can be written like this:

```!expression1
```

Notice that the ! is also in the not equal comparison (!=). Thats an easy way to remember it.

### Order of Operations

Its perfectly legal to chain as many ANDs, ORs, and NOTs as you want. For example, !(expression1 && (expression2 || expression3)) is perfectly legal. When you do this, the different parts of the chain are evaluated according to an order of operations, just like in math. The order of operation is NOT, AND, OR. Ties are done left to right. Also just like in math, you can use parenthesis to change the default order. For example,

```a && b || c
```

This is true if a and b are true, or if c is true. If we change it to

```a && (b || c)
```

This one is true if a and b are true, or if a and c are true. Notice the difference- in the first one, if a was false and c was true, the total value was true. Now it would be false. Be very careful of bugs like this. When in doubt, throw parenthesis everywhere.

## Variables and comparisons

You don't have to compare only numbers. You can also compare either a variable and a number (for example- x<7), or two variables (for example, x>y). When you compare a variable to something, what you really do is compare the value of the variable to either a number or another variable's value.

## If statements

Well thats fun, but how does C allow you to make a decision? You use an if statement. An if statement allows a program to run a series of lines if and only if a condition is true. An if statement looks like this:

```if(condition){
statement 1;
statement 2;
}
```

where condition is a boolean expression of some sort.

Anything inside the {} block will only happen if condition is true. If its false, they aren't executed. You can have as many statements in the block as you want.

### Else clauses

Sometimes you don't want to just do soemthing if a condition is true. Sometimes you want to do one thing if its true, and another if its false. An else statement looks like this:

```if(condition){
statement 1;
}
else{
statement 2;
}
```

Anything inside the first {} happens if the condition is true, and the code inside the second block happens if it is false.

### Else if statements

Occasionally, you need something a little more complex than 2 options. In that case, you need an else if.

```if(condition 1){
statement 1;
}
else if(condition 2){
statement 2;
}
else if(condition 3){
statement 3;
}
else{
statment 4;
}
```

If condition 1 is true, statement 1 is executed and none of the other statements are. If condition 1 is false and condition 2 is true, statement 2 is run. If conditions 1 and 2 are both false and condition 3 is true, statement 3 is run. If all 3 conditions are false, statement 4 is run. You can have as many else if parts as you want. And you don't need to have an else at the very end if you don't want to.

### Switch statements

Switch statements are a quick way of writing large else if statements where you're checking the value of a single variable. For example, if you want to do different things depending if a variable is 1, 2, 3, 4, or 5, a switch statement can be used. A switch statement works like this:

```switch(variable){
case value1:
statement1;
break;
case value2:
statement2;
break;
case value3:
statement3;
break;
}
```

If the value of variable is value1, statement 1 is executed. If its value 2, statement 2 is. And so on.

Notice the word break; at the end of each case. This is actually important- if you don't put it in, you'll execute the next case as well. This can actually be useful- if you want to do the same thing for 2 values, you can go:

```case value1:
case value2:
statement1;
break;
```

Here, if the variable is value1 or value 2, you'll execute statment 1. Be careful of your breaks, leaving one out by mistake is an easy bug to make. Even experienced pros make this one every once in a while.

## Boolean type

There's actually a boolean type in C. You can declare a variable of type bool, who's only legal values are TRUE and FALSE. You can use this to store true/false info in a program.

### The value of true and false

So, since everything in computers is a number, what values do true and false have? False is always 0, true is anything else. You can actually use an integer as a condition in an if statement. If the value is not zero, the if would be true, if its 0 the if is false. You'll see a lot of code, especially older code, using this feature.