Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Programming C++ Secations three

The document explains arithmetic operations in C++, including the use of parentheses and operator precedence. It details how the data type of an arithmetic expression is determined by its operands and provides examples of increment and decrement operators, as well as compound assignment statements. The document emphasizes the importance of understanding these concepts for proper expression evaluation in C++.

Uploaded by

moathjubouri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programming C++ Secations three

The document explains arithmetic operations in C++, including the use of parentheses and operator precedence. It details how the data type of an arithmetic expression is determined by its operands and provides examples of increment and decrement operators, as well as compound assignment statements. The document emphasizes the importance of understanding these concepts for proper expression evaluation in C++.

Uploaded by

moathjubouri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Arithmetic Operations

Parentheses are used in C++ expressions in the same manner as in algebraic expressions.

For example, to multiply a times the quantity b + c

we write a * ( b + c ).
There is no arithmetic operator for exponentiation in C++,
so x2 is represented as x * x.
34
Precedence of arithmetic operations

For example,

2 + 3 * 5 and (2 + 3) * 5

both have different meanings


35
Precedence of arithmetic operations
Example :

36
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) + and the result is 11


?=5*2%(7 4)

Evaluated as % and the result is 1

37
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.

38
Data Type of an Arithmetic Expression
Example
int * int; result int

int + float; result float

Int + double / float; result double

int double; result double

39
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

43
Increment and Decrement Operators

x = 5;
Cout << ++x;

x = 5;
44
Cout << x++;
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;

45

You might also like