Programming often uses Comparison Tests and Logical Tests to tell the computer what to do. This FAQ is a short guide on how these work and why they are useful.

You can find more information by looking through programming textbooks, or having a look on http://mathworld.wolfram.com/ but often these explanations are very in-depth and sometimes hard to understand. I encourage you to try to find some more information through other sources after reading through this.

**COMPARISON TESTS**

Often when programming you'll need to know things about a variable, whether this variable is greater or smaller than another variable, if it is equal to a certain number, etc. This can be achieved with the Comparison Tests.

Let's say we have three variables, a, b and c.

a is equal to 5

b is equal to 10

c is equal to 10

I'll use these variables in each of the conditional problems.

**== Equal to**

This test does not say that one variable is equal to another. It instead checks whether a variable is exactly equal to another variable or number and then gives you the answer.

a == b

As 5 does not equal 10 this would return a value of FALSE.

**!= Not Equal to**

This test checks whether a variable is NOT exactly equal to another variable or number. Every time you see an exclamation mark (sometimes called a BANG in programming) you are checking the opposite claim. == checks equal to, != checks not equal to.

a != b

As 5 does NOT equal 10 this would return a value of TRUE.

**> Greater Than**

This checks whether a variable is larger than another variable or number and if so it returns a value of TRUE.

a > b

a is a smaller number than b so this would return FALSE.

**< Less Than**

Opposite to greater than. This checks whether a variable is smaller than another variable or number and if so it returns a value of TRUE.

a < b

a is a smaller number than b so this would return TRUE.

**>= Greater Than or Equal To**

This test is very similar to the Greater Than test but also allows the variables to be equal.

b >= c

b is equal to c so this would return a value of TRUE.

HOWEVER

b > c

b is equal to c and this test only checks if a variable is larger, so this would return a value of FALSE.

**<= Less Than or Equal To**

Opposite to greater than or equal to. This checks whether a variable is equal to or smaller than another variable or number and if so it returns a value of TRUE.

b <= c

b is equal to c so this would return a value of TRUE.

HOWEVER

b < c

b is equal to c and this test only checks if a variable is smaller, so this would return a value of FALSE.

**LOGICAL TESTS**

In programming, you often want to check whether two or more statements are correct. The conditional tests will give you the answer, and logical tests can string these tests together.

In these examples, think of TRUE and FALSE as the result of one of the conditional tests above. For instance, we have already tested a < b and found that it is TRUE, so instead of writing out a < b, we've used the statement TRUE.

There are three main logical tests:

**! NOT**

This returns the opposite value for TRUE or FALSE. We use the operator ! to represent the NOT function.

!TRUE checks the value of TRUE, and returns the opposite value.

The opposite of TRUE is FALSE so this returns FALSE.

!FALSE checks the value of FALSE, and returns the opposite value.

The opposite of FALSE is TRUE so this returns TRUE.

**&& AND**

This returns a TRUE value if ALL values tested are true. We use the operator && to represent the AND function.

TRUE && TRUE

Both of these values are TRUE so it will return a value of TRUE.

TRUE && FALSE

In this case not both values are TRUE and so it will return a value of FALSE.

TRUE && !TRUE

Here we have a value of TRUE, but we also have a value of NOT TRUE. This turns the statement into;

TRUE && FALSE which returns a value of FALSE.

**|| OR**

This returns a TRUE value if ANY values are true. We use the operator || (above the enter key on a US layout keyboard, sometimes called a PIPE in programming) to represent the OR function.

TRUE || TRUE

Both of these value are TRUE, so it will return a value of TRUE.

TRUE || FALSE

Unlike the AND test, the OR test only cares if at least ONE of the statements is TRUE. There is ONE TRUE so this will return a value of TRUE.

FALSE || FALSE

There are NO TRUE statements here, so this will return a value of FALSE.

**NOT, AND, OR - Stringing Them Together**

Often in programming we want to know the answer to multiple tests. For instance, we may want to know if a is equal to 5, greater than b and also less than c. In these situations you can string your AND, NOT, OR statements together to find a final answer based on multiple criteria.

Like mathematics, Logical Tests follow an order of operations.

NOT (Think of a NOT statement as parenthesis, they always come first)

AND (Think of an AND statement as multiplication, they come after NOT but before OR)

OR (Think of an OR statement as addition, they come last, after NOT, AND)

This is best done by looking at each part in isolation and then stringing them together. These can be very easy, or very (very) hard.

**TRUE || TRUE && FALSE**

Firstly, Break this statement into its parts, and do each part in the order of operations.

This statement has an AND test and an OR test. AND comes before OR so do the AND first.

TRUE && FALSE returns FALSE as not all of the statements are TRUE.

Then put this result into the OR test.

TRUE || FALSE

This returns TRUE because the OR test only needs one value to be TRUE.

So by breaking it down we see that

TRUE || TRUE && FALSE returns TRUE.

**(a > b) || (a < c) && (a > (b/c)) && (b == c)**

This statement might look a little difficult, but it becomes much easier if we break it down. First step is to evaluate all the conditional tests, and then we evaluate the logical problems making sure we follow the order of operations.

(a > b) returns FALSE.

(a < c) returns TRUE.

(a > (b/c)) here we have some basic math included. (b/c) is 1, and a is larger than 1. So this returns TRUE.

(b == c) returns TRUE.

So now if we string this back together with the Logical tests, we have

FALSE || TRUE && TRUE && TRUE

If we follow the order of operations, we have

TRUE && TRUE && TRUE returns TRUE, which becomes

FALSE || TRUE

So the final answer is TRUE.