This question already has answers here:
for loop - statement with no effect
                                
                                    (5个答案)
                                
                        
                                6个月前关闭。
            
                    
for循环无限运行,当我运行以下代码时,它无限打印1。

#include<stdio.h>
#include<conio.h>
void main(){
int i,n;
scanf("%d", &n);
for(i=1;i<=n;i+2){
printf("%d",i);
}
getch();
}


如果输入n = 10

实际输出:

11111111111111111111111111111111111111111111111111.......


预期产量:

13579


我想知道为什么无限打印1。

最佳答案

i+2不会更改i,您需要i += 2i = i + 2

关于c - For循环为什么无限运行? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57753459/

10-11 23:13
查看更多