Simple clear advice in plain English

Small Basic Guide: 4. Making decisions and looping

Seemingly long and repetitive tasks can be very simple with the If and For commands

Loading the player ...
Small Basic can automatically adjust a loop to match the length of an array

A good program is able to perform different tasks depending on certain conditions and this is normally the job of the If command. Another common use for programs is to perform the same task several times. This is known as looping and one of the most common commands is For.

Read more: Programming articles | Small Basic articles

Decisions with If

Just like Excel an If command consists of a statement followed by instructions of what to do if that statement is true. This should then be followed by the word Else and then another command to do if the first statement isn't true. Several conditions can be added by using the ElseIf command. Regardless of how many conditions there are, there should always be the EndIf command after the last condition to tell Small Basic that the If command has ended. Thus:

If trafficLights = "red" Then
TextWindow.WriteLine("Stop")
ElseIf trafficLights = "yellow" Then
TextWindow.WriteLine("Get ready")
Else
TextWindow.WriteLine("Go!")
EndIf

Looping with For

Sometimes a program needs to perform a repetitive task. Creating a loop allows you to make this a simple task. A very simple loop that creates the two times table looks like this

For i = 1 To 10
TextWindow.WriteLine(i * 2)
EndFor

Looping is particularly useful when working with arrays as it offers a quick way to work with each variable stored in them. For example you can print every month name in a week array.

day[1] = "Sunday"
day[2] = "Monday"
day[3] = "Tuesday"
day[4] = "Wednesday"
day[5] = "Thursday"
day[6] = "Friday"
day[7] = "Saturday"

For i = 1 To 7
TextWindow.WriteLine(day[i])
EndFor

This is only useful if you already know the number of variables stored in the array. This is where the section about Objects starts to become very useful. Because the Array is an object we can access information about it that is created automatically. Change the first line in the example above to

For i = 1 To Array.GetItemCount(day)

And try deleting or adding to the week list. This loop will always work no matter how many variables are stored in the array.

Reader Comments

   

Add your comment

Please keep comments constructive and free from abuse of any kind and swearing. If you wish to link to a product or service online, please do so in such a way that makes it clear that it is not spam. If you are connected to any such product you should make that clear.

We may use your comments in the magazine. We may edit your comments for clarity or to remove unacceptable material. We will attribute your comments but not share your email address.

We request your email address and record your Internet Address (IP address) in order to block spam from our site. We will never share this information without your permission.

All comments are reviewed by the Computeractive Team before being published. Please bear with the slight delay this causes, you don't need to post more than once.

Click here to read our Privacy Policy

Click here to read our site Terms & Conditions

Related articles

small-basic-hello-world

Small Basic Guide: 1. Introduction and first program

We introduce Small Basic, a great programming language for beginners

small-basic-variables

Small Basic Guide: 2. Storing information in variables

We look at how to store and work with information in Small Basic

small-basic-example-program-showing-functions

Small Basic Guide: 3. Making things happen with functions

Functions are the commands in Small Basic that

Content Recommendation

Question & Answer

Q.Why is Windows Backup skipping files?

> Read the answer

Q.Why do my scanned documents display gibberish?

> Read the answer

Q.How can I convert MTS files to edit in Windows Movie...

> Read the answer

Best deals on the web

img

Samsung NP350E7C-A04UK

£342.99- Buy it now

img

Toshiba Satellite C850D-11Q (PSCC2E-00R00JEN)

£289.97- Buy it now

img

Samsung NP355V5C-A06UK

£322.99- Buy it now

Updating your subscription status Loading

Most popular articles

No matching document

Poll

Do you have Windows 8?

Jargon Buster

Computing terms explained in plain English

Restore Point

A Windows backup of system files and settings.

Great shopping deals from Computeractive

Information currently unavailable