Flow ControlLoops


The previous section discussed one form of flow controlconditionals. It's called flow control because the code that gets executed after the conditional is dependent on what the conditional evaluates to: either true or False. It's like a switch on a train track where a single track splits into two separate tracks. The position of the switch determines which track the train will take. There's another kind of flow control that involves doing the same task one or more times in sequence. In this case, the program loops and repeats a certain series of steps, based upon some condition.

Do...Loop

The simplest loop of all is the Do...Loop, which kind of reads like a sentence: "Do loop." Because no other information is passed, REALbasic just starts to loop and will loop indefinitely unless you exit from the loop in some way. As you loop and perform your calculations, after you have met the condition you want, you use Exit to exit the loop.

Dim x as Integer Dim anArray as Integer x = 0 Do   x = x + 1   If x = 50 Then     Exit   End If   anArray.append(x) Loop 


The last number in the array is 49.

Do...Loop Until

As in the previous example, you most often loop until some kind of truth condition is met, so there are two variants of the Do...Loop statement, which take into consideration this truth test: the Do Until...Loop statement and the Do...Loop Until statement. Do Until does the truth test at the beginning of each cycle of the loop, whereas the Do...Loop Until does the test at the end. You can almost always use whichever you choose, but there are some times when it makes more sense to use one over the other.

Dim x As Integer Dim anArray(-1) as Integer x = 51 Do   x = x * 2   anArray.append(x) Loop Until x > 50 


There is only one item added to the Array, and the value is 102.

Do Until...Loop

Now, do the same loop, but use Do Until...Loop instead.

  Dim x As Integer   Dim anArray(-1) as Integer   x = 51   // No values will appear in the ListBox1 because the value of x is tested at the beginning of the loop   Do Until x > 50    x = x * 2    anArray.append(x)   Loop 


No values appear in the Array. This is the key difference between the two and why you have to pay attention when using them; where the test is made makes a big difference in the outcome.

For Each...Next

For Each...Next iterates through each element in an Array. Declare a variable of the same type as the Array and use it as a temporary holder for each item in the Array:

Dim anArray(-1) As String Dim newArray(-1) As String Dim s As String anArray = Array("one", "two", "three", "four") For Each s in anArray   newArray.append(s) Next 


This creates an exact copy of the first Array. It returns four rows, one for each item in the array. When using this construct, you do not need to concern yourself with how many items are in the Array, nor do you have to keep track of any numbers.

For...Next...Step

There are times, however, when you need to loop through something that is not an Array and for which you cannot use the previous construct. One example is if you wanted to identify where in a string a particular character appears. There are built-in functions to help you do this in REALbasic, but I'll show you how to do it with a For...Next loop.

Dim aString as String Dim anArray(-1) as String Dim x,y as Integer Dim result as integer aString = "abcdefg" anArray = split(aString, "") y = Ubound(anArray) For x = 0 to y If anArray(x) = "c" then   result = x + 1 // result is 3   Exit End if Next 


There are a few things to point out in this example. The first is that I add 1 to the value of x when assigning the result. This is because arrays are zero based, but that is typically not how we identify the position of characters in a string in everyday life (nor in the not-everyday life of REALbasic, either, when working with EditField controls). Because c is the third character of the alphabet, I add one to the value of x.

Second, I have used Exit to terminate the loop early. If I know there is only one value I'm searching for, or if my goal is to find only the first value, then it does me little good to continue to iterate all the way to the bitter end. Exit solves that problem.

Moving Up and Down: To and DownTo

Dim anArray(-1) as String Dim newArray(-1) as String Dim s as String Dim x, y as Integer Dim result as String anArray = Array("one", "two", "three", "four") y = Ubound(anArray) For x = 0 To y   newArray.append anArray(x) Next For x = y Downto 0   newArray.append anArray(x) Next result = Join(newArray, " ") // result = "one two three four four three two one" 


Adds eight lines altogether, from one to four and then from four back down to one.

Skipping Rows: Step

By default, For...Next loops increment or decrement in steps of one, but it doesn't have to be that way. In fact, even though the previous examples have all used integers, you are not required to use integers and can just as easily use doubles. Step determines how much you increment the value represented by the variable x in these examples.

For example, if you wanted to read every other item in an Array, you would write it like this:

Dim anArray(-1) as Integer Dim x,y as Integer Dim sum as Integer sum = 0 anArray = Array(1,2,3,4,5,6) y = Ubound(anArray) // equals 5 For x = 0 To y Step 2     sum = sum + anArray(x) Next 


In this example, you iterate through six numbers, incrementing the value for x by 2 each time. The first time you go through the loop, x is equal to zero. The value for anArray(0) is 1, so the variable sum is equal to 1. When it loops through the second time, x is now 2 and the value for anArray(2) is 3, and this value is added to the sum variable, and so on through anArray(4) until the value for sum equals 9. The last item in the array is not accessed because when 2 is added to 4, the value is 6, which is outside the bounds of the array.

While...Wend

While...Wend works very much like Do...Loop. The expression is evaluated at the start of each loop with a slight difference. Do...Loop will execute a loop UNTIL an expression evaluates to TRue, which means that as soon as it is true, execution of the loop stops. While...Wend loops while a condition is true. Much like Do...Loop, you have to pay attention to when the expression will evaluate to TRue.

Dim myArray(-1) as String Dim newArray(-1) as String Dim x, y as Integer myArray = array("one", "two", "three", "four") y = ubound(MyArray) x = 0 // This While...Wend statement works While (x <= y)   newArray.append MyArray(x)   x = x + 1 Wend x = 0 //This While...Wend throws an error While (x <= y)   x = x + 1   newArray.append MyArray(x)   Wend Exception err   ListBox1.addRow str(x) 


It's best to look at this output directly.

newArray(0) = one newArray(1) = two newArray(2) = three newArray(3) = four newArray(4) = two newArray(5) = three newArray(6) = four newArray(7) = 4 


As you can see, when the value of x is incremented before the Append method is called; the loop displays the values as expected. When the value of x is incremented after the Append method is called, the loop starts with two, then throws an error on the last loop.

It is important to pay attention to when and where the test expression is evaluated.

Using Loops

When a loop executes in REALbasic, it takes over your application and the user is not able to interact with the user interface at all. For short loops, this isn't even noticeable to the user, but longer loops can make the interface slow and jerky. The way to solve this is to put long loops into a thread, which is discussed in more detail in Chapter 5, "Designing a Desktop Application."

Nesting Loops

All these loops can be nested. Again, this is best shown in an example:

Dim m,n,x,y as Integer n = 10 y = 10 for x = 0 to y   for m = 0 to n      If m = 5 Then         Exit      End If   next   // program starts here after Exit is called next 


The Exit statement causes looping to stop prior to meeting any of the conditions you've established for the loop. The program starts executing right after the loop in which the Exit statement was called. If you have a loop nested inside another loop and you call Exit inside the nested loop, the program will continue execution within the outer loop.




REALbasic Cross-Platform Application Development
REALbasic Cross-Platform Application Development
ISBN: 0672328135
EAN: 2147483647
Year: 2004
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net