You are on page 1of 21

Variable Scoping and :: Operator

• The scope of an
identifier is that part of
the program where it
can be used.
• For example, variables
cannot be used before
they are declared, so
their scopes begin
where they are declared.
• Demo on Scoping
Escape Sequence
• The backslash (\) is called an escape character.
• It indicates that cout is supposed to do something out of
the ordinary.
• When encountering a backslash in a string, the compiler
looks ahead at the next character and combines it with the
backslash to form an escape sequence.
• The escape sequence \n means newline.
Some Escape Sequences
Sequence Meaning
\n Newline. Position the screen cursor to the beginning of t he next
line
\t Horizontal tab. Move the screen cursor to the next tab stop
\r Carriage return. Position the screen cursor to the beginning of the
current line; do not advance to the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print backslash character
\’ Single quote. Used to print a single quote character.
\’’ Double quote. Used to print a double quote character.
Arithmetic Operators
Operations Precedence
() Parenthesis Evaluated first. If the parentheses are nested,
the expression in the innermost pair is evaluated
first. If there are several pairs of parentheses
“on the same level” (i.e., not nested),
they’re evaluated left to right.
* Multiplication Evaluated second. If there are several, they’re evaluated left to
right
/ Division
+ Addition Evaluated last. If there are several, they’re evaluated left to
right.
- Subtraction
Relational Operators
Operator C++ equivalent C++ sample Meaning of C++
s operator condition

> > X>y X is greater than y


< < X<y X is less than y
>= X >= y X is greater or equal
to y
<= X <= y X is less or equal to y
= == X == y X is equal to y
!= X != y X is not equal to y
Operator Precedence Summary
Operator Precedence Summary
Activity – Operator Precedence

•( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ) 324

• 2 % 2 + 2 * 2 - 2 / 2; 3

• 7 + 3 * 6 / 2 - 1;
15
Composite Assignment Operators
• In addition to the assignment operator, C++ also includes the
following composite assignment operators : +=, -=, *=, /=, and
%=.
• When applied to a variable on the left, each applies the indicated
arithmetic operation to it using the value of the expression on the
right.
int n=22;
n += 9; // adds 9 to n: equivalent n=n+9
Increment and Decrement Operators
• C++ provides two unary operators for adding 1 to or
subtracting 1 from the value of a numeric variable
• These are the unary increment operator, ++, and the
unary decrement operator, --
Operator Name C++ Code Description

++ Pre-increment ++x Increment x by 1, then return the new value


of x in the expression in which a resides.[so it
will return one greater than the original value]
++ Post-increment x++ Use the current value of x in the expression
in which a resides, then increment x by 1. [will
return the current value and then increment]
78
59
6
int count = 5 ;
count = count + 1 ;
count += 1 ; count

++count;
1

total = ++count + 3;
2
12
total

The increment and decrement operators execute before any


other binary arithmetic operators in an expression.
Pre-fix Increment/Decrement Operator
• The prefix form of ++ increments the variable to which it applies before its value is used in
context.
• Consider the following code
int count = 5;
total = ++count + 6;
With an initial value of 5 for count, total is assigned the value 12.
• In this case, count will be incremented to 6 before it gets added with the constant 6 or its
surrounding pression.
• The preceding statement is thus equivalent to the following two statements:
count = count + 1;
total = count + 6;
The rule is same for decrement operator
Post-fix Increment Example
• The postfix form of ++ increments the variable to which it applies after its value is used in context.
• Consider the following code
int count = 5;
total = count++ + 6;
With an initial value of 5 for count, total is assigned the value 11.
• In this case, count will be incremented to 6 only after being used in the surrounding expression.
• The preceding statement is thus equivalent to the following two statements:
total = count + 6;
++count;
• It would be clearer to write the preceding statement as follows:
total = 6 + count++;
The rule is same for decrement operator
WARNING
• You should take care applying these operators to a given variable more than
once in an expression.
• Suppose count has the value 5 and you write this:
total = ++count * 3 + count++ * 5;
• The result of this statement is undefined because the statement modifies the
value of count more than once using increment operators.
• there is no guarantee at all of consistency in the results.
• The effects of statements such as the following used to be undefined as well:
k = k++ + 1;
Decrement Operators
Operator Name C++ Code Description

-- Pre-decrement --x Decrement x by 1, then return the new value


of a in the expression in which x resides.[so it
will return one less than the original value]
-- Post-decrement x-- Use the current value of x in the expression
in which a resides, then decrement x by 1. [will
return the current value and then decrement]
Challenge : Post and Pre Increment

int x = 10, y = 10, z = 0;


z += ++x + y++;
cout << z; //??
Demo
Increment, Decrement Operators
Demo
Composite Assignment Operators
Type Conversion
• C++ also converts integral types into floating point types
when they are expected.
int n = 22;
float x = 3.14159;
x += n; // the value 22 is automatically converted to 22.0
cout << x - 2 << endl; // value 2 is automatically
//converted to 2.0

• But converting from a floating point type to an integral type


is not automatic.
Type Conversion
• In general, if T is one type and v is a value of another type, then the
expression T(v) converts v to type T.
• This is called type casting .
• For example, if expr is a floating-point expression and n is a variable of type
int, then
n = int(expr);
converts the value of expr to type int and assigns it to n.
• The effect is to remove the real number’s fractional part, leaving only its
whole number part to be assigned to n.
• This is truncating , not rounding.
Selection Structures: Branching

You might also like