answersLogoWhite

0


Best Answer

All operators must return a value, and it is this return value (the evaluation of the operation) that differentiates the pre-increment and post-increment operators. If the return value is of no concern, then the pre-increment operator is the preferred version, particularly when working with objects. This is because the post-increment operator's implementation will generally make a copy of the original object which it will return after incrementing the original object. If the return value is of no concern, then making a copy adds an expensive overhead (this doesn't apply to primitive data types since CPU optimisation can pre-empt the return value without requiring a copy to be made).

Applications for post-increment and pre-increment are many and varied, but ultimately the goal is to increment the value. Whether you use post-increment or pre-increment is largely down to whether you need to use the return value or not, and whether you need to use the original value or the incremented value.

Although the same thing can be achieved with longhand code, it is more efficient to use the appropriate post-increment or pre-increment operators.

For instance:

int x=1;

int y=x;

++x;

Can be implemented more efficiently with:

int x=1;

int y= x++;

Whereas:

int x=1;

++x;

int y=x;

Can be implemented more efficiently with:

int x=1;

int y= ++x;

Use caution when pre-incrementing the same value in a compound statement. For example:

int x=0;

int y= ++x * ++x;

The expected result is 2, but the actual result is 4. This is because both expressions (++x) return the same value: a reference to x. And since x is incremented twice, the expression evaluates to 2*2, not 1*2.

To get the expected result of 2, each operation must be evaluated separately:

int x=0;

int y= ++x;

y*= ++x;

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are applications of pre-increment and post-increment operators why these operators behaviour are made like this is there any reason for such behaviour?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Do broadcast and cable television operators use long-haul applications?

Broadcast and cable television operators use long-haul applications


Do pipeline industries use long-haul applications?

Pipeline industries operators use long-haul applications


What has the author A Unterberger written?

A. Unterberger has written: 'Pseudo-differential operators and applications'


Is dynamite illegal?

Dynamite is not illegal but the use is permitted only for authorized applications and with skilled operators.


What is the definition of fact family?

A fact family is a set of facts which are appropriate to some subset of numbers. The facts are usually simple applications of arithmetic operators.


What is PHP operators?

PHP Operators are syntactical constructs that assign, compare, or modify a value. There are bitwise operators, arithmetic operators, boolean operators, assignment operators, and concatenation operators. There are also a wide variety of functions and class methods which simulate or utilize these operations.


Do function formulas have operators?

They can have operators. It depends on the formula or function in question. There are lots of kinds of operators that can be used like the mathematical ones, operators for comparisons and boolean operators.


What are the operators in c tokens?

All arithmetic, logical operators are operators in c tokens. As: +, - , ++, --, %, &&, &, >>, << etc.


What are the different types of operators in QBASIC?

The different types of operators in QBASIC are:Arithmetic OperatorsRelational OperatorsLogical Operators


What are the mathematical operators of c?

the mathematical operators of c are.....%,*,/,+,-


What is the common ownership between transport providers and tour operators?

Transport operators and tour operators what is the common ownership


What is a relational operators in c?

Relational operators are those operators which shows relation between two operands. e.g. ==, <=,>=,<,>