Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control structures beyond standard conditionals and loops?

Structured programming languages typically have a few control structures, like while, if, for, do, switch, break, and continue that are used to express high-level structures in source code.

However, there are many other control structures that have been proposed over the years that haven't made their way into modern programming languages. For example, in Knuth's paper "Structured Programming with Go To Statements," page 275, he references a control structure that looks like a stripped-down version of exception handling:

loop until event1 or event2 or ... eventN
   /* ... */
   leave with event1;
   /* ... */
repeat;
then event1 -> /* ... code if event1 occurred ... */
     event2 -> /* ... code if event2 occurred ... */
     /* ... */
     eventN -> /* ... code if eventN occurred ... */
fi;

This seems like a useful structure, but I haven't seen any languages that actually implement it beyond as a special case of standard exception handling.

Similarly, Edsger Dijkstra often used a control structure in which one of many pieces of code is executed nondeterministically based on a set of conditions that may be true. You can see this on page 10 of his paper on smoothsort, among other places. Sample code might look like this:

do
    /* Either of these may be chosen if x == 5 */
    if x <= 5 then y = 5;
    if x >= 5 then y = 137; 
od;

I understand that historically C influenced many modern languages like C++, C#, and Java, and so many control structures we use today are based on the small set offered by C. However, as evidenced by this other SO question, we programmers like to think about alternative control structures that we'd love to have but aren't supported by many programming languages.

My question is this - are there common languages in use today that support control structures radically different from the C-style control structures I mentioned above? Such a control structure doesn't have to be something that can't be represented using the standard C structures - pretty much anything can be encoded that way - but ideally I'd like an example of something that lets you approach certain programming tasks in a fundamentally different way than the C model allows.

And no, "functional programming" isn't really a control structure.

like image 744
templatetypedef Avatar asked Jul 31 '11 03:07

templatetypedef


People also ask

What control structures are for loops?

Control structures alter the normal sequential flow of a statement execution. Loops allow the a block of statements to be executed repeatedly without actually writing them down numerous times.

What are the 4 control structures?

if-else conditionals, case statements, for loops, and while loops are all control structures.

What type of control structure is a conditional statement?

In selection control structures, conditional statements are features of a programming language which perform different computations or actions depending on whether a programmer-specified Boolean condition evaluates to true or false.


1 Answers

  • Since Haskell is lazy, every function call is essentially a control structure.
  • Pattern-matching in ML-derived languages merges branching, variable binding, and destructuring objects into a single control structure.
  • Common Lisp's conditions are like exceptions that can be restarted.
  • Scheme and other languages support continuations which let you pause and resume or restart a program at any point.
like image 79
munificent Avatar answered Sep 26 '22 15:09

munificent