Programming Lecture 3
Programming Lecture 3
For example,
2 + 3 * 5 and (2 + 3) * 5
• ? = 1 + 2 * (3 + 4)
9
Data Type of an Arithmetic Expression
• Data type of an expression depends on the type of its operands
– Data type conversion is done by the compiler
10
Data Type of an Arithmetic Expression
Example
int * int; result int
12
Data Type of an Arithmetic Expression
• Only the integer part of the result will be considered if two operands are integer
Even when the target variable is float
13
Increment and Decrement Operators
• Examples :
++K , K++ → k= K+1
--K , K-- → K= K-1
14
Increment and Decrement Operators
• If the value produced by ++ or – – is not used in an expression, it does
not matter whether it is a pre or a post increment (or decrement).
• When ++ (or – –) is used before the variable name, the computer first
increments (or decrements) the value of the variable and then uses its
new value to evaluate the expression.
• When ++ (or – –) is used after the variable name, the computer uses the
current value of the variable to evaluate the expression, and then it
increments (or decrements) the value of the variable.
x = 5;
Cout << x++; 15
special assignment statements
• C++ has special assignment statements called compound
assignments
+= , -= , *= , /= , %=
• Example:
X +=5 ; means x = x + 5;
x *=y; means x = x * y;
x /=y; means x = x / y;
16
Relational Expression and Relational Operators
• Relational expression is an expression which compares 2 operands and returns a
TRUE or FALSE answer.
Example : a >= b , a == c , a >= 99 , ‘A’ > ‘a’
• Relational expressions are used to test the conditions in selection, and looping
statements.
Operator Means
== Equal To
!= Not Equal To
< Less Than
<= Less Than or Equal To
17
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18