Understanding Loop Structures


Unlike decision structures, which control whether other statements execute, loop structures control how often other statements execute.

There are just three loop structures, even though the last in this list has several variations:

For Next.      A For loop is used to work through a known number of items. The number of times the loop should run is specified by means of a counter variable, which typically increments by1each time the loop repeats.

The following code loops through a task's resource assignments and displays the name of each resource in its own dialog box:

 Dim intCount As Integer     For intCount =1To ActiveProject.Tasks(1).Assignments.Count     MsgBox ActiveProject.Tasks(1).Assignments(intCount).ResourceName Next intCount 
Tip  

Use the Count property       Even though the number of items in a For loop must be known, that number doesn't have to be known by you, the programmer. In this code, the Assignments collection's Count property is used to supply the value for intCount.

You can use the Step argument to increment by values greater than 1, or you can use it to decrement the counter. If you decrement your loop, of course, the initial value of the counter must be set to the number of items, instead of to one. This code works like the previous example, but in reverse order:

 Dim intCount As Integer     For intCount = ActiveProject.Tasks(1).Assignments.Count To1Step 1     MsgBox ActiveProject.Tasks(1).Assignments(intCount).ResourceName Next intCount 

For Each Next.      This is essentially a For loop for objects, although it can also be used for items in an array. In this case, the code behaves identically to the first For...Next example, but uses an object variable instead of a numeric counter:

 Dim objAssign As Assignment     For Each objAssign In ActiveProject.Tasks(1).Assignments     MsgBox objAssign.ResourceName Next objAssign 

Quite often, loop structures contain decision structures. The following code loops through every task in a project and displays a dialog box stating whether the task is complete:

 Dim objTask As Task     For Each objTask In ActiveProject.Tasks     If objTask.PercentComplete = 100 Then         MsgBox "The task is complete!"     Else         MsgBox "The task is not finished."     End If Next objTask 

Do Loop.      A Do loop is really a combination of an If Then statement and a For loop for which you don't know how many items there are. It executes code an undetermined number of times, but is limited by the results of a test it performs for each repetition.

The two most common forms of a Do loop are the Do While Loop and Do Until Loop structures. A Do While loop runs code as long as the condition is True, whereas the Do Until loop runs code only while the condition is False (that is, until it's True). Compare the following two examples:

 Sub WhileTest()     Dim intCount As Integer         intCount = 1     Do While intCount <= 10         MsgBox intCount         intCount = intCount + 1     Loop End Sub     Sub UntilTest()     Dim intCount As Integer         intCount = 1     Do Until intCount > 10         MsgBox intCount         intCount = intCount + 1     Loop End Sub 

In each case, 10 dialog boxes appear, incrementing from1to 10. The difference is that the Do While loop runs while the variable is less than or equal to 10. The Do Until loop runs until the variable is greater than 10.

Note  

Unlike a For loop, it's possible that the statements in a Do loop won't be executed because the result of the conditional test might never be true. If the variable intCount in the two procedures had been set to 11 instead of 1, neither loop would run.

The other two variations of a Do loop are the Do Loop While and Do Loop Until structures. Whereas the more "traditional" Do loops test the condition statement before proceeding, these variants perform their actions and then test to see whether a condition is true. The following code always displays a dialog box, even though the condition is never true:

 Dim intCount As Integer     intCount = 100 Do     MsgBox intCount     intCount = intCount + 1 Loop While intCount <= 10 



Microsoft Office Project 2003 Inside Out
Microsoft Office Project 2003 Inside Out
ISBN: 0735619581
EAN: 2147483647
Year: 2003
Pages: 268

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