Programming Basics for Duncan part III: If and Loops

Your most basic and common decision-making statement is the if command. It always takes this form:

IF (some test that returns a boolean true/false) THEN (do something)

So you have a test and a something(s) to do if the test resolves to true. This is where we use the comparitors from last chapter:

IF x=5 THEN do something

Almost all languages also have an else option in the if.

IF x= 5 THEN do this thing ELSE do the other thing

And that’s pretty neat! We can write a program that can think about things and do different things depending on the outcome of a test. Remember, we can use lots of comparisons like =, <, > and so on.

But life gets complicated when you have to test many values at once, you end up with ifs inside ifs like:

IF x=5 THEN IF y=0 THEN do something ELSE if y=10 THEN do that thing ELSE do something else

That gets really confusing really fast. To make life easier we use…

Boolean Logic

As well as the normal operators like = or < or => you can use logical operators. There’s a bunch but people really only use three:

  • AND: do two comparisons and if they both resolve to true, the result is true.
  • OR: do two comparisons and if either are true, the result is true.
  • NOT: is a tool to invert a result of a comparison, like NOT x=5 resolves to true if x is any value except 5.

You can combine these to make a decision like:

IF x=5 AND y=0 THEN do the thing.

Brackets (Parentheses)

Like in maths, we use brackets to say which order we want to perform operations. In programming, they’re not really used to do maths but they are really important to compartmentalise clauses in Boolean logic. It lets you make complicated comparisons like:

IF (x=5 AND y=o) OR (x=5 and y=1) THEN do the thing.

These will let you build up complicated comparisons or tests but it’s not a great idea to do so as it makes your code hard to read and other programmers will think you are just trying to show off.

Now that we have a robust way to use if we can use it in another time-saving tool, loops.

Loops

A loop lets us do the same thing over and over again. My first ever program when like this:

10 PRINT “Hello World”
20 GOTO 10

This would output Hello World to the TV forever, until you hit the STOP key on my VIC20 and my friends and I were deeply impressed. We don’t use GOTO anymore and it’s considered gauche and very uncool.

But the idea of doing something again and again, or more usefully, a set number of times is called a loop. At the most basic they do this:

DO something
IF test is true THEN STOP ELSE GO BACK TO DO

All loops are like this, although most put the test first. The most common type is a FOR loop and it usually includes a counter often called an index and written i.

So you set i to 0 or 1 (normally) and you make a test to check the value of i each time the loop loops:

i = 1
IF i=5 THEN STOP
do something
i = i+1
GO BACK TO THE IF STATEMENT

It’s normally written:

FOR i=1, i=i+1, 5
do something
NEXT

Or some variation on that scheme.

This will ‘do something’ four times and the final value of i will be 5. FOR statements are the bread and butter of programming as they will let you count things, look for things over and over.

The other type of loop is a WHILE

WHILE NOT i = 5 THEN
do something
i=i+1
LOOP

That does the same thing as our FOR loop but you can use it when you don’t know how many times the look is supposed to run. You can do it without and index like

WHILE NOT y=”finished” THEN
do something
LOOP

That loops until y is set to the string “finished”, maybe forever.

So now we can do maths, do things with words, make tests and loop the same bit of code until a certain time or number of repetitions. For lots of (old) languages, that would be it. But we need one more chapter to cover the basics.

Leave a Reply

Your email address will not be published. Required fields are marked *