Back To Basics - Part 4

What's a control statement? What's a boolean expression? Mark Moxon answers these and many more

Open up a new packet of digestives and crack open a new coffee jar, we're about to start the most interesting part of our journey into Basic programming yet. Yes, it's time to investigate control statements.

Take control

So, what's a control statement? Well, simply put it is a statement which changes the flow of program execution. Until now, we've only seen programs which are executed one line at a time, in sequence, starting with the first line and finishing with the last line. Control statements change this situation and enable you to alter which lines are executed, and in which order. Complicated though this sounds, control statements are pretty easy to follow.

The first concept to get to grips with is that of boolean expressions. Don't worry about the rather surreal name: a boolean expression is just an expression which can be evaluated to one of two values: TRUE or FALSE. To give you an example, the expression '4>6' would evaluate to FALSE, as 4 is not bigger than 6, and '(2*var1)<>var2' would be TRUE if twice the value of var1 was not equal to that value of var2.

The actual values of the boolean variables TRUE and FALSE are -1 and 0, 50 if you wrote a program line PRINT TRUE it would print -1; this means that we can store boolean values in variables of type real or integer, so these two statements are equally valid:

condition=TRUE

condition%=FALSE

The first of these statements is exactly the same as:

condition=-1

and the second is the identical to this statement:

condition%=0

The words TRUE and FALSE are just a convenient shorthand to make the program more readable.

The table below shows the collection of numerical and string operators which can be included in boolean expressions so for example, the expression var1%<=var2% will evaulate to TRUE if the value of var1% is less than or equal to the value of var2%.

Conditional operators

Operator True when ...
A=B A is equal to B
A<B A is less than B
A>B A is greater than B
A<=B A is less than or equal to B
A>=B A is greater than or equal to B
A<>B A is not equal to B
A$=B$ A$ and B$ are the same
A$<>B$ A$ and B$ are different
NOT A A is FALSE
A AND B Both A and B are TRUE
A OR B Either A or B is TRUE (or both)
A EOR B Either A or B is TRUE (but not both)

If only ...

The first, and possibly the most important control statement is the IF-THEN structure, which forms the fundamental building block for decision making in programs. You can put together an IF-THEN structure in a number of ways, but the simplest is like this:

IF cond THEN statements

The condition cond is a boolean expression which evaluates to TRUE or FALSE: if it is TRUE, then the statements are executed, otherwise the program moves on to the next line. Notice that if you want to include more than one statement in statements then you should separate them by commas, though a much more readable method is described in the next section.

An extension of the IF-THEN structure is the inclusion of an ELSE part. The whole structure then looks like:

IF cond THEN state1 ELSE state2

Here the state1 statements are executed if the condition is TRUE, and the state2 statements are executed if it is FALSE.

To give you an idea of how all this works, have a look at Listing1. It contains a collection of IF-THEN and IF-THEN-ELSE statements. It's all pretty logical (if you'll excuse the rather sad pun), and if you read each structure through, it'll make sense.

Listing 1

REM >Listing1
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
MODE 0
:
var1=1.23
var2=2.34
IF 1<2 THEN PRINT "1<2: TRUE" ELSE PRINT "1<2: FALSE"
IF var1<>var2 THEN PRINT "var1<>var2: TRUE" ELSE PRINT "var1<>var2: FALSE"
IF (2*var1)<var2 THEN PRINT "2*var1<var2: TRUE" ELSE PRINT "2*var1<var2: FALSE"
END

Block structures

The most useful version of the IF construct is the so-called block structured IF-THEN- ELSE-ENDIF structure. This structure spans a number of program lines and enables you to create whole program segments that may or may not be executed, depending on a certain condition. The way it is laid out is as follows:

IF condition THEN
 ... <first statements> ...
ELSE
 ... <second statements> ...
ENDIF

The first set of statements is executed if the condition evaluates to TRUE, and the second set if it evaluates to FALSE. The big advantage of this structure is that the two sets of statements can include more IF-THEN structures without confusing the issue.

Let's take an example. Say we wanted to wnte a program which would ask you what your name was, and whether you were male or female, and would then tell you what your hair colour was. OK, this is only applicable if the computer already knows about the hair colour, sexes and names of certain people, but let's assume the information in the table below holds true: apologies for the green hair, but you try thinking of a fourth colour.

Hair colour

Name Sex Hair colour
KimMaleGreen
KimFemaleGinger
VivianMaleBrown
VivianFemaleBlonde

Now have a look at Listing2 below. This does exactly what we want, and the use of indenting spaces helps to show how the program flows.

Listing 2

REM >Listing2
:
ON ERROR REPORT:PRINT " at line ";ERL/10:END
MODE 0
:
INPUT "Please enter your name: " name$
IF name$="Vivian" THEN
  INPUT "Are you male or female? (M/F)" sex$
  IF sex$="M" THEN PRINT "Your hair is brown" ELSE PRINT "Your hair is blonde"
ELSE
  IF name$="Kim" THEN
    INPUT "Are you male or female? (M/F)" sex$
    IF sex$="M" THEN PRINT "Your hair is green" ELSE PRINT "Your hair is ginger"
  ELSE
    PRINT "That name is not known"
  ENDIF
ENDIF
END

Here's a quick breakdown of the program flow, which should be pretty obvious once you've typed it in and it's sitting in an Edit window.

Line 6: Get the name into name$.

Line 7: If the user has entered Vivian then ...

Line 8: Enter the sex into sex$.

Line 9: If the sex is male, then the user is male Vivian, and has brown hair, so print that; otherwise the user is female Vivian, and has blonde hair, so print that.

Line 10: If the program gets here, then the name isn't Vivian ...

Line 11: So check to see if it is Kim. If it is ...

Line 12: Enter the sex into sex$.

Line 13: If the sex is male, then the user is male Kim, and has green hair, so print that; otherwise the user is female Kim, and has ginger hair, so print that.

Line 14: If the program gets here, then the name entered is neither Vivian nor Kim ...

Line 15: So print an error message.

Line 16: Indicates end of IF construct that starts in line 11.

Line 17: Indicates the end of the IF construct that starts in line 7.

Line 18: End of program.

And the rest

There are plenty of other control structures to investigate, like WHILE-ENDWHILE and CASE-ENDCASE, but that's a treat for next time. Can't wait ...


Source: Acorn User 136 - November 1993
Publication: Acorn User
Contributor: Mark Moxon