1
#include<stdio.h>
int main()
{
 int i=0;
 for(;;)
 {
   if(i==10)
   {
     continue;
   }
   printf("%d",++i);
 } 
}

This above code makes the output as without print anything.But by my logic it prints the value from 1 to infinite times except 10.The continue statement doesn't run the below code after continue statement but the continue statement is inside the if condition then why it shouldn't run the statement that are below the continue statement?

SARAN V
  • 13
  • 3
  • Fyi, `fflush(stdout);` in case you're wondering where your output is. Line buffering an all. Regarding the loop, once `i` is 10, the continue will skip the only thing to ever change that state. therefore it will always be 10, will always continue, and never exit: e.g. an infinite loop. If you move the `++i` to `if(++i == 10)` and change the printf to just use `i` you'll probably get the operation you seek. – WhozCraig Aug 26 '22 at 17:54
  • This would be a very good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. For example how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code statement by statement while monitoring variables and their values. Though in this case a simple [*rubber duck debugging*](https://en.wikipedia.org/wiki/Rubber_duck_debugging) might be enough. – Some programmer dude Aug 26 '22 at 18:02
  • what would you expect in this case? use 'break' instead of 'continue'. – Serge Aug 27 '22 at 01:45

1 Answers1

1
  1. The output of C-programs is normally buffered, which is likely the reason why you don't see anything. Try printf("%d\n", ++i) as the linefeed will flush the buffer.
  2. Once i reaches 10, the number will not be increased any more, so your expectation that "it prints the value from 1 to infinite times except 10" will not be met.
treuss
  • 1,913
  • 1
  • 15