Logical Operators

Logical operators are used to either combine conditional and relational operators or change reverse their conditional output. You can combine conditional operational so that two or more conditions must be accepted, or at least one of multiple conditions are met before proceeding. The final logical operator is used to reverse a condition, so that that reverse is accepted as the ‘true’ boolean response.

The AND Operator

The and operator is written using two ampersands ‘&’ symbols, and is used to make sure two or more conditions are met before executing a piece of code. This can be used to make sure that two or more conditions are true. The below code snippets show how this works.

The below code will print the letter “A” as the number 3 is more than the number 2 and the letter ‘a’ is equal to the letter ‘a’.

The below code will print the letter ‘B’ as even though the letter ‘a’ is equal to the letter ‘a’, the number 3 is not more than the number 4, and when using the and operator both conditions must be met.

The OR Operator

The or operator is written using two pipe ‘|’ symbols, and is used to make sure at least one condition in multiple conditions is met before executing some code. This can be used to make sure that at least condition in a list of conditions are true. The below code snippets show how this works.

The below code will print the letter “A”, this is because even though the number 3 is not more than the number 4 the letter ‘a’ is equal to the letter ‘a’. When using the or operator only one of the conditions must be met.

The below code will print the letter ‘B’, this is because the number 3 is less than the number 4 and the letter ‘a’ is not equal to the letter ‘b’.

The NOT Operator

The not operators is written using one escalmation mark ‘!’ symbol, and is used to reverse a single condition. This means that when a condition usually returns true, using the not operator will make it return false instead. The below code snippets show how this works.

The below code will print the letter ‘B’ as the not operator is used to reverse the true boolean value within the if condition.

The below code will print the letter ‘A’ as even though the number 3 is not more than the number 4 the not operator reversing this condition.

Brackets and Complex Operations

Using these operators you can create really complicated if and else if statements, as you can have as many conditions with as many logical and relational operators as you want or need. You can also use brackets to seperate complex conditional statements, to ensure the ordering is processed correctly. The below code snippets show how this works.

The below code will print out the letter ‘A’. This is due to the order or precedence of the logical operators. Even though the first condition evaluates to false (1 is not equal to 0) due to the or operator at the end the whole condition evaluates to true. Every condition is checked from left to right, so the first two statements are within the and operator and the result of that is combined using the or operator and the last condition.

The below code will print out the letter ‘B’. This is because we have used normal brackets to seperate the conditions. Everything inside brackets will be worked out before taking into account the rest of the conditions. This means that even though the two operations inside the brackets are true, due to the combination of the and operator and the brackets, to seperate the or conditions, our whole statements evaluates to false.

Leave a Reply