|

Integer
division is somewhat different from everyday division.
When you divide 21 by 4, the result is a real number
(a number with a fraction). Integers don’t have
fractions, and so the "remainder" is lopped off.
The answer is therefore 5. To get the remainder,
you take 21 modulus 4 (21 % 4) and the result is
1. The modulus operator tells you the remainder
after an integer division.
Finding
the modulus can be very useful. For example, you
might want to print a statement on every 10th action.
Any number whose value is 0 when you modulus 10
with that number is an exact multiple of 10. Thus
1 % 10 is 1, 2 % 10 is 2, and so forth, until 10
% 10, whose result is 0. 11 % 10 is back to 1, and
this pattern continues until the next multiple of
10, which is 20. We’ll use this technique when looping
is discussed In unit 7 , "More Program Flow."
WARNING: Many novice C++ programmers
inadvertently put a semicolon after their if statements:
if(SomeValue
< 10);
SomeValue
= 10;
What
was intended here was to test whether SomeValue
is less than 10, and if so, to set it to 10, making
10 the minimum value for SomeValue. Running this
code snippet will show that SomeValue is always
set to 10! Why? The if statement terminates with
the semicolon (the do-nothing operator). Remember
that indentation has no meaning to the compiler.
This snippet could more accurately have been written
as:
if
(SomeValue < 10) // test
;
// do nothing
SomeValue
= 10; // assign
Removing
the semicolon will make the final line part of the
if statement and will make this code do what was
intended.
Combining
the Assignment and Mathematical Operators
It
is not uncommon to want to add a value to a variable,
and then to assign the result back into the variable.
If you have a variable myAge and you want to increase
the value by two, you can write
int
myAge = 5;
int
temp;
temp
= myAge + 2; // add 5 + 2 and put it in temp
myAge
= temp; // put it back in myAge
This
method, however, is terribly convoluted and wasteful.
In C++, you can put the same variable on both sides
of the assignment operator, and thus the preceding
becomes
myAge
= myAge + 2;
which
is much better. In algebra this expression would
be meaningless, but in C++ it is read as "add two
to the value in myAge and assign the result to myAge."
Even
simpler to write, but perhaps a bit harder to read
is
myAge
+= 2;
The
self-assigned addition operator (+=) adds
the rvalue to the lvalue and then reassigns the
result into the lvalue. This operator is pronounced
"plus-equals." The statement would be read "myAge
plus-equals two." If myAge had the value 4 to start,
it would have 6 after this statement.
There
are self-assigned subtraction (-=), division
(/=), multiplication (*=), and modulus
(%=) operators as well.
Increment
and Decrement
The
most common value to add (or subtract) and then
reassign into a variable is 1. In C++, increasing
a value by 1 is called incrementing, and decreasing
by 1 is called decrementing. There are special operators
to perform these actions.
The
increment operator (++) increases the value
of the variable by 1, and the decrement operator
(--) decreases it by 1. Thus, if you have
a variable, C, and you want to increment it, you
would use this statement:
C++;
// Start with C and increment it.
This
statement is equivalent to the more verbose statement
C
= C + 1;
which
you learned is also equivalent to the moderately
verbose statement
C += 1;
|