The first thing you need to know is what an operand is. An operand is something that the math or logic is being applied to. In it’s simplest form this is a number. In 1+1 both of the ones are considered to be operands.
But in programming it’s not THAT simple, the operands can also be variables, strings, other expressions, or Boolean values. So for example a + 5 = b - 6, the operands include a, 5, b, and 6.
Operators are what actually does something to the operands. If you were writing a sentence, the operators would be like the verbs. There are several differnt types of operators.
Mathematical operators are the basic math functions you learned as a kid. Addition, subtraction, multiplication and division ( +, -, /, *) are all operators.
Comparisons are also operators. The are greater than (>), less than (<), equal to (==) and not equal to (!=).
And finally Boolean operators are... well... operators! These include: both are true (&&), true if one is true (||), true if only one is true (xor) and true if a single operand is false (!).
Most people have a pretty good grasp on mathematical operators and comparison operators because we grew up with them. Let’s take a closer look at the Boolean operators, because they tend to give people a little more trouble but are an important part of decision making in your computer program.
Boolean (or bool) is a form of logic in which there are only two options: true and false. Since computers think in 0 or 1, yes or no, true or false, we end up using Boolean logic quite often.
In the examples below we are going to use variables to represent conditions. This sort of assumes these variables were set elsewhere in our program. Let’s just say that it is checking variable $color for green. This means that if they eye color is green it will be true, and if the eye color is anything else, it will be false. We are also checking to see if the pet is a dog in $dog. Let’s use some booolean logic on these variables!
Joe - Green Eyes - Cat
Billy - Blue Eyes - Dog
Fred - Brown Eyes - Dog
Mary - Green Eyes - Dog
First, we want someone who has green eyes and a pet dog. So we want both $color and $dog to be TRUE. This would be $color && $dog; because the double ampersand indicates you want both variables to be true. In our $color && $dog scenario, only Mary would be true, because she has green eyes ($color is true) and she has a dog ($dog is true).
Now let’s say we want someone with green eyes -or- a pet dog. In this case we would use $color || $dog; because the two lines indicate that the statement is true if either $color or $dog is true. In this example all four of the people would be true because they have either green eyes or own a dog.
Next we want someone who has green eyes or a dog, but not both. We would do this with $color xor $dog; because xor means one but not both variables need to be true. In this case the only one who comes out true is Joe, because he has green eyes and does not have a dog.
Finally we are looking for someone who does not have a dog. We would do this with !$dog; because the exclamation point means not. So basically we are looking for dog to be not-true, or more precisely, false. This is only true for Joe.
That’s the basics of operators, operands, and Boolean logic!