在C ++中递增 - 何时使用x ++或++ x? [英] Incrementing in C++ - When to use x++ or ++x?
问题描述
我目前正在学习C ++,我已经学会了增量。
我知道你可以使用++ x使之前的增量和x ++做后。
I'm currently learning C++ and I've learned about the incrementation a while ago. I know that you can use "++x" to make the incrementation before and "x++" to do it after.
我知道什么时候使用两个...我从来没有真正使用过++ x和东西总是工作正常到目前为止 - 所以,我应该什么时候使用?
Still, I really don't know when to use either of the two... I've never really used "++x" and things always worked fine so far - so, when should I use it?
例如:在for循环中,什么时候最好使用++ x?
Example: In a for loop, when is it preferable to use "++x"?
递减)工作?
推荐答案
这不是偏好问题,而是逻辑问题。
It's not a question of preference, but of logic.
x ++
在之后增加变量x的值。
x++
increments the value of variable x after processing the current statement.
++ x
在处理当前语句之前增加变量x 的值。
++x
increments the value of variable x before processing the current statement.
所以只要决定你写的逻辑。
So just decide on the logic you write.
x + = ++ i
i并将i + 1添加到x。
x + = i ++
会将i添加到x,然后增加i。
x += ++i
will increment i and add i+1 to x.
x += i++
will add i to x, then increment i.
这篇关于在C ++中递增 - 何时使用x ++或++ x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!