Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
7/2/2015
Assignment Operations
Assignment Statement: assigns the value of the expression on
the right side of the = to the variable on the left side of the =
The symbol "=" is known as the assignment operator. The meaning of "=" in programming
is different from Mathematics. It denotes assignment instead of equality.
Example:
Assignment operation to calculate an Employees raise.
7/2/2015
7/2/2015
7/2/2015
Example:
sum = sum + 10;
is equivalent to: sum += 10;
price *= rate +1;
is equivalent to:
price = price * (rate + 1);
7/2/2015
7/2/2015
Type Casting
Implicit Type-Conversion vs. Explicit Type-Casting
Converting a value from one type to another type is called type
casting (or type conversion).
There are two kinds of type casting:
7/2/2015
C++ will not perform automatic type conversion, if the two types are
not compatible.
CS001P Programming Fundamentals 1
7/2/2015
7/2/2015
Explicit Type-Casting
Explicitly perform type-casting via the so called unary type-casting
operator in the form of (new-type)operand or new-type(operand).
The type-casting operator takes one operand in the particular type,
and returns an equivalent value in the new type.
7/2/2015
10
7/2/2015
11
Operator static-cast<type>
C++ introduces a new operator called static_cast<type> to perform
type conversion (because the regular cast mentioned earlier is too lax
and could produce expected results).
static_cast signals an error if conversion fails.
Example:
7/2/2015
12
Increment/Decrement Operators
Increment Operator ++: unary operator for the special case
when a variable is increased by 1.
Prefix Increment operator appears before the variable.
Example: ++i
7/2/2015
13
//assign ns value to k
//and then increment n
CS001P Programming Fundamentals 1
7/2/2015
14
--i;
i--;
7/2/2015
15
7/2/2015
16
7/2/2015
17
7/2/2015
18
7/2/2015
19
Example:
7/2/2015
20
7/2/2015
21
7/2/2015
22
7/2/2015
23
7/2/2015
24
7/2/2015
25
7/2/2015
26
7/2/2015
27
7/2/2015
28
7/2/2015
29
7/2/2015
30
7/2/2015
31
7/2/2015
32
7/2/2015
33
7/2/2015
34
7/2/2015
35
Symbolic Constant
Symbolic constant: a constant value that is declared
with an identifier using the const keyword
7/2/2015
36
7/2/2015
37
Resource Link
https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp1_Basi
cs.html
7/2/2015
38