posts from @JoshJers tagged #flow control

also:

The question here is "what's your favorite non-C-like flow control statement/construct that you've seen in a programming language" but I'm gonna start with a little preamble.


I'm very familiar with C-style flow control:

if (condition)
  { thing; }
else 
  { otherThing; }


// And, thanks to C++17:
if (auto v= initializer(); condition) 
  { thing(v); }
else 
  { otherThing(v); }
for (int i = 0; i < 5; i++)
  { thing(); }
for (var e : elements)
  { thing(e); }
while (condition)
  { thing(); }

(plus, ya know, do/while and goto)

I've seen a few others, like Rust's loop (basically a while(true)):

loop
{ 
  thing(); 
  if (something) 
    { break; }
}

and Swift's ranges, which are nice:

for i in 0..<4
  { thing(i); }

What I'm wondering is: what are your favorite bits of flow control logic (loops, jumps, conditionals, etc) that don't show up in C or C++? Are there loop forms that you think are truly elegant? Is there a switch-like construct that doesn't suck like C's switch statement does?