按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
'ending condition': Defines the ending state that will terminate the looping。 An example
loop termination is when a counter reaches the maximum length of an array; and thus no
more elements can be referenced。
'step size': The size of the step that the counter should take。 By default; the size of a step
is 1; indicating an incrementing counter value。 If the step size were …1; the counter would
decrement。 A decrementing counter is OK; but you must remember to assign appropriate
starting and ending conditions; where the ending condition has a lesser value than the
starting condition。
The main purpose of the For loop is to generate a series of numbers that could be used as
indexes to an array。 In the case of iterating over the array; it generated a series of numbers (0; 1;
2; 3; and so on); and each number was then used to reference an array element in Me。root。
■Note The rule of thumb for a For loop is that it is employed to generate an index series that is used to
reference another piece of information。 The index series could be a direct array element reference; or it could
be used to perform a calculation; which is then used to generate a reference to a piece of data。 The index
series does not need to generate incremental or decremental values。 The index series does need to generate
a logical index series。
Using the If Statement
When the starting point city has been found; the tree will begin to search down the tree。 A
depth…first search means that the search will travel down the tree as far as it can before back
tracking and trying other routes。 The recursion of traveling down the tree is managed by the
FindNextLeg() method; which is defined as shown in Figure 4…16。
…………………………………………………………Page 128……………………………………………………………
106 CH AP T E R 4 ■ L E A R N IN G AB OU T D AT A S TR U CT U R E S; DE CI SI ON S; A N D L O OP S
Loop used to generate the index Function that determines whether or not the search can
series for the Connections array continue to the next Connections array element
Private Function FindNextLeg(ByVal returnArray As Node(); _
ByVal count As Integer; _
ByVal destination As String; _
ByVal currNode As Node) As Boolean
For c1 As Integer = 0 To currNode。Connections。Length 1
If CanContinueSearch(returnArray; currNode。Connections(c1)) Then Assume that you can go to the
returnArray(count) = currNode。Connections(c1) connection; so add it to the
If currNode。Connections(c1)。CityName。pareTo(destination) = 0 Then
Return True found route array
Else
If FindNextLeg(returnArray; count + 1; destination; _
currNode。Connections(c1)) Then If the current connection is the
Return True
End If end; stop searching down the
End If tree and return
End If Current connection is not the end; so go to
Next the connection and find another flight leg
Return False
End Function that will bring you to the destination
Figure 4…16。 FindNextLeg() looks for the next leg in the journey。
The big idea here is to create a flight route by traveling the tree of connections in the hope
that one of the connections will cause you to end up at your end point。 Notice that for each leg;
the parameter count is incremented; so as you progress a level deeper in the tree; you assign
the city at the level to the found route array。
What makes this function tick is the decision code represented by an If code block。 The If
code block says; “If this decision test is true; then execute the code within the If block; other
wise; move to the code immediately after the If block。”
An If statement has the following form:
If 'condition' Then
'Do action'
ElseIf 'condition' Then
'Do action'
Else
'Do action'
End If
The statements If; ElseIf; and Else together represent one piece of logic (for example; if
this cannot happen; then test the ElseIf; if that cannot happen; then do the default in the Else)。
The statements after the first If are optional。
The 'condition' must return a True or False value。 A True value means to execute the actions
within the block; and a False value means to try the next code statement。
The Else statement is a sort of default catchall that is executed if none of the other If state
ments prove to be true。
Here is an example of logic executed in an If statement:
If test1 Then
' Code1
ElseIf test2 Then
' Code2
…………………………………………………………Page 129……………………………………………………………
CH AP T E R 4 ■ L E A R N I N G A B OU T D AT A S TR U CT U R E S; DE CI SI ON S; A N D L O OP S 107
El