can anybody help me? i am new in c++.please..
++
--
*=
+=
-=

Dani AI

Generated

A compact, practical follow-up to what , and started: the listed symbols are increment/decrement and compound-assignment operators, but a few extra details and cautions are important when you actually use them.

Pre vs post: ++x (pre) performs the change and yields the new value; x++ (post) yields the old value and then performs the change. For built-ins the difference is just which value the expression produces; for user-defined types x++ typically makes a copy of the old state, so ++x is frequently more efficient (this matters especially for iterators).

int i = 1;
int a = ++i; // i == 2, a == 2
int j = 1;
int b = j++; // j == 2, b == 1

Compound-assignment (e.g. +=, -=, *=) is roughly x = x op y but with two important practical differences: the left operand is evaluated only once, and conversion rules can differ, so it can avoid extra temporaries and repeated evaluations. Avoid expressions that both modify and read the same scalar more than once in a single expression (for example, i = i++) — such code is unsequenced/undefined; see the language evaluation/ordering rules for details: evaluation order (sequencing rules).

int x = 5;
x += 3; // x == 8

Quick checklist:

  • Prefer ++it over it++ for iterators to avoid copies.
  • Use compound assignment to reduce temporaries and repeated evaluation.
  • Don’t write expressions that modify a variable more than once in the same full-expression.
  • You can overload these operators for classes; idiomatically implement pre-increment as X& operator++() and post-increment as X operator++(int).

Recommended Answers

All 6 Replies

sounds like a fun homework assignment.

They are mathematical operators. Add one to the value of current variable and store the new value in current variable. Subtract one from current value of variable and store new value in current variable. Multiple the current variable value with the right hand side of the operator and store the product in the current variable, etc.


While you're at it, don't forget /= and %=

sounds like a fun homework assignment.

thank you so much.it helps me.

sorry.wrong comment before.actually,thanks for your comment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.