We look at how to store and work with information 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)
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.
Article tags
Related articles
Content Recommendation
Q.Why is Windows Backup skipping files?
Q.Why do my scanned documents display gibberish?
Q.How can I convert MTS files to edit in Windows Movie...
Updating your subscription status
Voice over IP. The routing of voice conversations over the internet, which is cheaper than the telephone...