Under the supervision of
Dr. Ghofran Mohamad
2
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5
Precedence of arithmetic operations
For example,
2 + 3 * 5 and (2 + 3) * 5
both have different meanings
6
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 8
Precedence of arithmetic operations
• ? = 1 + 2 * (3 + 4)
– Evaluated as 1 + (2 * (3+4)) and the result is 15
• ?=5*2+9%4
– Evaluated as (5*2) + (9 % 4) and the result is 11
• ? = 5 * 2 % ( 7 – 4)
– Evaluated as (5 * 2) % (7 – 4) and the result is 1
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
• If operators are *, /, +, or – , then the type of the result will be:
– integer, if all operands are integer.
» Int A , B;
» A+ B → Integer.
– float, If at least one operand is float and there is no double
» Int A ; Float B;
» A + B → Float.
– double, if at least one operand is double
• Int A ; Float B; Double C;
» A + B + C → double.
10
Data Type of an Arithmetic Expression
Example
int * int; result int
int + float; result float
Int + double / float; result double
int – double; result double
Data Type of an Arithmetic Expression
• The data type of the target variable is also important
• If the result is a real number and the target variable is declared as
integer, only the integer part of the result will be kept, and decimal
part will be lost.
The result is calculated as
Example 16.66667
int avg; But avg will be 16
float sum=100.0, cnt = 6.0;
avg = sum / cnt;
12
Data Type of an Arithmetic Expression
float avg; The result of the division will be 16
avg will be 16.0
int sum=100, cnt = 6;
avg = sum / cnt;
• 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
• Increment operator: increment variable by 1
– Pre-increment: ++variable
– Post-increment: variable++
• Decrement operator: decrement variable by 1
– Pre-decrement: --variable
– Post-decrement: variable --
• 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.
• There is a difference between the following
x = 5;
Cout << ++x;
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
> Greater Than
>= Greater Than or Equal To
17
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18