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.
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.
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
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
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
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 = "January"
Month = "February"
And so on until you get to Month = "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.
currentMonth = Clock.Month
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.
Updating your subscription status