Decisions, Decisions

The 3rd of our fundamental building blocks is branching or conditional statements. These are simply terms to describe the ability within our programs to execute one of several possible sequences of code(branches) depending on some condition.

Back in the early days of Assembler programs the simplest branch was a JUMP instruction where the program literally jumped to a specified memory address, usually if the result of the previous instruction was zero. Amazingly complex programs were written with virtually no other form of condition possible - vindicating Dijkstras statement about the minimum requirements for programming. When high level languages came along a new version of the JUMP instruction appeared called GOTO. In fact BASIC still provides GOTO and you can try it out by typing the following bit of code:

10 PRINT "Starting at line 10"
20 J = 5
30 IF J < 10 GOTO 50
40 Print "This line is not printed"
50 STOP

Notice how even in such a short program it takes a few seconds to figure out what's going to happen. There is no structure to the code, you have to literally figure it out as you read it. In large programs it becomes impossible. For that reason most modern programming languages either don't have a direct JUMP or GOTO statrement or discourage you from using it.

The if statement

The most intuitively obvious conditional statement is the if, then, else construct. It follows the logic of English in that if some boolean condition is true then a block of statements is executed otherwise (or else) a different block is executed.

It looks like this in BASIC:

PRINT "Starting Here"
J = 5
IF J > 10 THEN
    PRINT "This is never printed"
ELSE
    STOP
END IF

Hopefully that is easier to read and understand than the previous GOTO example. Of course we can put any test condition we like after the if, so long as it evaluates to True or False, i.e. a boolean value.

Python looks quite similar:

import sys  # only to let us exit
print "Starting here"
j = 5
if j > 10:
    print "This is never printed"
else:
    sys.exit()

Its very nearly identical, isn't it?

You can go on to chain these if/then/else statements together by nesting them one inside the other like so:

if width == 100:
   area = 0 
else:
   if width == 200:
      length = length * 2
   else:
      if width == 500:
           width = width/10
      else:
           print "width is an unexpected value!"

Note:we used == to test for equality in each if statement, whereas we used = to assign values to the variables. Using = when you mean to use == is one of the common mistakes in programming Python, fortunately Python warns you that it's a syntax error, but you might need to look closely to spot the problem.

Case statements

This is such a common construction that many languages provide a special type of branch for it. This is often referred to as a Case or Switch statement and the Tcl version looks like:

switch $width {
    100 { set area 0} 
    200 { set length [expr {$length * 2}] }
    500 { set width [expr {$width / 2}] }
    }

Neither BASIC nor Python provide such a construct but rather compromise by providing an easier if/elseif/else format:

if width < 100:
   area = 0
elif width < 200:
   length = length * 2
elif width < 500:
   width = width/10
else:
   print "width is too big!"

Note the use of elif and the fact that the indentation (all important in Python) does not change. It's also worth pointing out that both Python versions of this program are equally valid, the second is just a little easier to read if there are many tests.

BASIC provides a slightly more cumbersome version with ElseIf...THEN which is used in exactly the same way as the Python elif


Previous  Next  Contents


If you have any questions or feedback on this page send me mail at: alan_gauld@xoommail.com