Module 5 Operators

These are examples of Operators.



Understanding Operators

Operators are generally used to preform some sort of calculation, comparison or assignment on one or more values. There are 5 types of Operators.

For this module we will be concentrating on Mathematical and Assignment Operators.

Mathematical Operators

For a mathematcial calculation, you use a mathematical operator. The values that you use can be any sort of values you like. You can use two variables, two numbers or a variable and a number. There are 8 types of mathematical operators that can be used in mathematical operations. Addition (+ Adds two values), Subtraction (- Subtracts one value from another), Multiplication (* Multiplies two values), Division (/ Divides one value by another), Modulus (% Divides one value by another and returns the remainder), Increment (++ Shortcut to add 1 to a single number), Decrement (-- Shortcut to subtract 1 from a single number) and Unary Negative ( - Makes a positive negative or a negative positive).

Mathematical Addition Operator

For a simple addition operator, we could write the Mathematical Operation of number1 + number 2 = the sum of number 1 and number 2. So if number 1 is 263.552 and number 2 is 5887.255 the answer would be . The answer was just written using the Addtion Operator.

Subtraction Operator

Simple subtraction is another form of a Mathematical Operator. An example of this my 2002 Chevrolet Tracker. I purchased the Tracker for $14,299 and financed it. That was in 2003 and the duration of the loan was 60 months. To date I have paid $7,123 on that loan. To calculate what is still owed, I simply subtract what has been paid from the original loan amount. This results in a balance due of $ . The answer was just written using the Subtraction Operator.

The problem here is the end value is in dollars and no decimal places are added or comma to seperate thousands from hundreds. I am going to presume the instructions for that come in a later Module.

Increments of 1

To increase a number by one, you can use the Increment Operator. The operator is simply a "++" that is put either before or after the number you wish to increase by one. Placement of the "++" determines how you need to call the results.

By adding one to the original number 55 and placing the increment before the original number we get a new value of the original number and that is . Simple right? The answer was just written using the Increment Operator for a new result.

However, if I place the "++" after the variable, the result is a changed variable, not a new value. Thu sin order to see the new number, you need to write script to call on that number. So if I create the second original number of 72 and place the "++" after the variable, by calling on the script of the original number I get . The answer was just written using the Increment Operator for a new original number.

To use the Decrement Operator, just use "--" instead of a "++" for a decreased amount by 1.

Assignment Operators

Assignment Operators assign a vaule to a variable. They do no compare two items, nor do they preform logical tests. There are 6 different Assignment Operators. Assignment (= Assigns the value on the right side of the opertor to a variable), Add and Assign (+= Adds the value on the right side of the operator to the variable on the left side, and then assigns the new value to the variable), Subtract and Assign (-= Subtracts the value on the right side of the operator to the variable on the left side, and then assigns the new value to the variable), Multiply and Assign (*= Multiples the value on the right side of the operator to the variable on the left side, and then assigns the new value to the variable), Divide and Assign (/=Divides the value on the right side of the operator to the variable on the left side, and then assigns the new value to the variable), Modulus and Assign (%= Takes the integer remainder of dividing the variable on the left side byt the value on the right side and assigns the new value to the variable).

Add-And-Assign Operator

The "+=" operator adds the value on the right side of the operator to the variable on the left side and then assigns to the variable that new value. Instead of writing the variable code name an extra time, you can use the add-and-assign operator to shorten the code. You can even use a variable rather then a plain number value on the right side. By doing it that way, the variable can be changed to affect the result of the assignment.

For this example, say I had a MegaBucks Lottery Ticket with 4 winning numbers on each line but one line had all six winning numbers! Yes, it's a dream but what the heck. By creating the variable for the 6 winning numbers of $1500000 and adding the $100000 for 4 winning numbers, I end up with the result of $ . Now could you imagine what to do with that amount?

Subtract-And-Assign Operator

So you won the lottery and now need to make plans on what to do with the money. Not so fast there, little camper! Uncle Sam wants his fair share!!! And believe you me, he's going to get it one way or the other.

2 things are certain in life, death and taxes. Generally speaking the tax on the lottery is going to be approximately 33%. This can be calculated with a Multiply-And-Assign (*=) operator by taking the winnings times .33 to come up with the amount of taxes being $ .

The "*=" operator multiplies the value on the right side of the operator to the variable on the left side and then assigns to the variable that new value. Instead of writing the variable code name an extra time, you can use the multiply-and-assign operator to shorten the code. You can even use a variable rather then a plain number value on the right side. By doing it that way, the variable can be changed to affect the result of the assignment.

You can then use that amount to create a Subtract-and-Assign Operator to remove the amount of taxes due to show you the true winnings which would be $ . Still a healthy chunk of change.

The "-=" operator subtracts the value on the right side of the operator to the variable on the left side and then assigns to the variable that new value. Instead of writing the variable code name an extra time, you can use the subtract-and-assign operator to shorten the code. You can even use a variable rather then a plain number value on the right side. By doing it that way, the variable can be changed to affect the result of the assignment.

But now you need to see how much you will actually, really, truly be paid since this is paid out over 25 years! For this use a Divide-and-Assign! Take that $1072000 and divide it by 25 to reach your yearly payout of $ . Personally, I could live off of that! Could You?

Like the addition and subtraction, the "/=" operator divides the value on the right side of the operator to the variable on the left side and then assigns to the variable that new value. Instead of writing the variable code name an extra time, you can use the divides-and-assign operator to shorten the code. You can even use a variable rather then a plain number value on the right side. By doing it that way, the variable can be changed to affect the result of the assignment.

Return to the top of the page.