简体   繁体   English

为什么in中的int数(数字> 10)会产生无限循环?

[英]Why does an int number in do (while number > 10) gives an endless loop?

In my understandings, the while should end the loop, cause int number "in" the loop should initialize it with 0. Or am i wrong? 以我的理解,while应该结束循环,导致int数“ in”在循环中将其初始化为0。还是我错了? But it gives me an endless loop, printing zeroes. 但这给了我一个无限循环,打印零。 I compiled it with gcc and tried to debug it with gdb. 我用gcc编译了它,并尝试用gdb调试它。 It makes no sense for me, that the while doesn't stop the loop. 对我来说,那一刻不会停止循环是没有意义的。

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int number = 20;
    do
    {
        int number = number / 2;
        printf("%d ", number);
    } while (number > 10);
}

You create new number every time in the loop, visible only within the loop and unrelated to the one outside the loop. 每次在循环中创建一个新number ,该number仅在循环中可见,而与循环外的数字无关。 Remove int in int number = number / 2; int number = number / 2;删除int int number = number / 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM