# Logical Operators

All examples given have used one comparison operator in the if statement. There may be scenario's when more than one comparison needs to be done. To do this you need what is called logical operators. These logical operators are used to take the results of statements using comparison Operators and compare them to other statements. There are 3 logical operators, and, or and not.

A pseudo code example of the and logical operator is as follows:

if condition 1 is true and condition 2 is true

• execute command.

An JavaScript example of this as follows:

```var x = 4
if (x > 1 && x < 5) print ("Is more than 1 and more than 5");```

A pseudo code example of the or logical operator is as follows:

If either condition 1 or condition 2 is true

• execute command.

An JavaScript example of this as follows:

```var x = 3
if (x == null  || x < 5) print ("Is either null or less than 5");```

The last statement not is used to to check the result of a comparison operator statement is not true. A pseudo code example of this is as follows:

if comparison is not true

• execute command.

An example of this is as follows:

```var x = 3;
var y = 3;
if (!(x == y)) print ("false"); else print("true");```

The not logical statement is mostly used when checking boolean variables. Boolean variables only contain the values true or false. Although boolean variables can be used as follows:

```var x = true;
if (x == true) print ("x is set to true"); ```

It can be shortened to to which does the exact function:

```var x = true;
if (x) print ("x is set to true"); ```

The not local statement can be used to check to see if the boolean variable is not equal to true:

```var x = false
if (!x) print ("x is set to false");```

Without the not logical operator, it can be written as

```var x = false
if (x != true) print ("x is set to false");```

Lastly, there is an other way of using the if statement which is called a ternary operation. This is essentially a shortened version of the if statement. The structure of the ternary operation is as follows:

comparison ? result when true : result when false

A JavaScript example of this is as follows:

```var a = 4;
var b = 3
var result = a > b ? a : b;
print (result);```

If used as an ordinary if statement it would be as follows:

```if (a > b)
var result = a;
else
var result = b; ```

Projects/Seed/Tutorial/basic/logical (last edited 2013-11-22 19:19:54 by WilliamJonMcCann)