为什么"对于(i = 100; I< = 0; --i)QUOT;永远循环下去? [英] Why does "for (i = 100; i <= 0; --i)" loop forever?

查看:181
本文介绍了为什么"对于(i = 100; I< = 0; --i)QUOT;永远循环下去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 无符号整型我;
为(ⅰ= 100; I&下; = 0; --i)
    的printf(%d个\\ N,I);


解决方案

I> = 0 在第二次循环条件下,如果你想让它循环从100到0

这,和其他人指出,你需要改变你的的定义我来有符号整数(只 INT ),因为当计数器意思是-1,这将是一些其他的正数,因为你宣布它的 unsigned int类型

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

解决方案

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.

这篇关于为什么&QUOT;对于(i = 100; I&LT; = 0; --i)QUOT;永远循环下去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆