[英]c++ for loop post-increment pre-increment difference
i've been searching for an answer, but i get more and more confused. 我一直在寻找答案,但我越来越困惑。
i have these 2 for loops 我有这两个for循环
for (int i = 1; (i < 5) && move.from[i - 1]; i++) {
int const departurePoint = move.from[i - 1];
int arrivalPoint = move.to[i - 1];
if (arrivalPoint < 0) { // A blot was hit
arrivalPoint = -arrivalPoint;
board[1 - turn][BAR - arrivalPoint]--; // Remove the blot
board[1 - turn][BAR]++; // and place it on the bar */
}
board[turn][departurePoint]--; // Move our own checker
board[turn][arrivalPoint]++; // to it's landing spot.
}
and 和
for (int i = 1; (i < 5) && move.from[i - 1]; ++i) {
int const departurePoint = move.from[i - 1];
int arrivalPoint = move.to[i - 1];
if (arrivalPoint < 0) { // We hit a blot
arrivalPoint = -arrivalPoint;
board[1 - turn][BAR - arrivalPoint]++; // Replace the blot
board[1 - turn][BAR]--; // remove it from the bar
}
board[turn][departurePoint]++; // Replace our own checker
board[turn][arrivalPoint]--; // to it's original spot.
}
my questions are: 我的问题是:
Your short question is What is the difference between i++
and ++i
is the value of the expression? 您的简短问题是
i++
和++i
和表达式的值之间有什么区别?
The value i++
is the value of i
before the increment . 值
i++
是的值i
增量之前 。 The value of ++i
is the value of i
after the increment . 的值
++i
是的值i
增量之后 。
Example: 例:
int i = 2;
std::cout << i++ << std::cout; // shows 2
std::cout << i << std::cout; // shows 3
i = 2;
std::cout << ++i << std::cout; // shows 3
std::cout << i << std::cout; // shows 3
The i--
and --i
operators works the same way. i--
和--i
运算符的工作方式相同。
for (int i = 1; (i < 5) && move.from[i - 1]; i++ /*i increments here and nowhere else*/)
and 和
for (int i = 1; (i < 5) && move.from[i - 1]; ++i /*i increments here and nowhere else*/)
Both codes are equivalent. 这两个代码是等效的。 The difference is very slight and it does not apply to this example.
差异很小,不适用于本示例。
when i==3
, 当
i==3
时
++i
means: 4=i+1=(++i)
then i=4
. ++i
意思是: 4=i+1=(++i)
然后i=4
。
i++
means: 3= i =(i++)
then i=4
. i++
意思是: 3= i =(i++)
然后i=4
。
it does not make a difference until you assign it to another variable: 除非您将其分配给另一个变量,否则它不会有所不同:
for(...; ...; k=i++)
or 要么
for(...; ...; k=++i)
i+1
means: i+1
表示:
store i
to a temporary variable. 将
i
存储到一个临时变量。 Increase the temporary variable by one. 将临时变量增加一。 It reads
i
but does not write to i
. 它读取
i
但不写入i
。 i
will change only on ++
, --
or i=
and a few other cases. i
只会在++
, --
或i=
以及其他一些情况下进行更改。
The for loop takes two statements and an expresion like this: for循环需要两个语句和一个这样的表达式:
for(init_statement;condition_expresion;progress_statement){
//...body...
}
The semantic of this instruction is basically the same as writing: 该指令的语义与编写基本相同:
init_statement;
while(condition_expresion){
//...body...
progress_statement;
}
Notice the side effect of both i++
and ++i
is the same, the variable i
is incremented by one. 注意,
i++
和++i
的副作用是相同的,变量i
加1。 Those two instructions are different when they are used as expresions, i++
evaluates to the value of i
before the increment, and ++i
evaluates to the value after the increment. 当它们被用作expresions这两个指令是不同的,
i++
计算结果为的值i
增量之前,和++i
的计算结果为增量后的值。 This is all irrelevant for the for loop, as the value of the progression statement is discarded. 这与for循环无关,因为已删除了progress语句的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.