-2

How can I use the continue keyword, to continue n times a loop. Like in shell we can do continue 2 to skip 2 iterations. How is it possible to achieve this in C.

  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/203695/discussion-on-question-by-newplayer55-c-continue-n-times-a-for-loop). – Samuel Liew Dec 05 '19 at 19:59

3 Answers3

2

You cannot do it using continue, but you can skip for loops incrementing the variable to skip n iterations.

for (int i = 0; i < 10; i++) 
{
   if (some_condition) 
     i += nSkip;

    (...) 
}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 3
    If the `continue` is a bad practice as commented above, then this one I would say even worse. Messing with loop counter inside the loop is making the code very confusing. – Eugene Sh. Dec 05 '19 at 15:05
  • `continue` is _not_ bad practice; you can of course use it in a messy way, as many constructs. – Ctx Dec 05 '19 at 15:06
  • @Ctx I am tending to agree. Note the "If" in my statement :) – Eugene Sh. Dec 05 '19 at 15:07
  • Bad practice or not, currently the OP appears to be asking exactly how to do that, and this answers that question (except for the missing `continue`). (Meanwhile I am not convinced that the question is currently what they mean to ask.) – Arkku Dec 05 '19 at 15:10
  • Yep, modifying the loop iterator from inside a for loop is very bad practice... if there's a need for that, use a while loop and modify it in one single place. – Lundin Dec 05 '19 at 15:11
  • 3
    But still, the correct solution would be `if (some_condition) { i += (nSkip-1); continue; }`, to be the equivalent. Or at least an `else` with the rest of the loop body – Ctx Dec 05 '19 at 15:12
1

This is at its root a programming style question, and those are always prone to opinionated debate. Reader beware. :-)

C does not have break(n) and continue(n). This was a deliberate choice. It was felt that these constructs are too difficult to maintain. It's hard for a later programmer to count and keep track of the nested loops. It's too easy for a later programmer to insert or delete a nesting level, throwing off the counts.

Veering down to the root of one of the biggest style debates there is, there are those who say that goto is evil and should never be used. There are also those who say that break and continue are just goto's in disguise and that they should never be used, either. Personally, I don't agree with either of those positions, but I do agree that break(n) and continue(n) have little to no value over pure goto; they're at least as confusing and prone to error. So if you find yourself needing break(n) or continue(n) in C, and there's no other way around, it, just bite the bullet and use a goto. The fact that you needed break(n) or continue(n) proves that you're doing something irretrievably ugly, so a goto won't make it any worse.

(Now, it's true, with that said, it's easy to replace break(n) with goto out;, but it's not nearly so easy. in general, to replace continue(n) with a goto. So you'll probably have to do something else, and it'll probably be ugly, but again, by the time you get here, you're doomed to that anyway.)

See also this question and its answers, although there the discussion is specifically about break(n), not continue(n).

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 1
    A good statement; however, it looks like the OP indeed meant to skip two iterations of the loop, judging from the accept of the other answer... – Ctx Dec 05 '19 at 15:16
  • re. `break` and `continue` being `goto`s in disguise, one could extend that argument to `return`, i.e., the value should only be returned from one place (the last line of the function body). But the same counter-argument applies to `break`, `continue` and `return`: the code without them often ends up with large parts deeply nested inside `else` branches. And of course one can further counter _that_, by further functions as replacements for the deep nesting, etc. =) – Arkku Dec 05 '19 at 15:18
  • @Ctx (forehead slap) So he did! Oh well, another excuse to launch into a canonical old debate about a programming style issue *not* averted... – Steve Summit Dec 05 '19 at 15:19
-2
int i;
for (int i = 0; i < 10; i++) 
{
   if (n<=2) 
     continue;
   else
      <do your task>
}
chakra t
  • 27
  • 4