Small Basic中文站-快乐编程

查看: 7470|回复: 1


发表于 2010-6-16 17:48:43 | 显示全部楼层 |阅读模式
本帖最后由 颠倒黑白 于 2010-6-16 17:55 编辑


  • 充当计算机内存的 "档案馆"
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 * 8it 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.

2. 变量不会自己注销 (除非程序被终止)注意这个Area变量等于48是不会从内存中移除的,他会一直停留在那里,直到我们把程序终止,变量在他存活期内可以重复获得新的赋值. 但当程序终止后,SmallBasic就会进行一场内存大扫除,清空程序中所有的变量和被赋予的数值。

来看看实例吧, 在程序运行之初我们执行了变量定义

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...")

3. 为什么变量是可变的

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.


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.


使用道具 举报

发表于 2013-8-5 11:38:20 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册


手机版|小黑屋|Small Basic中文站-快乐编程 ( 闽ICP备09051788号 |

GMT+8, 2017-6-23 18:26 , Processed in 0.242444 second(s), 17 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表