简体   繁体   中英

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.

#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.

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 .

One advantage of a for loop is that it bundles the loop controls in a single line. You'd not see the problem with for (i = 0; i < 3; i++) as the loop; the continue would jump to the i++ in the loop control.

You say you are using Eclipse as your 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). That in turn means that no output appears until the buffer fills, you call fflush(stdout) , or the program does stop. It's a known issue with Eclipse. You could call setvbuf() : add a line setvbuf(stdout, NULL, _IOLBUF, BUFSIZ); at the start to ensure line buffering is in effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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