1

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.

Plusce
  • 117
  • 2
  • 14
  • I cannot imagine a use case for that which cannot be served with the usual constructs (`if`, `while`, etc). Please provide a minimal example – Walter Jun 18 '15 at 01:05
  • For example: we have an array we want to fill in specific characters (only "a" or "b"). If we wouldn't use 'goto' after writing incorrect value, the condition would be rechecked (or... I don't know better option). As a result - our counter would be increased (instead of write tab[i] again, wrote badly before, we would write tab[i+1]) – Plusce Jun 18 '15 at 01:16
  • 1
    what's wrong with a `while` loop? as in `char x=0; while(is_wrong(x=obtain())); table[i]=x;` – Walter Jun 18 '15 at 09:11
  • We can do it in this way too, but isn't using a 'goto' more intuitive in this situation? (knowing, that this command isn't recommended usually) – Plusce Jun 19 '15 at 15:18
  • 1
    `goto` is **never** intuitive, since the label and the `goto` statement are in general not related in a structural way. With `do` there is always a `while`, an associated block of execution and a condition. – Walter Jun 19 '15 at 17:25

4 Answers4

6

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;
  }
}
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • I used the the same operation before the moment :). Works good. So, we couldn't do it shortly? – Plusce Jun 18 '15 at 01:01
3

I think a more reasonable solution is

int trie=0;
for(int i=0; i<10; i++) {
    while((i+tries)%3==0 {
         ++tries;
    }
}
john zhao
  • 986
  • 1
  • 6
  • 8
1

If you want to avoid goto at any cost,

int tries = 0;
for (int i=0; i<10; ++i) {do{

  bool redo = ((i+tries)%3 == 0);

  if (redo) { ++tries; }
  else      { break; }

} while(1); }
mpapec
  • 50,217
  • 8
  • 67
  • 127
1

Why can you not use a simple while loop?

auto is_okay = [](char x) { return x=='a' || x=='b'; };
container C;
for(std::size_t i=0; i!=C.size(); ++i) {
  char x;
  while(!is_okay(x=obtain_new_character()));  // get new x until it's okay
  C[i]=x;
}

Or, equivalently, a do while loop

container C;
for(std::size_t i=0; i!=C.size(); ++i) {
  char x;
  do {
    x=obtain_new_character();
  } while(x!='a' && x!='b');
  C[i]=x;
}

Or even a for loop

container C;
for(std::size_t i=0; i!=C.size(); ++i) {
  char x=0;
  for(; x!='a' && a!='b'; x=obtain_new_character());
  C[i]=x;
}
Walter
  • 44,150
  • 20
  • 113
  • 196