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?
robotic is the scripting language in megazeux, the greatest game engine ever made.
i have spoken at length about megazeux before and i will probably do it again, but here is the short version.
megazeux was somehow more obscure than its spiritual predecessor, zzt, but it's the one i landed on, and one of my longest-running regrets is never having quite made a game in it. shown here is the opening shot of caverns of zeux, the game that came with it. (you can play it in a web browser! science is amazing!) or, rather, caverns of zeux was "it", the game you were downloading, but it happened to be bundled with the engine and editor used to create it, and you could just go in and see how it was all put together.
the basic idea is that megazeux gives you a big box of standard gizmos to play with — boxes you can push around, turrets that shoot you, ice you slide on, and so forth — but you can also do absolutely whatever you want by creating a robot and programming it yourself. zzt let you do this, but megazeux let you do it better.
as an example, here is the code for an early robot in Caverns of Zeux, posing as a simple village dwarf:
cycle 10
: "st"
go RANDANY for 1
goto "st"
: "touch"
if "form" = 1 then "sc"
if "form" = 4 then "sc"
[ "Ah! Another traveller. Make sure the visit the healer if you"
[ "get hurt. You'll need some coins, though- look in the Ice"
[ "cavern. I've heard rumors of untold wealth within them!"
goto "st"
: "sc"
* "AAAAAA!!!!!"
end
this is a little terse, but the gist is that the guy will wander around at random (one step every 10 tics), and if you push against them (thus forcing them to their special state "touch"), they will either give some helpful direction, or scream because (spoilers) you are a dragon.
one of the clever things about megazeux is that its scripting is functionally an assembly language, but the language and the editor work together to make it a little more ergonomic. for example, take go RANDANY for 1 — this is an instruction of the form go DIR for NUM. however:
-
there are several special directions for common behaviors to avoid the need for expressions or intermediate variables.
RANDANYwill evaluate to one of the four cardinal directions at random. -
the editor recognizes a number of propositions as optional. these are... like croutons. you can sprinkle them throughout a line and they don't affect parsing. as soon as you move to another line, the editor will "fix up" your line to have the canonical arrangement of prepositions. that's what the
foris doing here; it doesn't actually do anything, but it helps a human reader understand what the number is for. you could typego into randany through 1and get the same result. -
likewise, there is canonical capitalization — directions will be converted to all caps for you.
-
there's syntax highlighting!! in this weird dos game editor from like 1994!!
one downside is that variables are... clumsy. they exist, but they are not exactly pretty. and this is not ideal when a very very very common thing a robot wants to do is remember its state.
but robots are always running, so a very common pattern is to simply use their instruction pointer as state. whatever they're currently doing, that's what their state is.
enter zap + restore.
these two instructions operate on labels. zap "foo" means to find the first active "foo" label and disable it. the secret sauce is that you can have duplicate labels, and any form of jump will take you to the first active one. (restore does the reverse, of course — it enables the last such inactive label.)
thus, a robot that gives a sequence of different responses to the same input is very easy:
end
: "touch"
* "Please don't touch me."
zap "touch"
end
: "touch"
* "I said, please don't touch me."
zap "touch"
end
: "touch"
* "Stop!"
zap "touch"
end
: "touch"
* "AARRGGHH!!"
explode 4
the current state is merely the set of enabled vs disabled labels. you can do the same thing (and in fact caverns of zeux does) to track, say, a boss's health, and have its behavior change when it goes below a certain level. or you could make this more elaborately complicated by having labels that start out disabled, so maybe then you restore everything later and now the sequence is different. all without ever needing to store or examine any data at all.
and that is my favorite form of flow control.