8
#include <stdlib.h>
#include <stdio.h> 
enum {false, true}; 
    
int main() 
{ 
   int i = 1; 
   do
   { 
      printf("%d\n", i); 
      i++; 
      if (i < 15) 
        continue; 
   } while (false); 
      
   getchar(); 
   return 0; 
} 

What happens after the continue statement is executed in this code?

Where does the control go?

4
  • @FredLarson The continue will be executed because 2 < 15 is true. Commented Sep 29, 2020 at 13:09
  • 2
    That (valid) construct is easily checkable by executing the program. Commented Sep 29, 2020 at 13:10
  • 1
    If you want to use false and true (and bool), you can #include <stdbool.h>. Commented Sep 29, 2020 at 13:24
  • @MikeCAT, I think he said after the continue. Meaning does it go back to the top of the loop, or does it go to the while condition. Commented Mar 17, 2024 at 13:44

4 Answers 4

15

The next statement will be while (false); which ends the do-while loop so after that it executes getchar();

In general:

do
{
    ...
    statements
    ...

    continue;   // Act as "GOTO continue_label"

    ...
    statements
    ...

continue_label:
} while (...);

If you want to try it out, you can use this code:

int i = 0;
do
{
    printf("After do\n");
    ++i;
    if (i < 2) 
    {
        printf("Before continue\n");
        continue;
    }
    printf("Before while\n");
} while(printf("Inside while\n") && i < 2);

Output + comments to explain:

After do              // Start first loop
Before continue       // Execute continue, consequently "Before while" is not printed
Inside while          // Execute while
After do              // Start second loop
Before while          // Just before the while (i.e. continue not called in this loop)
Inside while          // Execute while
Sign up to request clarification or add additional context in comments.

2 Comments

What would be the recommended way if you want something similar to continue to unconditionally go back up to the top of the loop? Do I have to use a goto?
@QwertYuiop There is no C statement to make the execution "go back" to do. So goto will be needed. That said, it's probably better to reconsider your loop design so that a goto isn't needed
5

ISO/IEC 9899:2011, 6.8.6.2 The continue statement

[...]

(2) A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body. More precisely, in each of the statements

while (/* ... */) {
/* ... */
continue;
/* ... */
contin: ;
}

do {
/* ... */
continue;
/* ... */
contin: ;
} while (/* ... */);

for (/* ... */) {
/* ... */
continue;
/* ... */
contin: ;
}

[...] it is equivalent to goto contin;

What happens after continue statement is executed in this code? Where does the control go?

To the end of the loop, i.e. while ( false ) in your code, which will exit the loop.

Comments

3

From here:

The continue statement passes control to the next iteration of the nearest enclosing do, for, or while statement in which it appears, bypassing any remaining statements in the do, for, or while statement body

Because the closest one of these is the while(false) statement, execution flow continues to that statement, and exits the loop.

This would be true even if there were other statements between the continue and while(false) For example:

int main() 
{ 
   int i = 1; 
   do
   { 
      printf("%d\n", i); 
      i++; 
      if (i < 15) 
        continue;          // forces execution flow to while(false)
      printf("i >= 15\n"); // will never be executed
   } while (false); 
   ...  

The continue; statement here means the printf statement following it will never be executed because execution flow continues to the nearest one of the loop constructs. Again, in this case while(false).

Comments

1

When you use continue statement, further statements inside the loop get skipped and control goes to next iteration which is "condition check" in your case (in case of for loop, it goes to the third statement of for loop where increment/decrement is done to a variable generally). Since the condition is "false", iteration stops.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.