REALbasic 2005r3 was released a few minutes ago, and with it comes a Linux release, a lot of bug fixes, as well as a few new cool features. One thing I enjoyed implementing and immediately using was the Continue statement, as well as extended Exit statement syntax. Partially taken from Visual Basic, we now allow:
Exit // exit whatever loop we're in
Exit Do // exit the do loop we're inside, even if
// we're inside of another loop
Exit For // exit the for loop we're inside, even if
// we're inside of another loop
Exit While // exit the while loop we're inside, even if
// we're inside of another loop
Which is all fine and cool, but then we added an interesting twist tot he “exit for” statement:
Dim map(255,255) As Integer
Dim x, y As Integer
…
For y = 0 To 255
For x = 0 To 255
If map(x,y) = whatImLookingFor Then
Exit For y
End If
Next
Next
You can now identify which for loop you want to exit by specifying the loop variable. In this case, “y” identifies the outer for loop, and so this exit statement will exit that loop.
The Continue statement is another statement that can be used in a loop to immediately jump to the beginning of the loop, and continue executing. For example:
For i As Integer = 0 To 100
If i = 2 Or i = 31 Then
Continue
End If
// Do something with i
Next
This code will reach the “do something with i” portion in every case except for when i is 2 or i is 31. Often the uses for continue vary, but the prime reason for including it is to help decomplexify* code. For example:
Dim goodDate As New Date
Dim foundIdealDate As Boolean
While Not foundIdealDate
If goodDate.Hour > 20 Or _
(dateGettingPlanned IsA FriendlyDinner And goodDate.DayOfWeek = 6) Then
// We don't like planning things past 8 PM, and
// Fridays can't be for friendly dinners
goodDate.DayOfWeek = goodDate.DayOfWeek + 1
goodDate.Hour = 8
Continue
End If
For Each otherDate As Date In AlreadyPlannedDates
// For simplicity, all dates are exactly 1 hour
If otherDate.TotalSeconds < = goodDate.TotalSeconds And _
otherDate.TotalSeconds + 60*60*60 > goodDate.TotalSeconds Then
// In this case, we know we overlap with this date, so we might
// as well set the idealDate to the hour after this
goodDate.TotalSeconds = otherDate.TotalSeconds + 60*60*60
// We don't need to evaluate anymore, we conflicted — just continue
Continue While
End If
Next
// Check a few more things, and finally
foundIdealDate = True
Wend
In this example, we have several points where we hand-calculate the best “next” spot to check. Instead of having a lot of nexted if statements, a few temporary variables, or the dreaded GoTo statement**, we can simply use the new continue statement, and we’ll jump back to the top of the loop, and continue with execution again.
*While on the phone with Mars the other day, he used “my” word, complexify. I was pleased that it’s catching on, although I don’t actually know if he heard it first from me or not.
**GoTo can be used well in certain cases, but every time you’re about to use it, you should consider if there’s a better way
Have fun with r3. Time for me to get to work on r4!