Wyo C++ - Ch. 5 Notes
Objective #1: Use the arithmetic operators.
- The assignment operator
is the equals symbol (=)
- The assignment operator changes the value of the variable to its left after
evaluating the expression on its right!!!!!! For example,
sum = 3 + 1000;
the variable sum ends up with the value
1003. Note that you cannot use a comma within numerical values in C++. The
statement in the example above is called an assignment statement since it
assigns a value (1003) to a variable (sum). Other examples:
salary = 40000;
poundsPressure = 15 + 12;
sum = original + 300;
salary = salary + raise;
-
You can make multiple assignments in one C++ statement
but I prefer separate statements for code written in this class.
- For example:
i = j = k = 10;
// initializes all three variables to the value,
10
is valid but three separate statements like the following example is required
by my Coding Standards:
i = 10;
j = 10;
k = 10;
- The common arithmetic operators are:
+ for addition
- for subtraction
* for multiplication
/ for division
% for modulus
(like finding the remainder of a division problem)
For example, in the assignment statement
rem = 5 % 2;
the value 1 is assigned to the variable rem since 1 is the remainder
that you get when you divide 5 by 2
- Sometimes it is convenient to store calculations in separate variables if
they are going to be used later in the program; at other times, it is convenient
to simply output the results directly to the screen
- Use "whitespace"
to make your arithmetic expressions and statements readable to fellow programmers
even if the C++ compiler doesn't require whitespace
x = 3 + y; is better than
x=3+y;
- Compound operators are sometimes useful if used carefully
- example: j += 1; is the
same as j = j + 1;
- example: total /= 2; is the
same as total = total / 2;
Objective #2: Increment and decrement
variables.
- Most often, we use the incrementing and decrementing operators with variables
in statements such as these, that is with no other operators or code except
the variable name that is being incremented or decremented.
- In this course, we will avoid using the incrementing or decrementing operators
in statements with other operators. If you are interested though in understanding
how these operators may be used in more complicated situations, read the following:
When the incrementing operator is used in front of a variable (as in ++sum),
it is formally known as the preincrementing operator. When it follows a variable
(as in sum++ ), it is known as the postincrementing operator. If other operators
are used in the C++ statement, you must be aware of the differences between
these two versions of the incrementing operator.
In the following two statements the variable sum is incremented at different
moments in relation to when its value is displayed on the screen. In the first
example (with the postincrementing operator), the value of 45 is displayed
on the screen because the compiler actually increments the variable sum AFTER
(i.e. post) the display (i.e. the "cout operation"). In the second
example, sum is incremented BEFORE (i.e. pre) the cout operation.
- cout << sum++;
// prints the value 45 if the original value of sum was 45
- cout << ++sum; //
prints the value 46 if the original value of sum was 45
- It can be very tricky understanding the subtle differences between the pre
and postincrementing (and decrementing operators). Do the following examples
and check your answers to be sure that you understand them!
- Assume that the initial values of x, c, and m are 0, 5, & 10, respectively
and give the final values of those three variables (in that order) after
each of the assignment statements are executed.
(Use the original values of x, c, & m in each separate exercise.)
- x = c++ + m;
- x = (++m - c) * ++x;
- x = x-- * (c - m + c - m - c - m);
- x = m++ - --c;
- x = m++ - --m;
- x = ++m - c++;
- x = ++m - m++;
- Click here for the answers
Objective #3: Understand the order of operations.
- Remember to obey the order of operations ("Please
Excuse My Dear Aunt
Sally") that you learned in junior high or elementary
school. That is, perform the following mathematical operations in this order:
Parentheses, Exponentiation, Multiplication, Division, Addition and Subtraction.
- However, with regard to multiplication and division, you must work left
to right within an algebraic expression. Be sure that you understand that
8 / 4 * 2 is equal to 4. The expression
is not equal to 1 since the division is performed before the multiplication
according to the order of operations.
- With regard to addition and subtraction, you must work left to right within
an algebraic expression as well. Be sure that you understand that 8
- 4 + 2 is equal to 6 and NOT 2.
- Note that in C++ the modulus
operator (%) has the same precedence as * and / but
is higher in precedence than + and -. (Some other computer languages rank
multiplication and division higher in precedence than modulus.)
- Use parentheses if necessary to override the order of operations in order
to produce the desired result
Objective #4: Use mixed data types.
- Promotion is supported in
C++ to allow your expressions or statements to temporarily convert "smaller"
data types to "larger" ones in order to provide an answer to a calculation
(see pp. 81 & 82). Promotion is performed by the compiler in mixed mode
expressions. Technically, the compiler cannot multiply an integer with a double
for example so it would automatically promote the smaller of the two data
types, the integer, to a double.
int num1 = 2;
double num2 = 2.1;
double result = 0.0;
result = num1 * num2;
As in some other programming languages, this multiplication does not result
in a "type mismatch" compile error like in C++ since the compiler
promotes num1 to a double. The variable result ends up with the floating-point
value 4.2. In this example, an integer (a 4-byte variable) is being implicitly
promoted to a larger data type double (8-bytes).
- Typecasting
is also allowed by the C++ compiler to allow the programmer to specify the
data type just before the variable within a statement, but you must enclose
the variable identifier in parentheses
double circumference = 0.0;
int diameter = 2;
const double PI = 3.14;
circumference = PI * double (diameter);
In the code segment above, the int variable diameter is explicitly being
type casted (or sometimes just called casted) to a double. In this example,
the int variable would have been promoted anyway by the C++ compiler even
if the programmer didn't cast it to a double. Typically type casting is
used to avoid integer division as explained elsewhere in these notes.
A programmer could cast a variable "down" as well. For example,
int letter = 65;
cout << char (letter);
will cause the variable letter to be displayed as a capital A since the
ASCII character for the ASCII value 65 is the letter A.
- With a mixed mode expression such as (numPurchased * 0.06). The result
of the expression is a double even if numPurchased is an integer data type.
- This Web site
has a good explanation of typecasting.
Objective #5: Avoid overflow, underflow, and floating-point errors.