[英]C programming, Is this an infinite loop?
What would happen if the follow lines were a part of a compiled program?如果以下几行是编译程序的一部分,会发生什么?
void main() {
int x = 5;
while (x == 5);
}//end main
I believe it is, I have compiled it, and the screen just stays the same, I also tried to add after the while statement我相信是的,我已经编译了它,并且屏幕保持不变,我也尝试在while语句后添加
int y = 10;
printf("%i", y);
and then end main.然后结束主。 However it never prints.
但是它从不打印。 So I am fairly certain that its an infinite loop but I would just like to be sure.
所以我相当确定它是一个无限循环,但我只想确定。
是的,这是一个无限循环,因为永远不会满足退出循环的条件:因为变量 x 在循环内永远不会改变,循环的条件将始终为真。
Yes, it's an infinite loop.是的,这是一个无限循环。 Remember, while the condition is true the while loop will continue.
请记住,当条件为真时,while 循环将继续。 As 5 will always be equal to 5 the condition will always be true, even if you don't do anything inside the while loop.
由于 5 将始终等于 5,因此条件将始终为真,即使您不在 while 循环中执行任何操作。
Yes it is, but it could actually leave the loop in some scenarios:是的,但在某些情况下它实际上可能会离开循环:
There must be other cases where the loop can end, but those are the ones I could think of.肯定还有其他情况可以结束循环,但我能想到的就是这些。
If you're interested, you can try to do so with Cheat Engine to change the value of x.如果您有兴趣,可以尝试使用 Cheat Engine 更改 x 的值。
try:尝试:
void main() {
int x = 5;
while (x == 5){
printf("%d\n", x);
}
}
Use format %d for doubles对双打使用格式 %d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.