简体   繁体   English

在while循环中使用continue后,代码不会被执行

[英]Code doesn't get excuted after using continue in while loop

I wrote the following code using a complier which was configured to be suitable with c89 standard using eclipse. 我使用一个编译器编写了以下代码,该编译器被配置为适合使用eclipse的c89标准。

#include <stdio.h>

int main(void) {
    int i=0;
    printf("initial value for variable i is: %d\n",i);
    while (i<3)
    {
        if (i==1) {continue;}
        printf("%d\n",i);
        i++;
    }
    return 0;
}

The problem is that the code doesn't get excuted (No errors at all) and nothing shows in the console. 问题在于代码不会被执行(根本没有错误),并且控制台中没有任何显示。 When removing the following line of code if (i==1) {continue;} everything works correctly. 当删除以下代码行时, if (i==1) {continue;}一切正常。

When i is 1 , it doesn't get its value changed, so it is still 1 on the next iteration, and the next, and … it takes a long time to change from 1 . i1 ,它的值不会改变,因此在下一次迭代和下一次迭代时仍为1 ,并且...从1改变要花很长时间。

One advantage of a for loop is that it bundles the loop controls in a single line. for循环的一个优点是它将循环控件捆绑在一行中。 You'd not see the problem with for (i = 0; i < 3; i++) as the loop; 您不会看到for (i = 0; i < 3; i++)作为循环的问题; the continue would jump to the i++ in the loop control. continue将跳转到循环控件中的i++

You say you are using Eclipse as your IDE. 您说您正在使用Eclipse作为您的IDE。 That's probably why no output appears at all. 这可能就是为什么根本没有输出的原因。 Its "terminals" appear to the programs as a non-terminal, so I/O is fully buffered (rather than line buffered as is normally the case with terminal output). 它的“终端”在程序中显示为非终端,因此I / O被完全缓冲(而不是像终端输出那样通常被行缓冲)。 That in turn means that no output appears until the buffer fills, you call fflush(stdout) , or the program does stop. 反过来,这意味着在缓冲区填满,调用fflush(stdout)或程序停止之前,不会出现任何输出。 It's a known issue with Eclipse. 这是Eclipse的一个已知问题。 You could call setvbuf() : add a line setvbuf(stdout, NULL, _IOLBUF, BUFSIZ); 您可以调用setvbuf() :添加一行setvbuf(stdout, NULL, _IOLBUF, BUFSIZ); at the start to ensure line buffering is in effect. 首先确保行缓冲有效。

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

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