A variable is a chunk of computer memory set aside to hold a piece of information that (usually) varies; hence the name
variable. All but the most simple "Hello world" example programs do this: Just think of the little calculator program that's part of Windows' accessories: At least it has to keep the first number in a calculation around somewhere, while the user keys in the second number. When the = button is pressed, the machine must recall the first number to do the actual calculation.
In Small Basic (as well as in a great many other programming languages), variables are assigned a name: Whenever something needs to be done with that information, you just write the name assigned to information in the program code.
EditThe computer's memory "archive"
To get an idea of how variables work, you can think of the computer's memory as a kind of archive room full of filing cabinets: When the computer runs into a line of Small Basic code like like
Area = 6 * 8
it first works out whatever is to the right of the = sign the
6 * 8 - remember that
* is the computer's multiply sign, so this works out to 48.
The word
Area to the left of the = sign is a
name for the variable: The computer takes the result it has just worked out to the archive, to see if there's a drawer somewhere labelled
Area. If it doesn't find a drawer with this name on it, it instead finds an empty, unused drawer to hold the new result. To make sure it can find the information again, the computer scribbles down the name
Area, and sticks it to the front of the drawer. To Small Basic and the computer, "
Area = 6 * 8" means something like:
First work out what 6 times 8 is, then file the result as a new variable in the archive, under the name of "Area".
In a Small Basic program, when a piece of information that was previously stored in memory is needed, all we have to do is to put the name of the variable that holds the information we want from the memory's archives, as a "placeholder" for the actual information. Like this:
TextWindow.WriteLine(Area)
As you've probably already seen,
TextWindow.WriteLine tells the text window to write out a line with whatever is inside the parenthesis that comes right after it. In this case, it's the word
Area, but since the word isn't enclosed in quotation marks, the computer doesn't just put the word Area on the text window. Instead, the computer goes back to the archive to find the drawer that's marked with the word "Area", to see whatever is inside. It finds the result, 48, that was put in there earlier (when the computer was dealing with the
Area = 6 * 8 part),
but it doesn't take the information out of the drawer - instead it makes a note of what it found in the drawer, and returns to work and proceeds to write the data it just got from the archive, onto the TextWindow.
EditVariables never "die" (until the program finishes)
Notice that the 48 wasn't
removed from the archive; it's still back in there - the computer merely
copied the information held in memory, onto the TextWindow. As long as a program is running, it can retrieve the information held in a given variable, as many times as it will ever need. But as soon as a program has finished running, Small Basic does a bit of "house cleaning" in the archives, discarding all the information that the program put in there.
Here's an example to demonstrate this: If we first run the program
Area = 6 * 8
TextWindow.WriteLine(Area)
the first line causes the computer works out that 6 * 8 equals 48, and store that in memory. The last line goes back to check what was stored under the name of Area, and displays it in the text window. Everything just like described in detail above.
Now try and remove the first line, so there's only
TextWindow.WriteLine(Area)
left: This time around, the drawer labeled Area was "house cleaned" after the first time the program was run, so even though the result was
once in the archive, it's not there when we run the program again. When asked to look up a drawer labeled Area, the computer returns from the archive "empty-handed", with nothing. And that's exactly what gets written on the text window: Nothing (except for that ubiquitous "Press any key to continue...")
EditWhy variables are variable
A Small Basic program cannot itself "clear out" or
remove variables, but it can
change the contents of a variable. Here's a simple example:
MyVariable = "something"
TextWindow.WriteLine("The variable MyVariable now contains " + MyVariable)
MyVariable = "something else"
TextWindow.WriteLine("The variable MyVariable now contains " + MyVariable)
The first line in this example stores the word "something" in a variable (an "archive file drawer" in memory) named
MyVariable, then the second line retrieves it and writes it out onto the text window, just like in the above examples, only this time it's just a word, a little bit of text, rather than a number.
In the third line, the
same MyVariable gets assigned a
new, different piece of information: When the computer is sent off to the archive to store "something else", it finds that there's already a drawer called
MyVariable. In this situation, it
replaces whatever was in that drawer up until now, with the new information - the old information (in this case "something") is lost, and the new information ("something else" in this example) takes it's place.
EditNaming variables
The name for a variable can be almost any mix of letters, digits, and the character _ (called "underscore"); the only exception is that it cannot begin with a digit. Besides, Small Basic doesn't worry about UPPER and lower case; the names AREA, area and Area all refer to one and the same variable.
windows 8EditRead More
*
buy backlinks
*
backlink services
*
edu links
*
link building services
*
google backlinks
*
windows 8Contoh Surat Pengunduran Diri
Contoh surat lamaran kerja
Contoh daftar riwayat hidup
Status lucu
Kata Kata Gombal
Kata kata galau
Kata Kata Gombal OVj
Model Rambut 2012