Have you ever heard of Coroutines? I haven't until a few months ago when digibob (from SplashDamage) mentioned them and explained to me what they are. Instead of pasting his explanation from my IRC logs, I'll instead quote Wikipedia:
In computer science, coroutines are program components that generalize subroutines to allow multiple entry points and suspending and resuming of execution at certain locations.
[...]
Coroutines are more generic than subroutines. The start of a subroutine is the only point of entry; the start of a coroutine is the first point of entry, and places within a coroutine following returns (yields) are subsequent points of entry. Subroutines can return only once; in contrast, coroutines can return (yield) several times. The lifespan of subroutines is dictated by last in, first out (the last subroutine called is the first to return); in contrast, the lifespan of coroutines is dictated entirely by their use and need.
(from http://en.wikipedia.org/wiki/Coroutine)
Let's continue with an (pseudo-code) example:
coroutine unsigned coFibbonacci() {
unsigned a, b;
a = 0;
b = 1;
while( 1 ) {
unsigned c;
YIELD( b );
c = a;
a = b;
b += c;
}
}
Calling coFibbonacci repeatedly would return the Fibonacci series. A more advanced example (which I won't paste here) would possibly consist of game scripts that control scripted sequences and always yield when waiting for operations to be performed, so that the control flow will look natural while basically being event-based.
Usually one uses state machines in languages that don't support coroutines but an implementation using coroutines would be a lot more elegant.