C++中while(x--)是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32649032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:11:09  来源:igfitidea点击:

What does while(x--) mean in C++

c++loops

提问by Anuj Saharan

I just started with competitive programming and have been using a loop like the one below to define the number of test cases in most of the practice problems.

我刚刚开始使用竞争性编程,并一直使用如下所示的循环来定义大多数练习问题中的测试用例数量。

for(int i=1;i<=t;i++)
{
...
}

However, I have seen people using a while loop which just has the condition (t--)which runs perfectly okay too. Can someone explain to me how this condition actually works? Is it the same as while(t!=0), but instead of a decrement in the value of later in the loop, we are doing it here?

但是,我见过有人使用 while 循环,它的条件 (t--)也运行得很好。有人可以向我解释这种情况实际上是如何工作的吗?它是否与 相同while(t!=0),但不是在循环中减少后面的值,而是在这里进行?

回答by Gombat

Yes.

是的。

int t = 5;
while(t--){...}

runs the loop until tis 0during checking it's value, since 0is equally to falsein a logical context.

运行循环,直到t0检查它的价值时,因为0同样是false在一个合理的范围内。

Because t--is the post decremental operator on t, the value of it will be modified after the whilechecks it, so it will be -1 in the end. Therefore, the values of tinside the loop will be: 4,3,2,1,0.

因为t--是 on 的后递减运算符twhile检查后会修改其值,所以最后为-1。因此,t循环内部的值将是:4,3,2,1,0。

But it creates an additional variable outside the loop scope - so I do prefer the forloop.

但它在循环范围之外创建了一个额外的变量 - 所以我更喜欢for循环。

回答by SingerOfTheFall

The body of the whileloop is executed repeatedly until the value of its condition becomes false (note that if the condition is initially false, the loop will not execute at all). The condition test takes place before each execution of the loop's body. Now, let's say you have this:

while循环体反复执行,直到其条件的值变为假(注意,如果条件最初为假,则循环根本不会执行)。条件测试在每次执行循环体之前进行。现在,假设你有这个:

int x = 5;
while(x--)
{
    cout << x;
}

Each iteration the condition of the while loop will be evaluated and checked for false. Since we use postfix decrement, xwill first be used (i.e. checked for falseto break the loop), and then decreased, which means the loop above will print 43210, and then finish.

每次迭代都会评估和检查 while 循环的条件false。由于我们使用postfix decrement,所以x会先被使用(即check forfalse来打破循环),然后再减少,也就是说上面的循环会打印43210,然后结束。

It's worth noting that after the loop finishes, the value of xwill be -1.

值得注意的是,循环结束后,的值为x-1。

回答by idclev 463035818

If there is no reason to do otherwise, I would alwayswrite a loop like this:

如果没有理由不这样做,我总是会写一个这样的循环:

const int tmax = 3;
for (int t=0;t<tmax;t++){std::cout << t << std::endl;}

If you only care about how many times the loop is executed but you do not need the value of tinside the loop, there is actually no reason to use something else. However, if you want to count down, you could do this:

如果你只关心循环执行了多少次而你不需要t循环内部的值,那么实际上没有理由使用其他东西。但是,如果你想倒计时,你可以这样做:

for (int t=tmax;t!=0;t--){std::cout << t << std::endl;}

or

或者

for (int t=0;t<tmax;t++){std::cout << tmax - t << std::endl;}

which one may consider both as ugly (I personally do not like the first too much, because I am a devotee of counting up in for loops whenever possible. The second one isnt nice because you might use the value tmax-tseveral times inside the loop, i.e. either you introduce an extra varible or the opportunity for bugs) and prefer to write

哪个可能认为两者都丑陋(我个人不太喜欢第一个,因为我是一个尽可能在 for 循环中计数的爱好者。第二个不太好,因为您可能会tmax-t在循环内多次使用该值,即要么你引入了一个额外的变量,要么有错误的机会)并且更喜欢写

int t = tmax;
while (t--){std::cout << t << std::endl;}

which has the little disadantage of introducing an extra variable outside of the loop. However, when tmaxisnt const and the value isnt needed after the loop, probably the most compact way to write the loop is this:

它具有在循环外引入额外变量的小缺点。但是,当tmax不是 const 并且循环后不需要该值时,编写循环的最紧凑方法可能是这样的:

while (tmax--){std::cout << tmax << std::endl;}

I am pretty sure they are all the same in performance and it is just about your prefered coding style, but I hope I could give you a hint, why someone would prefer one over the other.

我很确定它们在性能上都是一样的,这只是关于你喜欢的编码风格,但我希望我能给你一个提示,为什么有人会更喜欢一个。

回答by Andreas DM

while(x--)stops after the result of x--has been evaluated to false/ 0

while(x--)在结果x--被评估为false/后停止0

int x = 5;
while (x--) { 
    ;// do something 
}

Because of the post decrement, x has value -1after the loop has finished.

由于后递减,-1循环完成后x 具有值。

回答by pulkit

while(x--) 

This loop will run 'x' times, and since x-- is post-decrement operator, it will first check value of x to be non-zero and decrement it afterwards.

这个循环将运行 'x' 次,并且由于 x-- 是后递减运算符,它将首先检查 x 的值是否为非零,然后将其递减。

So if you try printing value of x inside the loop, they will be, x-1 x-2 . . 0

因此,如果您尝试在循环内打印 x 的值,它们将是 x-1 x-2 。. 0

回答by dj1986

There are 2 ways to traverse from 0 to 9.

从 0 到 9 有两种遍历方式。

first - 0 1 2 3 4 5 6 7 8 9 to traverse in first way you need to

first - 0 1 2 3 4 5 6 7 8 9 要以第一种方式遍历,您需要

for(x=0; x !=9; x++){
    cout << x;
}

second - 9 8 7 6 5 4 3 2 1 0 to traverse in second way you need this loop

second - 9 8 7 6 5 4 3 2 1 0 以第二种方式遍历你需要这个循环

for(x=9; x !=0; x--){
    cout << x;
}
  • x++ is post increment. It will increase value of x by 1 after using current value. It is same as x = x + 1
  • x-- is post decrement. It will decrease value of x by 1 after using current value It is same as x = x - 1
  • x++ 是后增量。使用当前值后,它会将 x 的值增加 1。它与 x = x + 1 相同
  • x-- 是后减量。使用当前值后,x 的值会减少 1 与 x = x - 1 相同