To increment or decrement the value of a variable while executing loops,why do we use the postfix operator....why not prefix?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

To increment or decrement the value of a variable while executing loops,why do we use the postfix operator....why not prefix??

19th Jun 2017, 7:23 PM
Glamorous
Glamorous - avatar
6 Answers
+ 2
it doesn't matter in most cases and postfix version is older
19th Jun 2017, 7:29 PM
alex
alex - avatar
+ 2
okay...thanks
19th Jun 2017, 7:29 PM
Glamorous
Glamorous - avatar
+ 1
I use prefix unless there a reason not to. Normally, the compiler will optimize both to the same performance, but logically postfix uses more steps. Compare how a naive C++ compiler could implement it: int naivePrefix(int& x) { x = x + 1; return x; } int naivePostfix(int& x) { int y = x; x = x + 1; return y; } [edit] fixed. post and pre were swapped.
19th Jun 2017, 7:37 PM
Igor B
Igor B - avatar
+ 1
we can use both. its depend on coder how he/she want to use
19th Jun 2017, 7:37 PM
MD. WOALID HOSSAIN
MD. WOALID HOSSAIN - avatar
0
any of them can be used
4th Jul 2017, 11:09 AM
Glamorous
Glamorous - avatar
- 1
Better use prefix as it runs before compilation..
23rd Jun 2017, 4:54 PM
Bheeshma
Bheeshma - avatar