Small Basic中文站-快乐编程

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 7716|回复: 1

SmallBasic的变量详解

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

附说明:变量是指在程序的运行过程中随时可以发生变化的量。变量是程序中数据的临时存放场所。在代码中可以只使用一个变量,也可以使用多个变量,变量在内存中的存储变量中可以存放单词、数值、日期以及属性。由于变量让你能够把程序中准备使用的每一段数据都赋给一个简短、易于记忆的名字,因此它们十分有用。变量可以保存程序运行时用户输入的数据(如使用InputBox函数在屏幕上显示一个对话框,然后把用户键入的文本保存到变量中)、特定运算的结果以及要在窗体上显示的一段数据等。简而言之,变量是用于跟踪几乎所有类型信息的简单工具。
回到本文,变量在SmallBasic的应用也一样简单。大部分都是英文的说明,颠倒黑白偷懒了,我只翻译一下大标题和一小部分...



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


4.变量的命名

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 | 显示全部楼层
谢谢!辛苦了邵阳人才网
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2017-12-13 09:33 , Processed in 0.131325 second(s), 8 queries , File On.

Powered by Discuz! X3.3

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