Logic Basic tutorial for beginners
Arrays

Arrays are a set of variables that have a unique name, and each element of an array has an index identification. An array is declared as follows: the array name with a numerical value in parentheses and their type:

Variable VarName(20) String, Age(20) Integer

The above command line creates two arrays: CustomerName with 20 variables and Age with 20 variables. The index of an array starts with the value 0 (zero), so in the example above, the index ranges 0-19. The following is an example of how to put and get text and numbers in arrays:

Variable VarName(20) String, Age(20) Integer

VarName(0) = "Priscila"
Age(0) = 10
VarName(1) = "John"
Age(1) = 20
VarName(2) = "Sarah"
Age(2) = 16

In this example values ​​were assigned only to arrays with index 0-2, but you can assign values to arrays with index 0-19.

You may have noticed that two arrays were created with the same amount of indexes, and each index of the array VarName was associated with the same index number of the array Age. This type of association is important because it allows you to get various information, of a person for example, with a single index.

To get a value from an array, you should write the name of the array and its index in parentheses, eg:

Write VarName(2)

If you put this command at the end of our previous example, the result will be Sarah.

In the array index you can also place numerical variables, which as its name says, being variables, it makes arrays a powerful scheduling feature. Below, an example of listing the names on the window using a variable as an index of the arrays:

Variable VarName(20) String, Age(20) Integer

VarName(0) = "Priscila"
Age(0) = 10
VarName(1) = "John"
Age(1) = 20
VarName(2) = "Sarah"
Age(2) = 16

X = 0
While X <= 2
  Write VarName(X), " - ", Age(X)
  X++
Loop

When running the program for this example, will be listed in the active window the first three names with their ages.

An important rule is: in the array index you can put numbers or variables, but not complex expressions. If you need to put an expression in the index of the array, assign it to a variable and then enter it as an index.


Back to index      Next topic