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 video
The Assignment
Using the code below as a starting point, build the new program so that it does the correct computations. i have provided the Psuedo-Code as a convenience for you. Enjoy!!!
The Code
#include using namespace std;
int main(void)
{
int i, j, k;
cout << "Enter a value for i: ";
cin >> i;
cout << "Enter a value for j: ";
cin >> j;
// 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
cout << k;
return 0;
}
int main(void)
{
int i, j, k;
cout << "Enter a value for i: ";
cin >> i;
cout << "Enter a value for j: ";
cin >> j;
// 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
cout << k;
return 0;
}