Simple clear advice in plain English

Small Basic Guide: 2. Storing information in variables

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

Loading the player ...
An example of working with variables in Small Basic

Welcome back to our series on learning to program with Small Basic. In this video I'm going to take a look at variables. All programs work by manipulating information, known as variables in programming parlance.

Variables provide a place to store information and describe what it is. There can be many different kinds of variables in programming languages but Small Basic keeps things simple with either text or numbers.

Read more: Programming articles | Small Basic articles

Creating a variable is very simple. Type a name for the variable, then an = symbol and finally the information the variable should contain.
To create some variables with numbers, enter the following commands, each on a separate line.

number1 = 5
number2 = 6

Running the program doesn't do a lot apart from show that there are no errors, so let's add a couple of more lines to show the variables in action. This should look familiar from the Hello World but notice that there are no inverted commas. This is because we want Small Basic to print the contents of the variable.

TextWindow.WriteLine(number1)
TextWindow.WriteLine(number2)

Running the program again will show the contents of the two variables.

Variables can be changed in several different ways. Let's add 5 to number1. Move to the end of the program and type

Number1 = number1 + 5 
TextWindow.WriteLine(number1)

Run the program and see how the second time that number1 is printed it now shows 10 rather than 5.

You can also do calculations with several variables. For example, lets add the two numbers together instead and create a new variable at the same time called total. Delete the two lines we just wrote and enter the following.

Total = number1 + number 2
TextWindow.WriteLine(total)

Running this program will show that the total is 11. All kinds of arithmetic can be used with variables including multiplication and division.
Variables can also contain text. In this case you should type " at the beginning and end of the text. For example

greeting = "Hello World"

Numbers and text can be combined into a single text variable, for example

summary = "The sum of " + number1 + " and " + number2 + " is " + total
TextWindow.WriteLine(summary)

Arrays

An array is a group of related variables. They look like ordinary variables except for the number in square brackets. For example a useful array would contain all the different months of the year.

Month[1] = "January"
Month[2] = "February"
And so on until you get to Month[12] = "December"

There are several different ways to work with this Month array. The following program shows two ways of displaying a month. One clever aspect of arrays is that you can use a different variable to decide which variable in the array to display.

‘Method1
TextWindow.WriteLine(month[2])
‘Method2
currentMonth = Clock.Month
TextWindow.WriteLine(month[currentMonth])

The first method works but needs updating every month. The second uses the Clock tool in Small Basic to give the month number and then use that to display the current month. This makes that part of the program fully automated.

In the next video we will look at Small Basic functions.

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

screenshot-of-small-basic

Download: Learn to write programs with Microsoft Small Basic

A simple programming language for beginners

small-basic-hello-world

Small Basic Guide: 1. Introduction and first program

We introduce Small Basic, a great programming language for beginners

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

VoIP

Voice over IP. The routing of voice conversations over the internet, which is cheaper than the telephone...

Great shopping deals from Computeractive

Information currently unavailable