Conversation

Charlotte 🦝 therian

don’t want to use goto in c?

don’t fret and use this replacement:

int iters = 10;
int state=0;
for(;;) {
  switch(state) {
    case 0:
      printf("Hewwo!\n");
      iters--;
      state = iters < 0;
      break;
    case 1:
      return;
  }
}
1
0
2

(bugs intentional)

FILE * file1, file2, file3;
char buf1[4], buf2[4], buf3[4];
int state = 0;
for(;;) {
  switch(state) {
    case 0:
      if ((file1 = fopen(fname1, "r")) < 0) {
        state = 3;
        continue;
      }
      if ((file2 = fopen(fname2, "r")) < 0) {
        state = 2;
        continue;
      }
      if ((file1 = fopen(fname3, "r")) < 0) {
        state = 1;
        continue;
      }
      fgets(buf1, 100, file1);
      fgets(buf2, 100, file2);
      fgets(buf3, 100, file3);
      strcat(buf1, buf2);
      strcat(buf1, buf3);
      printf(buf1);
      fclose(file3);
    case 1:
      fclose(file2);
    case 2:
      fclose(file1);
    case 3:
      return;
  }
}
0
0
1