Tech Tuesday: Control Structures (Cont’d)

I was originally planning to write today’s Tech Tuesday post about how we can define our own words in a programming language.  But then I noticed an important omission in last week’s post on control structures.  I didn’t really explain how we can have multiple statements in either a loop or in a branch of program execution.  I just kind of snuck that into the following example:

var i = 1;
while (i < 6) {
    alert("Counter is " + i);
    i++;
}

Here we have two statements inside the loop: the “alert” and then the incrementing of the variable i.  In order for the computer to know what is supposed to be part of the loop, we need some way of grouping these two statements together.  In Javascript this is accomplished with the so called “curly braces” { and }

var i = 1;
while (i < 6) {
    alert("Counter is " + i);
    i++;
    alert("Still in the loop");
}
alert("After the loop");

We refer to a group of statements as a block. Other programming languages have different ways of indicating blocks.  For instance, Python uses indentation as follows:

i = 1
while i < 6:
    print "Counter is" , i
    i++
    print "Still in the loop"
print "After the loop"

Yet other languages, such as Ada and many variants of BASIC, use additional reserved words as in the following example of branching

if condition then
    -- statement 1
    -- statement 2
    -- ...
else
    -- statement 1
    -- statement 2
    -- ...
end if

The “then” starts a new block which goes to the “else” which starts another block which ends at the “end if”.  Many of these languages also have a generic “begin” and “end” to delineate blocks.

Finally, Lisp and related languages such as Scheme or Clojure use so-called s-expressions, which are lists where each list starts with a ( and ends with a ) and has zero or more elements in it.  In these languages there are no simple statements.  Instead everything has a value, even the rough equivalent of a block, which is “progn”.  For instance (progn (+ 3 5) (* 2 7)) first adds 3 and 5 and then multiplies 2 by 7.  The value of this “block” (really expression) is the last value, i.e. 14. I am planning a whole series of posts down the line on Lisp.

As it turns out, there can be more to blocks than just grouping statements.  At least in some programming languages, such as C and C++, variables that are defined inside a block exist only while the code inside that block executes.  For instance, the following C code

#include <stdio.h>

int main(void) {
    int i = 3;
    if (1) {
        int i = 5;
        printf("%d", i);
    }
    printf("%d", i);
    return 0;
}

outputs first 5 and then 3.  This is not the case for Javascript as you can see by trying the following:

var i = 3;
if (1) {
    var i = 5;
    print(i);
}
print(i);

If you run this you will find that both the first and the second print output a value of 5.  The var i inside the block did not create a variable only inside of this block but changed the value of the variable i that was defined outside of the block.  We will learn a lot more about this when we learn about variable scoping.  Now we have the basics in place and next week can in fact learn about how to define new words in a programming language.

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

Newer posts

Older posts

blog comments powered by Disqus
  1. continuations posted this

Newer posts

Older posts