Shortcut Operators
OK... Shortcut Operators can be a little bit confusing at first, but when you get the hang of it they shorten everything that you type. In this lesson you are going to have the opportunity to interact with Shortcut Operators
So basically what they do is just simplify what you have to type into your code.
For Example:
This line of code for a video game point counter,
currentPoints = currentPoints + 100;
Could be shortened to,
currentPoints += 100;
Using Shortcut Operators keeps us from having to type the name of the variable currentPoints twice. All we have to do is simply put the addition(+) operator in front of the assign(=) operator and BOOM we don't have to type in the variable again.
Another Example:
This line of code for a video game point counter,
currentPoints = currentPoints * bonusMultiplier;
Could be shortened to,
currentPoints *= bonusMultiplier;
Using Shortcut Operators can be Addition, Subtraction, Multiplication, and Division. These operators gives the programmer many options to work with and allow you to type less code in the long run.
Another, more complex, example,
This line of code for a video game point counter,
currentPoints = (currentPoints * bonusMultiplier) * 2
Can easily be shortened to this,
currentPoints *= bonusMultiplier * 2
So basically what they do is just simplify what you have to type into your code.
For Example:
This line of code for a video game point counter,
currentPoints = currentPoints + 100;
Could be shortened to,
currentPoints += 100;
Using Shortcut Operators keeps us from having to type the name of the variable currentPoints twice. All we have to do is simply put the addition(+) operator in front of the assign(=) operator and BOOM we don't have to type in the variable again.
Another Example:
This line of code for a video game point counter,
currentPoints = currentPoints * bonusMultiplier;
Could be shortened to,
currentPoints *= bonusMultiplier;
Using Shortcut Operators can be Addition, Subtraction, Multiplication, and Division. These operators gives the programmer many options to work with and allow you to type less code in the long run.
Another, more complex, example,
This line of code for a video game point counter,
currentPoints = (currentPoints * bonusMultiplier) * 2
Can easily be shortened to this,
currentPoints *= bonusMultiplier * 2
Pre and Post Operators
In this lesson you are going to have the opportunity to interact with Pre and Post Operators. A Pre-Operator is an operator that is stated in the code BEFORE the Variable. A Post-Operator is an Operator that is stated in the code AFTER the operator. these work very different than each other in the final results of how the solution is acquired.
Pre-Operator
A Pre-Operator will interact with the Variable before any other operations are accomplished.
Example:
int v;
v = 10;
v = 2 * ++v
From the example above the integer v which is equal to 10, is going to be multiplied by 2, BUT this will only happen AFTER v is increased by 1. The reason that this will happen is because the Pre-Operator is a higher priority than the multiplication operator.
So.... the way that this is figured is like this, (v + 1) * 10 = v
Result v = 22
Post-Operator
A Post-Operator will interact with the Variable eventually some where along the course of the program.... But it will not really have any interaction with this equation
Example:
int v;
v = 10;
v = 2 * v++
From the example above the integer v which is equal to 10, is going to be multiplied by 2, eventually v will be increased by 1 to equal 11.
So..... the way that this is figured is like this, v * 10 = v, later on v will be increased by 1
Result v = 20
Pre-Operator
A Pre-Operator will interact with the Variable before any other operations are accomplished.
Example:
int v;
v = 10;
v = 2 * ++v
From the example above the integer v which is equal to 10, is going to be multiplied by 2, BUT this will only happen AFTER v is increased by 1. The reason that this will happen is because the Pre-Operator is a higher priority than the multiplication operator.
So.... the way that this is figured is like this, (v + 1) * 10 = v
Result v = 22
Post-Operator
A Post-Operator will interact with the Variable eventually some where along the course of the program.... But it will not really have any interaction with this equation
Example:
int v;
v = 10;
v = 2 * v++
From the example above the integer v which is equal to 10, is going to be multiplied by 2, eventually v will be increased by 1 to equal 11.
So..... the way that this is figured is like this, v * 10 = v, later on v will be increased by 1
Result v = 20
Pre-Assignment videos
The Assignment
Add the following Psuedocode into your program and turn each line into code.
You will need three int variables named i, j, and k.
You will need three int variables named i, j, and k.
Pseudocode
// add i and j and assign it to k
// print k to the screen
// Pre-Operate 1 to i and Multiply it by j assign the result to k
// print k to the screen
// Post-Operate 1 to j and divide it by i assign the result to k
// print k to the screen
// Pre-Operate 1 to j and i, multiply i by j, add it k, assign the result to k
// add i to itself using short code operators
// print i to the Console
// subtract j by itself using short code operators
// print j to the Console
// multiply i by itself using short code operators
// print k to the Console
// divide k by itself using short code operators
// print k to the screen
// Pre-Operate 1 to i and Multiply it by j assign the result to k
// print k to the screen
// Post-Operate 1 to j and divide it by i assign the result to k
// print k to the screen
// Pre-Operate 1 to j and i, multiply i by j, add it k, assign the result to k
// add i to itself using short code operators
// print i to the Console
// subtract j by itself using short code operators
// print j to the Console
// multiply i by itself using short code operators
// print k to the Console
// divide k by itself using short code operators