Wednesday, June 22, 2016

Basics of Programming Part 3: Operators

not related, just funny, and something I wish was real

Part 1: Variables  |  Part 2: Functions | Part 4: Loops

An OPERATOR is a character or symbol that performs a predefined function. To continue our long tradition of jars and honey, an operator could be taking out some of the honey or adding more in (arithmetic). Another operator could be looking at two jars and see if they are both honey (conditionals). Here is a list of all the operators I can think of at this moment:

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (remainder)
= (assign value)
== (true comparison)
! (relational false)
&& (conditional and)
|| (conditional or)
> (greater than)
>= (greater than/equal to)
< (less than)
<= (less than/equal to)

These are not all the operators, but they are very common. The arithmetic operators work how you’d expect. We’ve already covered the difference between = and ==, but for clarification remember that a single = means we’re assigning a value, while == means we’re checking if the relation between two variables is true.

! is a new one, and not one covered in most basic Maths classes (I think; it’s been a long time). This is called a NOT operator. It’s a comparison as well, so != means we’re checking to see if the relation between two variables IS NOT TRUE (false). This can be confusing, or at least it was for me when I first started. So let’s look at a word problem.

I have a bunch of jars of honey, some of which are expired (I don’t know if honey expires, but for this example it does). If I want to get rid of all of the expired jars I need a way to check each jar. So I write a condition to get rid of all of the unfresh (real word :P) jars.

                if(jar != fresh)
           trashJar();//remember functions are actions

This says that if the jar IS NOT fresh (literally fresh is false) then we get rid of it. You could do a different check, something like this:

                if(jar == fresh)

This will accomplish the same task and for something so simple this would work. But let’s say you have a few different jars that are full of honey, others tomatoes, and others moonshine. If you wanted to toss out both tomatoes (because they’re gross) and moonshine (I don’t know why you would), you could write something like this:

if(jar == tomatoes || jar == moonshine)

This is still not a lot of writing, but imagine you not only had tomatoes and moonshine, but carrots, pickles, pig’s feet, zombie viruses, etc. Instead of writing all of this…

if(jar == tomatoes || jar == moonshine || jar == carrots || jar == pickles || jar == pigsFeet || jar == zombieViruses)

…you could simply write:

if(jar != honey)//if jar IS NOT honey…
     trashJar();…then we get rid of it

I think that’s a lot easier. Don’t you? Instead of listing everything not honey you can simply say if it isn’t honey, trash it.

Anyway, in the above example we used the operator || which means OR. Some languages accept both || or or as valid, but we’ll stick with ||. Likewise the operator && means AND. Using all of these operators we can create some very powerful functions, and we can combine them all together if we really wanted to. For instance, if we wanted to throw out jars of honey that were almost empty or expired we could do the following:

if(jar == honey && (honeyAmount <= 1 || !fresh))

This conditional states that if the jar is a honey jar AND either the honey is less than 1 OR is not fresh, then we toss it. Parentheses work like they did in grade school. You first evaluate what’s inside, then you can do the rest of the problem. In this case, even if honeyAmount is greater than 1 but it’s expired, then we’ll toss it out (parentheses), as long as it’s a honey jar (first part).

Operators can get tricky, but this first pass should be enough to get you intrigued. Join us next time when we discuss loops. Sounds sexy, right?

No comments:

Post a Comment