continue statement in a do while loop
Asked Answered
U

4

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

Unstained answered 29/9, 2020 at 13:3 Comment(4)
@FredLarson The continue will be executed because 2 < 15 is true.Righthanded
That (valid) construct is easily checkable by executing the program.Thorley
If you want to use false and true (and bool), you can #include <stdbool.h>.Lobby
@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.Fernandes
L
12

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
Lyingin answered 29/9, 2020 at 13:10 Comment(2)
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?Fernandes
@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 neededLyingin
L
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.

Lobby answered 29/9, 2020 at 13:11 Comment(0)
D
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).

Debi answered 29/9, 2020 at 13:4 Comment(0)
T
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.

Towage answered 29/9, 2020 at 13:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.