"continue" inside while is redundant or not?

    while (getchar()!='\n')
    {
        continue;
    }

Is "continue" keyword redundant in this situation? I am trying to skip the rest of the input such as "noooo"

Yes, continue here is redundant since the loop would "continue" by default when it reaches the end of the block. At the end of the block or at a continue, it will re-evaluate the conditional in the while statement. This will call getchar() again to test if the next character is a newline. But this would happen with or without that continue statement, making it unimportant in this particular context.

'continue' is redundant. The while loop executes whatever is inside the body of the loop after checking the condition. Since continue is there, it will simply execute the continue statement and recheck the condition. If there is nothing inside the body it will not execute anything and simply check the condition again.

#include<stdio.h>
int main(){
  while (getchar()!='\n'){
      //continue;
  }
  return 0;
}

You can simply remove the body of while loop and put a semicolon as shown below.

#include<stdio.h>
int main(){
  while (getchar()!='\n');
  return 0;
}

Yes, the continue is redundant in this case because it is at the end of the loop. In fact, you can just put a ; at the end of the while.

Comments
  • Syntactically redundant, yes, though depending on who you ask it could make the intent of the loop clearer.
  • here ir's useless. ,now If there's code after the statement, the code is never reached if the continue isn't wrapped in a if statement
  • "very redundant" is redundant.
  • @stark very true, I adjusted the wording
  • I have no idea why the book I am reading now gave this example. Maybe a typo in there. The book I am talking about is C Primer Plus (Stephen Prata) (Chapter 8)(Listing 8.4 Guess.c program)
  • @Applik without context it is hard to say why that was included. Maybe the author believed it would help the reader conceptualize the control flow of a while loop. But this particular example doesn't add anything to real code that I can see.
  • In a book, I can see why it would be useful. The alternative is while (getchar()!='\n') {} or while (getchar()!='\n');, both of which (and especially the latter) seem rather confusing otherwise.
  • How about using C code in your answer to a question tagged [c] (and not [c++])?
  • The continue is redundant but I am against while(...); because it is too easily overlooked, or thought to be a mistake (as often done with an if clause). An empty { braced } code block makes the intention more obvious.