0% found this document useful (0 votes)
10 views

CH 3

The document discusses variables and data types in C++ programming. It explains how to declare variables, assign values, perform arithmetic operations, and use various operators like increment, decrement, modulo. It also covers scopes and shadowing of variables declared in blocks.

Uploaded by

Random Swag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CH 3

The document discusses variables and data types in C++ programming. It explains how to declare variables, assign values, perform arithmetic operations, and use various operators like increment, decrement, modulo. It also covers scopes and shadowing of variables declared in blocks.

Uploaded by

Random Swag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

An Introduction to

Programming through C++


Krishna. S
Jan 18, 2018
Ch. 3: Variables and Data Types
Reserving memory for storing numbers

• Before you store numbers, you must


explicitly reserve space for storing them.
• This is done by a “variable definition”
statement.
• variable: name given to the space you
reserved.
• You must also state what kind of values
will be stored in the variable: “data type”
of the variable.
Assignment statement
Used to store results of computation into a variable. Form:
variable-name = expression;
• Example:
s = u*t + 0.5 * a * t * t;
• Expression : can specify formula involving constants or
variables, almost as in mathematics.
– If variables are specified, their values are used.
– multiplication must be written explicitly.
– multiplication, division have higher precedence than addition,
subtraction
– multiplication, division have same precedence
– addition, subtraction have same precedence
– operators of same precedence will be evaluated left to right.
– Parentheses can be used with usual meaning.
Examples
int x=2, y=3, p=4, q=5, r, s, t;
r = x*y + p*q;
// r becomes 2*3 + 4*5 = 26
s = x*(y+p)*q;
// s becomes 2*(3+4)*5 = 70
t = x – y + p – q;
// equal precedence,
// so evaluated left to right,
// t becomes (((2-3)+4)-5 = -2
Arithmetic between different
types allowed
int x=2, y=3, z, w;
float q=3.5, r, s;
r = x; // r becomes 2.0
z = q; // store with truncation
s = x*q; // convert to same type
// then multiply.
// Which type?
An interesting assignment
expression
int p = 12;
p = p + 1;

• Rule for evaluation: first evaluate the value on the left


hand side. Then store the result into the lhs variable.
• So 1 is added to 12, the value of p
• The result, 13, is then stored in p.
• Thus p finally becomes 13.

“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.

You might also like