Nesting and Arrays – AS Computing Revision (F452)


A two-dimensional array stored as a one-dimens...

A two-dimensional array stored as a one-dimensional array of one-dimensional arrays (rows). (Photo credit: Wikipedia)


  • An array is a contiguous block of memory divided into cells of the same data type
  • The array is like a list, and the cells are the items on the list
  • Contiguous means they’re all stored next to each other in memory
  • You can have an array of booleans or an array of strings, for example, but not an array with booleans and strings in it
  • The cells are all stored under the same identifier – the identifier of the array – but can be accessed individually, using their index number
  • Arrays can start at either 0, or 1 in the exam
  • The length of an array cannot be changed during the program – you can only change it whilst the program isn’t running, by editing the source code and then recompiling
  • An element of an array can be accessed using Identifier[index], where Identifier is the identifier given to the array, and index is a literal or expression that evaluates to an integer
  • [Square] brackets are used in many languages, but round brackets (parentheses, like these) may be used in the exam
  • Array cells can be used in expressions, just like variables
  • Don’t try to access a cell that doesn’t exist – doing so will create a runtime error
  • Using arrays is a lot better than using loads and loads of variables, as it makes the code a lot more tidy!

Multidimensional Arrays

  • Standard, one-dimensional arrays are like a long list (vertical or horizontal)
  • Two-dimensional arrays are like a table
  • The cells in two-dimensional arrays are referenced using two index numbers – one for the row and one for the column, like this:
  • TwoDimensionalArray[3, 5] = 0
  • That statement would assign the value 0 to a cell in TwoDimensionalArray (which must be an array of numbers, if it can accept a 0)
  • My notes say that two-dimensional arrays are referenced row, then column i.e. how far down, then how far accross, which is the other way ’round from what you’d do when plotting something on a graph
  • Thus, the example above would put the 0 in the cell five along and three down
  • What order they’re referenced in doesn’t really matter most of the time, though, because you usually just have to make sure you check every cell, and the order in which you do so is unimportant
  • It has been known for a question to specify that a two-dimensional array is referenced column then row (normal way ’round)… it’s another of those things the exam board won’t make its mind up about!
  • There are more possible dimensions arrays can have, but fortunately that won’t happen in AS Computing F452

Looping Through

  • One of the best things about arrays is that you can go through them automatically using iteration (or recursion, I suppose…)
  • The indexes are sequential numbers and the array is of a fixed length, which makes loops perfect to go through arrays
  • FOR loops are used most often, just because they’re the easiest for counting, but WHILE and REPEAT can also be used
  • If you don’t know the length of an array, you can get away with writing “Length Of” followed by the array name, in your pseudocode
  • In the loop, to refer to a different cell each time, use the counting variable as the index
  • I don’t normally provide example code, but I think some wouldn’t hurt now:

FOR i = 0 to length of SomeArray
    SomeArray[i] = “Hello”
    NEXT i

  • What that bit of code did was fill every cell in SomeArray with the string “Hello” (SomeArray must be an array of strings)
  • Note that it’s starting at 0 and using [square] brackets
  • Arrays in the exam may start at 1 and / or use parentheses (‘normal brackets’, like these, to most people)


  • Using a construct within another construct is called nesting
  • You can nest any construct within any other construct (they don’t have to be the same type, or anything like that…)
  • You can also have many levels of nesting
  • We couldn’t have a post about arrays and nesting without mentioning how to iterate through two-dimensional arrays using loopception!
  • One loop iterates through the rows, and a loop within that iterates through the columns (usually), ensuring every cell is visited
  • When using a WHILE within a WHILE (or a REPEAT within a REPEAT, I guess), remember to initialize the inner one within the outer one!
  • I’m not sure if it’s technically nesting, but you can create a really long IF statement such as IF THEN… ELSE IF THEN… ELSE IF THEN… ELSE… END IF if you need more options than a standard if statement gives you, but you’re not using a CASE statement, for some reason…

Declaring An Array

  • Sometimes you’ll be asked to do it in a high-level language, or sometimes in pseudocode
  • There aren’t really any rules for declaring arrays in pseudocode, and they’ll probably make syntax error allowances for your high-level language attempts, too, as long as you do the following:
  • You have to give it an identifier
  • …so that there’s some way to refer to it
  • You have to give it a length
  • …so that the computer knows how much memory to reserve
  • You have to give it a data type
  • …so that the compiler knows what rules apply to it in calculations (and is able to spot where you’ve made an error, adding it to the wrong data type, for example)
  • It should also be made clear how many dimensions the array has
  • …so that the index numbers have meaning / make sense
  • You might have to set up / initialize the array, too (a loop could be used to initialize)

About Matt

I like writing, filmmaking, programming and gaming, and prefer creating media to consuming it. On the topic of consumption, I'm also a big fan of eating.
This entry was posted in AS Computing Revision, Revision and tagged , , , , , , , , , . Bookmark the permalink.

Enter comment:

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s