-6

The continue statement is for skipping the iteration but it doesn’t work as I expect. Here is my code:

int i = 0;
do
{
  if(i== 10)continue;
  printf("\n This is = %d",i);
  
  i++;
} while (i<20);

My understanding is that it will skip just

This is = 10

and rest it will print but it is skipping since

This is = 9 till 19

And here is the output

 This is = 0
 This is = 1
 This is = 2
 This is = 3
 This is = 4
 This is = 5
 This is = 6
 This is = 7
 This is = 8

why?

Also when I added for loop ahead of this loop that is entirely getting ignored and not getting executed?

int i = 0;
do
{
  if(i== 10)continue;
  printf("\n This is = %d",i);
  
  i++;
} while (i<20);

for(int a = 0 ; a<20 ; a++)
{
    if(a==10)continue;
    printf("\nHere = %d",a);
}

Output :

 This is = 0
 This is = 1
 This is = 2
 This is = 3
 This is = 4
 This is = 5
 This is = 6
 This is = 7
 This is = 8

Why? Please explain.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    How do you imagine that `i` becomes 11? – mkrieger1 Aug 19 '23 at 08:41
  • 3
    When `i` is 10, the loop continues before it gets incremented so it'll always be 10, making an infinite loop. – Shawn Aug 19 '23 at 08:42
  • 2
    See [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – mkrieger1 Aug 19 '23 at 08:43
  • 1
    Explain the code in great detail to a [rubber duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – Some programmer dude Aug 19 '23 at 08:44
  • Regarding the `for` loop, it's *after* the old loop. Not "ahead of". And if the old loop is infinite, then the program simply won't come to the new `for` loop you added. – Some programmer dude Aug 19 '23 at 08:47
  • I didn't get how this loop is getting infinite, also till 9 it should print? – user3737377 Aug 19 '23 at 08:48
  • 2
    Second lesson for today: When printing output, print with a ***trailing*** newline. The `printf` function will write to `stdout`. When running in an interactive terminal `stdout` is *line buffered*. That means the output is flushed (actually written) when you print a newline. When you print a leading newline, you flush (actually write) the *previous* line, not the current text. So change all your `printf("\n anything")` calls to `printf("anything\n")`. – Some programmer dude Aug 19 '23 at 08:51
  • Thanks everyone, I think I got the answer, I have placed i++ before while and continue statement will bring the compiler after i++ thus value is not getting incremented and remaining same thus it is infinite. But got one query still that why 9 didn't got printed? – user3737377 Aug 19 '23 at 08:52
  • And if you just want to skip printing `10`, then change the condition to print every value except `10`. So e.g. `if (i != 10) { printf(...); }` – Some programmer dude Aug 19 '23 at 08:53

2 Answers2

2

There are a couple of things going on here. First, The printf format string begins with a newline, but doesn't end with one. That's backwards from what is usually done. But because each printed string doesn't end in a newline, it is most likely being buffered until the next newline. So in fact, it's printing " This is = 9", but it isn't actually going through until the next newline, which never comes.

The reason the next newline never comes is because, once i is 10, it goes into an infinite loop. Each time through, it does the comparison i == 10, which is true, so it immediately continues the loop, never executing the i++; increment. In the latter example, the for loop is never reached since it's stuck in the do/while loop.

To fix the first problem, I suggest using a conventional format string, e.g. printf("This is = %d\n",i); With the newline at the end, the printf will normally be seen immediately (at least when not being redirected to a file). Alternatively, you can force the buffer to be flushed by calling fflush(stdout) immediately after calling printf. That will work regardless of where your newlines are.

The second problem can be fixed by incrementing i prior to continuing, e.g.

if (i == 10) {
    i++;
    continue;
}

But a cleaner way to write the loop would be:

do {
  if (i != 10) {
      printf("This is = %d\n", i);
  }
  i++;
} while (i<20);

This shares the increment statement for both cases by avoiding the continue.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • Thanks for the response I got it why infinite but why "this is = 9" didn't got printed? – user3737377 Aug 19 '23 at 09:01
  • @user3737377 The `"This is = 9"` call to `printf` happened, but it ended up stuck in the `stdout` buffer. When you call `printf`, `putchar`, etc., the characters are placed in a buffer. At some point, that buffer is written (flushed), at which point you will actually see those characters. Typically, when writing to `stdout`, that happens when a newline character is seen. It also happens when the program exits, or when `fflush(stdout)` is called. But in this case, none of those things happened (continued in next comment). – Tom Karzes Aug 19 '23 at 09:09
  • @user3737377 Since it was stuck in an infinite loop, the last string was stuck in the buffer waiting for a newline character. Normally this wouldn't happen, since strings normally end with a newline, but since this one didn't, it just stayed in the buffer. At that point it went into an infinite loop, with nothing more being printed, so you never saw the last string. As I said, adding the missing trailing newline character to the string would probably fix it. Calling `fflush(stdout)` after the `printf` call would also fix it, even without the newline. – Tom Karzes Aug 19 '23 at 09:12
  • thanks for the response, I didn't get all things fully as I need to dig into buffer and stdout – user3737377 Aug 19 '23 at 09:26
0

@When i is equal to 10 in this do-while loop

int i = 0;
do
{
  if(i== 10)continue;
  printf("\n This is = %d",i);
  
  i++;
} while (i<20);

then due to the continue statement it looks something like

int i = 0;
do
{
  i = 10;
  if(i== 10)continue;
} while (i<20);

or

int i = 10;
do
{
  if(i== 10)continue;
} while (i<20);

and you have an infinite loop becuase the statement where the variable i is being changed

  i++;

is skipped.

As you have an inifinite loop then the code after the loop including the for statement

for(int a = 0 ; a<20 ; a++)
{
    if(a==10)continue;
    printf("\nHere = %d",a);
}

does not get the control.

To avoid the problem you could write for example

int i = 0;
do
{
  if(i== 10)
  {
      ++i;
      continue;
  }
  printf("\n This is = %d",i);
  
  i++;
} while (i<20);

Or it will be simpler to write

int i = 0;
do
{
  if(i== 10)continue;
  printf("\n This is = %d",i);
  
} while ( ++i<20);

As for the question why is

this is = 9

is not printed then it is due to absence of the new line character in the call of printf

  printf("\n This is = %d",i);

Try rewrite it like

  printf("This is = %d\n",i);

That is the output buffer is freed when a new line character is encountered. It is so called "line buffering".

From the C Standard (7.21.3 Files)

When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for the response I got it why infinite but why "this is = 9" didn't got printed? – user3737377 Aug 19 '23 at 09:00
  • @user3737377 See my appended answer. – Vlad from Moscow Aug 19 '23 at 09:05
  • i got this, it is something related to buffer which i still need to look into as my understanding is that it will print as i have specified new line character \n and complier should move to next line and print it. – user3737377 Aug 19 '23 at 09:13
  • @user3737377 Until the buffer is fill and a new line character is encountered then characters are not transmitted to the screen. – Vlad from Moscow Aug 19 '23 at 09:16
  • but you mentioned "is not printed then it is due to absence of the new line character in the call of printf" but in start itself I have specified \n ? – user3737377 Aug 19 '23 at 09:21
  • @user3737377 The content of the buffer is transmitted when a new line character is encountered. What is placed in the buffer after the content of the buffer was transmitted is stored in the buffer. – Vlad from Moscow Aug 19 '23 at 09:33