TextWindow.Write("Hello ") + ", ") TextWindow.Write(name2 + ", ") TextWindow.Write(name3 + ", ") TextWindow.Write(name4 + ", ") When you run this you’ll get the following result: Figure - Not arrays Clearly there must be a better way to write such a simple program, right? Especially since the computer is really good at doing repetitive tasks, why should we bother with writing the same code over and over for every new user? The trick here is to store and retrieve more than one user’s name using the same variable. If we can do that then we can use a For loop we learned in earlier chapters. This is where arrays come to our help. What is an array? An array is a special kind of variable which can hold more than one value at a time. Basically, what it means is that instead of having to create name1, name2, name3, name4 and name5 in order to store five user names, we could just use name to store all five users’ name. The way we store multiple values is by use of this thing called For example, name[1], name[2], name[3], name[4] and name[5] “index.” can all store a value each. The numbers 1, 2, 3, 4 and 5 are called indices for the array. Even though the name[1], name[2], name[3], name[4] and name[5] all look like they are different variables, they’re in reality all just one variable. And what’s the advantage of this, you may ask. The best part of storing values in an array is that you can specify the index using another variable which – allows us to easily access arrays inside loops. Now, let’s look at how we can put our new knowledge to use by rewriting our previous program with arrays. For i = 1 To 5 TextWindow.Write("User" + i + ", enter name: ") name[i] = TextWindow.Read() EndFor TextWindow.Write("Hello ") For i = 1 To 5 TextWindow.Write(name[i] + ", ") EndFor
TextWindow.Write("Hello ") + ", ") TextWindow.Write(name2 + ", ") TextWindow.Write(name3 + ", ") TextWindow.Write(name4 + ", ") When you run this you’ll get the following result: Figure - Not arrays Clearly there must be a better way to write such a simple program, right? Especially since the computer is really good at doing repetitive tasks, why should we bother with writing the same code over and over for every new user? The trick here is to store and retrieve more than one user’s name using the same variable. If we can do that then we can use a For loop we learned in earlier chapters. This is where arrays come to our help. What is an array? An array is a special kind of variable which can hold more than one value at a time. Basically, what it means is that instead of having to create name1, name2, name3, name4 and name5 in order to store five user names, we could just use name to store all five users’ name. The way we store multiple values is by use of this thing called “index.” For example, name[1], name[2], name[3], name[4] and name[5] can all store a value each. The numbers 1, 2, 3, 4 and 5 are called indices for the array. Even though the name[1], name[2], name[3], name[4] and name[5] all look like they are different variables, they’re in reality all just one variable. And what’s the advantage of this, you may ask. The best part of storing values in an array is that you can specify the index using another variable – which allows us to easily access arrays inside loops. Now, let’s look at how we can put our new knowledge to use by rewriting our previous program with arrays. For i = 1 To 5 TextWindow.Write("User" + i + ", enter name: ") name[i] = TextWindow.Read() EndFor TextWindow.Write("Hello ") For i = 1 To 5 TextWindow.Write(name[i] + ", ") EndFor
Introducing%20Small%20Basic.docx