Preferences

https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html

  int function(void) {
      static int i, state = 0;
      switch (state) {
          case 0: /* start of function */
          for (i = 0; i < 10; i++) {
              state = 1; /* so we will come back to "case 1" */
              return i;
              case 1:; /* resume control straight after the return */
          }
      }
  }


Huh, I didn't know you could put case statements arbitrarily anywhere inside a switch block in C. Past the initial WTF, it's a nice hack for implementing coroutines.

Oh yeah, you can toss ‘em in just about anywhere. The most well-known application of this would be Duff’s device: https://en.wikipedia.org/wiki/Duff%27s_device
As I understand it, they're kinda syntactic sugar for goto statements.
Personal preferences differ, but I would find this code so much cleaner if it was a goto.

This item has no comments currently.

Keyboard Shortcuts

Story Lists

j
Next story
k
Previous story
Shift+j
Last story
Shift+k
First story
o Enter
Go to story URL
c
Go to comments
u
Go to author

Navigation

Shift+t
Go to top stories
Shift+n
Go to new stories
Shift+b
Go to best stories
Shift+a
Go to Ask HN
Shift+s
Go to Show HN

Miscellaneous

?
Show this modal