Tech Tuesday: Control Structures

Last time in the Tech Tuesday cycle on programming we saw that so-called reserved words have special meaning.  They effectively constitute the built-in vocabulary of a programming language.  Now let’s go all the way back to the basic analogy that I used for the overview post: programming is telling a computer what to do, which is very much like telling a person what to do.  

One of the expressive abilities that is required for that are conditionals, as in: if the front door is locked, then go around the house to the back door. In other words, some of the instructions apply only if a certain condition is encountered.  All programming languages have some notion of a conditional statement.  For instance, in Javascript we can say

if (a < 0) alert (a + " is a negative number");

This alert will be executed only when the value of a is less than 0.  The alert statement will be ignored otherwise.  Now most languages also provide the ability to include alternative statements to be executed when the condition is not met as follows:

if (a < 0) alert (a + " is a negative number");
else alert (a + " is zero or a positive number");

Expressions such as “if” and “else” in a programming language that impact the flow of program execution are generally referred to as control structures.

Control structures come in two basic flavors: branching and looping.  We already saw one example of branching with if and else.  Many programming languages also have some kind of statement that lets us test many different cases and execute instructions accordingly.  This usually goes by a name such as “case” or “select” or “switch” as in the following example from the programming language Pascal (copied from the Wikipedia entry on the switch statement): 

case age of
   0,1: writeln('baby');
   2,3,4: writeln('toddler');
   5..12: writeln('kid');
   13..19: writeln('teenager');
   20..25: writeln('young');
   else writeln('old');

As it turns out this is our first example of something called syntactic sugar.  I am not sure where exactly that terminology comes from, but the idea is that “sugar” is something we use to make something taste better or be more palatable (cf Mary Poppins). The reason is that we could have used a series of if statements instead, as in the following (again in Pascal)

if (age = 0) or (age = 1) writeln('baby');
if (age = 2) or (age = 3) or (age = 4) writeln('toddler');
... and so on

Now if you look closely at the execution of the code in both cases you would notice that they might not be exactly equivalent.  And that’s because in the second case every condition needs to be evaluated whereas in the first, depending on how case works the whole case statement could stop once one of the conditions has been met.  As homework you might want to think about how to fix that problem. 

Now the other type of control structure that we need is some kind of ability to repeat a set of instructions.  Again this is something we do naturally when giving instructions to a person as in “keep turning the key until the door is unlocked.”  The “turning of the key” is to be repeated until a condition is met, in this case that the door is unlocked.  Unlocking could require one or more turns of the key.  The Pascal programming language had a specific control structure which exactly mimics this:

   turn(key, door)
until (is_unlocked(door))

Of course it could turn out that the door is already unlocked before you turn the key at all.  In that case the above instructions will still turn the key at least once which might either be a wasted effort or in fact wind up locking the door by mistake.  So what we want instead is a control structure that first tests a condition and only repeats the instructions if the condition applies.  That turns out to be the more common loop structure in programming.  Pretty much every programming language has a version of the while loop as in:

while (is_locked(door))turn(key, door)

And in those cases where you do want to test the condition after executing the loop at least once most of those language instead of using entirely different reserved words as in Pascal, allow you to say something like

   turn(key, door

You will notice that this is not as intuitive for a human reader as the Pascal example that I gave above because the condition we are testing is the opposite of the end state we are trying to reach.  We need to keep repeating as long as the door is still locked.

For loops too many programming languages provide a variety of syntactic sugar.  The most common is the nearly ubiquitous for loop.   Let’s say you wanted to display the numbers 1 through 5, here is a Javascript for loop that would accomplish that:

for(i = 1; i < 6; i++) alert("Counter is " + i);
This is syntactic sugar because we would easily accomplish the same thing with a while loop as in
var i = 1;
while (i < 6) {
   alert("Counter is " + i);

The argument in favor of having a separate for loop is that it can be easier to read because it pulls the initial state (i = 1), the condition to be tested (i < 6) and the change to occur on each iteration (i++) all right next to each other.  That can make it easier to understand what is going on in the loop.  In general though it is worth being mindful of syntactic sugar as just with regular sugar having too much of it can be a bad thing.  Some languages (Perl comes to mind) have a ton of it and reading someone else’s code can become quite tricky.

The key take away from today should be that programming languages come with a variety of control structures but all you really need is branching (if) and looping (while). Every possible control flow can be put together with just those two.  And in an upcoming post we will encounter recursion as an alternative way to loop.  But before we can do that we will need to learn about defining our own “words” as a way of extending a programming language, so that we can actually say something like turn(key, door) as in the example above. 

Enhanced by Zemanta

Posted: 10th July 2012Comments
Tags:  tech tuesday programming control structures

Newer posts

Older posts

blog comments powered by Disqus
  1. paramendra reblogged this from continuations
  2. rekidetiar reblogged this from continuations
  3. cavalierzee reblogged this from continuations
  4. continuations posted this

Newer posts

Older posts