#Is goto always bad

6 messages · Page 1 of 1 (latest)

void spruce
#

So I am a mostly beginner and heard somewhere that goto is bad to use but here it seems like the perfect thing to use instead of having an infinate loop that is then broken out of. I was wondering is there is a reason for doing the loop way instead of using goto or if both of those are wrong and there is a better way.
void GetPatternType(Pattern* pattern) {
puts("How do you want to enter the pattern? from a file(f), manual(m), select a procedurally generated pattern(p)");
ClearInputBuffer();
char choice = getchar();
switch (choice) {
case 'm':
pattern->patternType = userSpecified;
break;
case 'f':
pattern->patternType = file;
break;
case 'p':
puts("select a procedural pattern type: random(r), random numbers(n), random chars (c), fibinacci sequence (f), primes (p)");
Pattern:
ClearInputBuffer();
char patternChoice = getchar();
switch (patternChoice) {
case 'r':
pattern->patternType = Random;
break;
case 'n':
pattern->patternType = RandomNum;
break;
case 'c':
pattern->patternType = RandomChar;
break;
case 'f':
pattern->patternType = RandomChar;
break;
case 'p':
pattern->patternType = RandomChar;
break;
default:
puts("error, invalid selection");
goto Pattern;
}
}
}

strange pythonBOT
#

So I am a mostly beginner and heard somewhere that goto is bad to use but here it seems like the perfect thing to use instead of having an infinate loop that is then broken out of. I was wondering is there is a reason for doing the loop way instead of using goto or if both of those are wrong and there is a better way.

void GetPatternType(Pattern* pattern) {
  puts(
      "How do you want to enter the pattern? from a file(f), manual(m), select "
      "a procedurally generated pattern(p)");
  ClearInputBuffer();
  char choice = getchar();
  switch (choice) {
    case 'm':
      pattern->patternType = userSpecified;
      break;
    case 'f':
      pattern->patternType = file;
      break;
    case 'p':
      puts(
          "select a procedural pattern type: random(r), random numbers(n), "
          "random chars (c), fibinacci sequence (f), primes (p)");
    Pattern:
      ClearInputBuffer();
      char patternChoice = getchar();
      switch (patternChoice) {
        case 'r':
          pattern->patternType = Random;
          break;
        case 'n':
          pattern->patternType = RandomNum;
          break;
        case 'c':
          pattern->patternType = RandomChar;
          break;
        case 'f':
          pattern->patternType = RandomChar;
          break;
        case 'p':
          pattern->patternType = RandomChar;
          break;
        default:
          puts("error, invalid selection");
          goto Pattern;
      }
  }
}
techtinker.
gritty kelp
#

goto is generally fine as long as you only jump forwards and not into any strange places

#

and as long as there isn't any other statement that could easily replace it

#

in this case, you should just make a while (true) loop; the intention of that is much more clear