[英]Increment order in a while loop
Here I have a simple algorithm that computes the square of a number by adding up all of the odd numbers equal to the inputted number, such that: 在这里,我有一个简单的算法,通过将等于输入数字的所有奇数加起来来计算数字的平方,这样:
1 = 1 = 1-squared
1 + 3 = 4 = 2-squared
1 + 3 + 5 = 9 = 3-squared
and so forth. 等等。 The algorithm is as follows:
算法如下:
int triangleSquare(int number) {
int product = 0;
int currentOdd = 1;
while (number--) {
product += currentOdd;
currentOdd += 2;
}
return product;
}
My question is about the segment: 我的问题是关于细分市场的:
while (number--) { ... }
For some reason I get different results between while (--number)
and while (number--)
, which I assume is because --number
should decrement first before performing the while loop (and vice-versa for number--
). 由于某些原因,我在
while (--number)
和while (number--)
之间得到了不同的结果,我认为这是因为--number
在执行while循环之前应先递减(反之亦然,对于number--
)。 My question is, why does it make a difference? 我的问题是,为什么会有所作为? Shouldn't the while loop always execute last, regardless of the increment order?
无论递增顺序如何,while循环都不应总是最后执行吗? I must be missing something obvious, but that seems very odd to me, almost as though
number--
makes the conditional behave like a do {...} while()
loop. 我一定是失去了一些东西很明显,但似乎很奇怪对我来说,几乎就像
number--
使有条件的行为像一个do {...} while()
循环。
The two candidate loops could be written (are equivalent to): 可以编写两个候选循环(等效于):
while (number-- != 0)
while (--number != 0)
Consider what happens when number == 1
at loop entry: 考虑当循环入口处的
number == 1
时会发生什么:
number
is compared to zero and then decremented; number
的值与零进行比较,然后递减; since 1
is not 0
, the loop body is executed. 1
不为0
,因此执行循环体。 number
is decremented and then compared to zero; number
的值递减,然后与零比较; since 0
is 0
, the loop body is not executed. 0
为0
,因此不执行循环体。 That's the difference! 就是这样!
while (number--)
does the following: while (number--)
执行以下操作:
while (number)
while (number)
number--
number--
On the other hand while (--number)
does this: 另一方面
while (--number)
这样做:
--number
--number
while (number)
with the new value while (number)
And that's the difference. 就是这样。
This is a horrible way to write code, but... 这是编写代码的可怕方式,但是...
An expression has two characteristics: a return value, and side effects. 表达式具有两个特征:返回值和副作用。 The decrementation is the side effect;
递减是副作用; it is the same for
--number
and number--
. --number
和number--
相同。 The condition in the while
, however, depends on the return value; 然而,
while
的条件取决于返回值。 --number
returns the value after the decrementation; --number
返回减量后的值; number--
the value before the decrementation. number--
减量前的值。 When the decrementation actually takes place on the variable is not really specified (for either): some time after the preceding sequence point and before the following one. 当实际对变量进行减量运算时,并没有真正指定(对于任一变量):在前一个序列点之后和下一个序列点之前的某个时间。
In other words: --number
is the equivalent of: 换句话说:
--number
等于:
int
decr( int& number )
{
number -= 1;
int results = number;
return results;
}
and number--
is the equivalent of: 和
number--
是等价的:
int
decr( int& number )
{
int results = number;
number -= 1;
return return;
}
In both cases, you're testing the return value of the function. 在这两种情况下,您都在测试函数的返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.