Professional Documents
Culture Documents
• 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
•( 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
++count;
1
total = ++count + 3;
2
12
total