CH 3
CH 3
“p = p + 1” is nonsensical in mathematics.
“=” in C++ is different from “=” in math.
Some additional operators
• The fragment “i = i + 1” appears very
frequently, and so can be abbreviated as
“i++”.
• ++ : increment operator. Unary
• Similarly we may write “j--” which
means “j = j – 1”
• -- : decrement operator
Intricacies of ++ and --
• ++ and –- can be written after the variable,
and this also cause the variable to
increment or decrement.
int i=5, j=6;
++i; --j; // i becomes 6, j 5.
• ++ and -- can be put inside expressions,
which is discussed in the book for
completeness, but not recommended in
polite programming.
A useful operator: % for finding
remainder
• x % y evaluates to the remainder when x is
divided by y. x, y must be integer
expressions.
• Example
int n=12345678, d0, d1;
d0 = n % 10;
d1 = (n / 10) % 10;
• d0 will equal the least significant digit of n, 8.
• d1 will equal the second least significant digit
of n, 7.
Compound assignment
• The fragments of the form “sum = sum +
expression” occur frequently, and hence they
can be shortened to “sum += expression”
• Likewise you may have *=, -=, …
• Example
int x=5, y=6, z=7, w=8;
x += z; // x becomes x+z = 12
y *= z+w; // y becomes y*(z+w) = 90
Blocks and Scope
• Code inside {} is called a block. // The summing program
• Blocks are associated with // written differently.
repeats, but you may create
them arbitrarily. main_program{
• You may declare variables inside int s = 0;
any block. repeat(10){
New summing program: int term;
• The variable term is defined cin >> term;
close to where it is used, rather
than at the beginning. This s = s + term;
makes the program more }
readable. cout << s << endl;
• But the execution of this code is }
a bit involved.
How definitions in a block execute
Basic rules
• A variable is defined/created every time control
reaches the definition.
• All variables defined in a block are destroyed
every time control reaches the end of the block.
• “Creating” a variable is only notional; the
compiler simply starts using that region of
memory from then on.
• Likewise “destroying” a variable is notional.
• New summing program executes exactly like the
old, it just reads different (better!).
Shadowing and scope
• Variables defined outside a block can be used
inside the block, if no variable of the same
name is defined inside the block.
• If a variable of the same name is defined, then
from the point of definition to the end of the
block, the newly defined variable gets used.
• The new variable is said to “shadow” the old
variable.
• The region of the program where a variable
defined in a particular definition can be used
is said to be the scope of the definition.
main_program{ Example
int x=5;
cout << x << endl; // prints 5
{
cout << x << endl; // prints 5
int x = 10;
cout << x << endl; // prints 10
}
cout << x << endl; // prints 5
}
Concluding Remarks
• Variables are regions of memory which can store
values.
• Variables have a type, as decided at the time of
creation.
• Choose variable names to fit the purpose for
which the variable is defined.
• The name of the variable may refer to the
region of memory (if the name appears on the
left hand side of an assignment), or its value (if
the name appears on the right hand side of an
assignment).
More remarks
• Expressions in C++ are similar to those in
mathematics, except that values may get
converted from integer to real or vice versa
and truncation might happen.
• Truncation may also happen when values get
stored into a variable.
• Sequence generation and accumulation are
very common idioms.
• Increment/decrement operators and compound
assignment operators also are commonly used.
More remarks
• Variables can be defined inside any block.
• Variables defined outside a block may get
shadowed by variables defined inside.