Search code examples
c++perlloopsredo

C++ - back to start of loop without checking the condition


Programming on perl, we can use a smart function named 'redo' - we can go back to the start of the loop, without looking at the condition. It's useful when, for example, we create a table in which we must to set a expected values/characters (e.g. "a-b-c", nothing else). I would like to ask if exist in C++ function like that. I would be grateful for your help.


Solution

  • There is no redo keyword for going back to the start of a loop, but there's no reason you couldn't do it with goto. (Oh I feel so dirty recommending goto...)

    int tries = 0;
    for(int i=0; i<10; ++i) {
      loop_start:
      bool ok = ((i+tries)%3==0);
      if(ok) {
         ++tries;
         goto loop_start;
      }
    }