News for hackers. Stuff that matters.
Re:Misconceptions about loops in C by trealira on Friday June 28, 2024 @08:33AM (#40820006)
In this particular case I think you could do one of the following (ignoring the "while (true)" + "break" combination):

1:

  while (printOneStr(s[i]), ++i < n)
      printOneStr(", ");
2:

  goto start;
  do {
      printOneStr(", ");
  start:
      printOneStr(s[i]);
      ++i;
  } while (i < n);
But for this kind of code in general, I have thought that the "do while" statement should be extended to take an extra statement after the "while (cond)" part. It could still be an empty statement, with just a semicolon, like "do ... while (cond);", exactly like it's already used; but in cases like yours, you would be able to write this:

  do {
      printOneStr(s[i]);
       ++i;
  } while (i < n) {
      printOneStr(", ");
  }
As a bonus, the current mandatory semicolon after do-while statements would stop being a special case for the grammar of C.

There are no comments on this comment yet.