Learning Programming: Data and Variables
Programming involves two important concepts – code and data. Code is where all the action is, and data is what the action is all about.
Code will “do things,” like print messages to a screen or in a browser, perform calculations, examine information and make decisions based on various rules and so on. Data is how we represent information to the computer – note that data is fed as inputs to a function and can be created via the outputs created by functions.
In a spreadsheet, we have seen that code is embodied in formulae, or functions. Data is embodied in the values you see in each cell. This leads us to a discussion of the next important concept: how does the code make references to the data that it consumes and creates? This is through variables.
In a spreadsheet, when you write a function, notice how the cell location is shown in the formula. For example, if you write a SUM formula by selecting it from the menu and then dragging the mouse over two cells. You’ll see something like this in the address/formula bar:
A1 and B1 are the names that are used in the spreadsheet to refer to the values in the cells with the address or location A1, and B1, respectively. Another way of putting it is that A1 is a variable whose value is the value you’ve entered in the cell at location A1.
The syntax for denoting a variable, and for the value represented by a variable, are specific to each programming language. Recall from Lesson 2 that syntax is the set of conventions peculiar to a language. Naming, creating and accessing variables is a very important part of the syntax of a language that you’ll need to get very familiar with when you’re learning a language.
In a spreadsheets, the values of the variables are automatically set for you when you type a value into a cell. You don’t have to do anything else to say “A1 should be equal to 2” other than clicking in cell A1, and then typing 2. In fact, there’s no other easy way to set a value for a cell – you have to either type a function or a value after clicking into a cell for that cell location to refer to anything.
In most languages, creating a variable is something you have to do explicitly. Most languages will come with some “pre-packaged” variables, which have values set for them as soon as you run a program in that language.
There is no difference 1)at least in modern languages between the syntax for setting the value of a variable and for accessing its value. For example, you might write something like:
a1=2 b1=4 sum=a1+b1
The value of sum at the end of this will be 6.
When you start manipulating data, you have to observe certain rules. Some of these rules will be pretty obvious – you cannot divide a number by 0, for example, nor can you take the square root of a negative number.
Try to do one of the above erroneous operations, and observe what happens. You’ve generated your first error! A big part of programming is making sure you understand error messages. With both Google Spreadsheets and Excel, you will get helpful-looking errors but this won’t always be the case. Reading error messages and then trying to decipher what it says about where your code is incorrect will take up half – or more – of the time you spend writing code.
One of the ways you can keep your coding error-free is by knowing what rules govern the data you’re using. When you use numbers, for example, you have to know about the “division by zero is wrong” rule. If you’re using dates, you know that the day value of the date cannot be greater than 31 – and so on.
Here’s another rule to break – try to add a number to something that’s not a number, and force an error. What types of values will generate an error?
The rules that govern the data depend on the type of the data. The two examples above are rules for two common data types – a number, and a date. Each language will have its own set of data types, and in each language, the rules that govern a type will be specific to that language.
References [ + ]
|1.||↑||at least in modern languages|