在阅读了C++/STL的C++/STL的隐藏特征和暗角(comp.lang.c++.modated)之后,我感到非常惊讶的是,以下的片段在2008年视觉工作室和G++4.4编辑和工作。

代码如下:

#include <stdio.h>
int main()
{
    int x = 10;
    while (x --> 0) // x goes to 0
    {
        printf("%d ", x);
    }
}

产出:

9 8 7 6 5 4 3 2 1 0

我想这是C,因为它在海合会也是工作。标准中的定义在哪里,它来自何处?


当前回答

x 可以在 C++ 的相反方向更快地达到零:

int x = 10;

while( 0 <---- x )
{
   printf("%d ", x);
}

8 8 6 4 2

你可以用箭来控制速度!

int x = 100;

while( 0 <-------------------- x )
{
   printf("%d ", x);
}

90 90 80 70 60 50 40 30 10

; ; ;)

其他回答

和这个完全一样

while (x--)

在 -- 和 >. x 之间缺少一个空白。 x 在检查条件 x>0 后, 出现衰减 。 @ action: inmenu

或者是完全不同的东西... x 幻灯片到 0 。

while (x --\
            \
             \
              \
               > 0)
     printf("%d ", x);

不那么数学,但是... 每幅画画上写着一千个字...

我们用常规的方式定义状态, 而循环括号“ () ) ” 和牙套“\\\\\\\\\\\\\\\\\\\ ” 中的终止状态, 但是-- & > 是一个同时定义所有状态的方法。 例如 :

int abc(){
    int a = 5
    while((a--) > 0){ // Decrement and comparison both at once
        // Code
    }
}

上面写着, 降一个, 并运行回环, 直到时间 a 大于 0 。

换句话说,它本该是这样的:

int abc() {
    int a = 5;
    while(a > 0) {
        a = a -1 // Decrement inside loop
        // Code
    }
}

无论是哪种方式,我们都做同样的事情,实现同样的目标。

相当于

while (x-- > 0)

x- (后衰减) 等于 x = x-1 (但返回X 的原值) , 所以代码转换为 :

while(x > 0) {
    x = x-1;
    // logic
}
x--;   // The post decrement done when x <= 0