为什么“for (i = 100; i <= 0; --i)”永远循环?

发布于 2024-10-13 19:11:19 字数 92 浏览 6 评论 0 原文

unsigned int i;
for (i = 100; i <= 0; --i)
    printf("%d\n",i);
unsigned int i;
for (i = 100; i <= 0; --i)
    printf("%d\n",i);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

落墨 2024-10-20 19:11:20

也许<= 0?因为从一开始它就是 false

For 循环

  • init: i = 100
  • test: i <= 0 // false on first pass

将测试更改为 i >; 0(100 次)

i >= 0(101 次)与声明 signed int i; 一起,使其实际上减少到 -1 。无符号 int 将从 0 到 max-int(溢出)。

The <= 0 maybe? since it is false from the start

For loop

  • init: i = 100
  • test: i <= 0 // false on first pass

Change the test to i > 0 (100 times)

or i >= 0 (101 times) together with the declaration signed int i; so that it actually decreases down to -1. An unsigned int will go from 0 up to max-int (overflow).

情话难免假 2024-10-20 19:11:20

如果你希望它打印从 100 到 0 的所有数字,那么你需要

unsigned int i;
for (i = 100; i >= 0; --i)
    printf("%d\n",i);

在原始代码中第一次运行循环时,i 是 100。测试“100 <= 0”失败,因此没有显示任何内容。

If you want it to print all numbers from 100 down to 0 then you need

unsigned int i;
for (i = 100; i >= 0; --i)
    printf("%d\n",i);

The first time your loop ran in your original code, i was 100. The test '100 <= 0' failed and therefore nothing was showing.

谁与争疯 2024-10-20 19:11:19

如果你希望它从 100 循环到 0,则在循环的第二个条件中应该是 i >= 0

正如其他人指出的那样,你需要更改你的定义i 为有符号整数(只是 int),因为当计数器为 -1 时,它将是其他正数,因为您将其声明为 无符号int

Should be i >= 0 in the second condition in the loop if you want it to loop from 100 to 0.

That, and as others have pointed out, you'll need to change your definition of i to a signed integer (just int) because when the counter is meant to be -1, it will be some other positive number because you declared it an unsigned int.

夏了南城 2024-10-20 19:11:19

由于 i 是无符号的,因此它永远不会小于零。删除未签名。另外,将 <= 替换为 >=

Since i is unsigned, it will never be less than zero. Drop unsigned. Also, swap the <= for >=.

断念 2024-10-20 19:11:19

由于 i 是无符号的,因此表达式 i <= 0 是可疑的,并且等价于 i == 0

并且代码不会打印任何内容,因为条件 i <= 0 在第一次计算时为 false。

Since i is unsigned, the expression i <= 0 is suspicious and equivalent to i == 0.

And the code won't print anything, since the condition i <= 0 is false on its very first evaluation.

葬心 2024-10-20 19:11:19

如果代码不执行任何操作,那么它就没有问题。

假设您希望它打印循环索引i从100到1,您需要将i <= 0更改为i > 0

因为它是一个无符号整数,所以不能使用 i >= 0 因为这会导致它无限循环。

If the code is supposed to do nothing, nothing is wrong with it.

Assuming that you want it to print the loop index i from 100 to 1, you need to change i <= 0 to i > 0.

Because it is an unsigned int, you cant use i >= 0 because that will cause it to infinitely loop.

卖梦商人 2024-10-20 19:11:19

循环检查 i <= 0;

i 永远不会小于或等于零。其初始值为100。

The loop checks for i <= 0;

i is never less-than-or-equal to zero. Its initial value is 100.

瑶笙 2024-10-20 19:11:19

从技术上讲,该代码没有任何问题。 i <= 0 的测试很奇怪,因为 i 是无符号的,但它在技术上是有效的,当 i 为 0 时为 true,否则为 false。在你的情况下,我永远不会碰巧是0。

Technically nothing is wrong with that code. The test for i <= 0 is strange since i is unsigned, but it's technically valid, true when i is 0 and false otherwise. In your case i never happens to be 0.

或十年 2024-10-20 19:11:19

我怀疑你的意思是测试是 i > 0

I suspect you meant the test to be i > 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文