Loops work best when everything has a pattern. If it requires addition of an offset to a value, that's the best case. If the value can be derived from a function, that's good too. Sometimes you have to use a lookup table if there's no real pattern. Here's what each of the three cases looks like. If you have a grasp of basic algebra, it's a piece of cake. (If you don't, teach yourself! You won't get far in programming without it.)

**Addition of an Offset**
Code:

Dim appleCost As Double = 0.20
Dim currentCost As Double = 0
For i As Integer = 1 To 10
currentCost += appleCost
Console.WriteLine("{0} apples cost {1}." i, currentCost)
Next

If you want to reduce complexity, you can exploit that multiplication is repeated addition:

Code:

Dim appleCost As Double = 0.20
For i As Integer = 1 To 10
Dim currentCost as Integer = appleCost * i
Console.WriteLine(...)
Next

This technique is easy to spot in repetitive if statements; you'll find many values separated by the same amount each time:

Code:

If currentScore < 200 Then
interval = 90
ElseIf currentScore < 400 Then
interval = 80
ElseIf currentScore < 600 Then
interval = 70
End If

The pattern 200, 400, 600 shows a linear increase by 200 each time. The function is f(x) = x * 200. The pattern 90, 80, 70 is a linear decrease by 10 each time; f(x) = 90 - 10(x - 1) is a good way to describe it since we assumed x starts with 1 in the last one. So the loop would look like this:

Code:

For index = 1 To ???
If currentScore < 200 * index
interval = 90 - (10 * (index - 1))
End If
Next

Does your code look like it has a simple linear pattern like this? Spend a few minutes thinking about it before you move on.

**Derivation from a Function**
Right. Your code doesn't have an easy linear pattern. The scores go +100, +100, +600, +300, +900, +600, +700. The intervals go -5, -5, -10, -15, -10, -5, -5. Since the numbers change each time, we'll have to figure out if a function fits.

First, let's see what a loop that uses function derivation looks like. We start with the tedious code to print the first 10 squares:

Code:

Console.WriteLine(1) ' 1 * 1
Console.WriteLine(4) ' 2 * 2
Console.WriteLine(9) ' 3 * 3
...

We know our loop variable's going to go in sequence 1, 2, 3... Can you think of a function that seems like a good fit? What about f(x) = x * x?

Code:

Function Square(ByVal x As Integer) As Integer
Return x * x
End Function

Code:

For i As Integer = 1 To 10
Console.WriteLine(Square(i))
Next

Now look at your pattern. Can you think of a function that produces the patterns? It might help to plot them on paper. Give it a try for a few minutes before going on; you might not need the next one.

**Lookup Tables**
If you try really hard you can probably find a polynomial that fits those points. But polynomial math's slower than linear math, so you don't want to fool with it. When faced with numbers that have no pattern at all (or one that's expensive to calculate), the best you can do is use a lookup table. This consists of creating an array with the appropriate value for each index. The nice thing about lookup tables is they always work and are rarely slower than offsets or functions. The not nice thing is all the typing involved.

For example, suppose I had data about the cost of an apple each month. Odds are these prices aren't related by a simple function, so I'd have to use a lookup table to print each month's data:

Code:

Dim applePrices() As Double = { 0.20, 0.25, 0.80, 0.41, ... }
For i As Integer = 0 To applePrices.Length - 1
Console.WriteLine("In month {0} apples cost {1}.", GetMonth(i), applePrices(i))
Next

This is what you're going to have to do if you don't want to use a more reasonable pattern. It'd look something like this:

Code:

Dim scoreThresholds() As Integer = { 199, 299, 399, ... }
Dim intervals() As Integer = { 90, 85, 80, ... }
For i As Integer = 0 To scoreThresholds.Length
If currentScore < scoreThresholds(i) Then
P1movement.Interval = intervals(i)
Exit For
End If
Next

The Exit For is important. Suppose it's not there and the score is 387. 387 > 199, so index 0 fails the If. 387 > 299, so it fails the if. 387 < 399, so the if is taken and the interval is set. But if we keep going, 387 is also less than 999, 1299, and all of the other scores! So we have to quit as soon as we find a match or do the more complicated test to see if it's between the current and last.