answersLogoWhite

0

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

13y ago

What else can I help you with?

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'


What are the applications of exponentials of operators in quantum mechanics?

Exponentials of operators in quantum mechanics are used to describe the time evolution of quantum systems. They are important in solving the Schrdinger equation and understanding how quantum states change over time. These exponential operators help calculate probabilities, predict outcomes of measurements, and model the behavior of particles in quantum systems.


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 operaters used in VBNET?

In VB.NET, operators are used to perform operations on variables and values. The main categories of operators include arithmetic operators (e.g., +, -, *, /, Mod), relational operators (e.g., =, <>, <, >, <=, >=), logical operators (e.g., And, Or, Not), and bitwise operators (e.g., And, Or, Xor, Not). Additionally, VB.NET includes assignment operators (e.g., =, +=, -=) and concatenation operators (e.g., & for strings). These operators enable developers to manipulate data and control program flow effectively.


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 a relational operators in c?

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