Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
i = 5
x = i++
print(x)
>> 5
i = 5
x = ++i
print(x)
>> 6
So it depends on how you want to increment the number. -
@Deserter Yes, because one takes effect already while the other only after the end of the iteration loop/function/scope where the increment is.
But what the OP is talking about is more applicable in cases of for loops, where both yield the same results but if the compiler does not optimize it using ++i is marginally faster. -
@Deserter yea ofcouse there is a use case when you want i++ but I meant when you can use both, use ++i because it can be slightly faster :)
-
@karthik2502 I even thought when i pressed 'Post': 'I should have checked his profile first' hahaha.
But your welcome :) -
@SISheogorath i am going to fuck things up if I start doing that haha
Also my coworkers are going to hate me 😂 -
GrosTon1167y++i does work on JavaScript but not on mobile, i've been stuck for 1 hour this week because of this.
-
@Deserter I think you mean print(++i) and print(i++) because in the code you gave I is always 6.
Related Rants
-
crafter18I freaking love optimizing multithreaded applications.
-
benhongh4Spent 2 days carefully crafting a highly optimised routine, then spent 3 minutes to write an unoptimised versi...
-
athlon11Random fact #0 Back in the days of SEGA Saturn, SEGA was really picky in terms of the game stability. All the...
Fun/Interesting fact:
"++i" can be slightly faster than "i++"
Because "i++" can require a local copy of the value of "i" before it gets incremented, while "++i" never does. In some cases, some compilers will optimize it away if possible... but it's not always possible, and not all compilers do this.
undefined
optimisation
fun facts
i++
++i
interesting facts