INTRODUCTION TO COMPUTATIONAL MEDIA

WEEK 3: Array Variables, Repeat Loops

Computers can remember many things and do operations on these things many times. This is a great prosthesis for humans who can only keep a few things in memory at once, and get bored doing the same thing over and over again. As the reading for this coming week suggests, cheap dumb iteration is behind the power of evolution and computers can possibly imitate that process. On the other hand the inefficiency of humans protects us from totalitarian strains that want to gather more information about individuals and then enforce their particular code.

For Loops

• Sometimes you need to go one by one through things.
• The for style of repeat loops uses a variable to count how many times to repeat.
• You need to give the incrementing variable a name (conventionally "i").
• You need to set the initial value.
• You need to set an end value.
• Your will need to set how much the repeat loop should increment the variable in each loop.
• For Example
• for(int i = 0; i< 10; i++){ //i will be 0 to start, and will get bigger by 1 until it equals 9 (less than 10)
println("loop " + i);
• }
• These repeat loops can also save many lines of code. If you have many lines of code that look very similar, it might be time for a repeat loop.

Array Variables

• Variables are like containers. Remember I said to imagine that the machine sets aside a coffee cup somewhere everytime you name a variable.
• Sometimes you want to store a couple of things together. Now you should imagine a spice rack or egg carton where you can store a couple of related things together. This is called an array.
• Arrays and repeat loops often go together like peas and carrots.
• An list is like a variable in most ways. It can be named anything and you have to specify what type of thing you want to put into it.
• Square brackets are the telltale sign of an array.
• You can initiate an empty array like this: int[] numbers = new int[10]; for a collection of ten integers.
• You can initiate an array and fill it at the same time like this: String[] names = {"Dan","Dick","Don"}; for a collection of three names. This is the exception of using square brackets for arrays.
• To get something from the array you use the index of the slot you want: println(names[1]) This is will output "Dick" from the previous array. Note that processing starts numbering from 0.
• To set something in an array you use the index again: names[1] = "Richard" so now the second name in the array is replaced with "Richard."

MultiDimensional Arrays

• Sometimes you boxes within boxes.
• For instance you might need to keep track of multiple balls, but also keep track of several things about each ball (eg xpos, ypos, speed, direction).
• The syntax for using multidimensional arrays adds another set of square brackets
• You would initialize like this: int[][] balls = new int[3][4] //this would allow you to keep track of 4 things about 3 balls
• If the array looked like this: [[3,40,50,10], [4,50,70,80], [6,9,20,11]] then asking for balls[2][0] would give you a 6.
• If you said: balls[0][2] = 45; // the 50 would be changed to a 45.