Example:
say in a role playing game (like zelda) i have a game character, and for this character i have to store his:
- life, 10
- majic, 50
- power, 100
- speed, 400
- accuracy, 30
now, i could make 5 seperate integer variables to store all these values, like life = 10, majic = 50, power = 100, speed = 400 and accuracy = 30 which is perfectly ok but say you want to like check if each variable is above a certain value 200, you would have to do something like:
if life > 200 then
if majic > 200 then
if power > 200 then
if speed > 200 then
if accuracy > 200 then
an alternative is to use arrays, you could create a single array to store all this information rather than making 5 seperate variables. sine there are 5 stats we want to store, we need to create an array with 5 spaces. Arrays are created using the
DIM statement, and you can call your array anything you want, i'll call this array "stats", so:
dim stats(4)
you may be wandering why that i have "4" and not "5" in there, well the space stats(0) will be used for life, stats(1) for majic, stats(2) for power, stats(3) for speed and stats(4) for accuracy. so you would have:
stats(0) = 10
stats(1) = 50
stats(2) = 100
stats(3) = 400
stats(4) = 30
now, if you go back to the searching to see if all stats are above 200, you can change the code and write this instead:
for x = 0 to 4
if stats(x) > 200 then
next x
see how easier and neater it is, you may not notice it now but you will as you advance.
--------------------------------------------------------------------
Thats was a 1 dimension array, to visualise a 2 dimension array, say we wanted to store the same stats, life, majic, power, speed and accuracy not for just our main character, but for 10 different characters, you can use the same array to do this. Think about it, 10 characters, each having 5 stats:
dim stats(9,4)
i hope you now understand why i have "9" there and not "10", because 0 is also considered. So the stats of all will be as follows:
character 1
stats(0,0) = 10
stats(0,1) = 50
stats(0,2) = 100
stats(0,3) = 400
stats(0,4) = 30
character 2
stats(1,0) = 10
stats(1,1) = 50
stats(1,2) = 100
stats(1,3) = 400
stats(1,4) = 30
etc till you reach character 10 with stats(9,0) to stats(9,4). Go back to the search to see if all characters have stats above 200, imagine the number of variables we would have had to declare to store this information, and the number of "if" statements we would need to write to check each and every variable, with arrays all this can be done in 5 lines:
for x = 0 to 9
for y = 0 to 4
if stats(x,y) > 200 then
next y
next x
that loops round the entire stats array and checks each individual value is greater than 200. Hope you understood.
phew, i got lots of time on my hands