|
Each | ||||
For Each < Object() > ... Next | ||||
Parameters: NONE | ||||
Returns: NONE | ||||
The Each keyword is used in denote the linked list variation of the For/Next looping structure. While the normal For/Next loop provides a mechanism for iterating a fixed number of times, ie from a start value to the known end value. The For Each/Next variation is for looping through linked lists of unknown quantity. While the each keyword is used within the For/Next frame work, the syntax for iterating through a linked list is different. The most immediate change you'll notice, is that we no longer need a CounterVar, or the Start and End points for that matter. Why ? - Well this is because the For Each / Next loop structure is managing the current list position internally, with the list position found inside the type variable container itself. In other words, it keeps track of where it is within the list invisibly to us. When you think about, it's often not important to known where we are in the list, or how many objects the list contains. We just want to run through them and apply some process to them. Which is the beauty of a linked list. So what does a For Each declaration look like? - Since the For each controls are built to work with typed variables, we therefore need a declare a type and dim a variable with list support. But lets just imagine, we've a type variable delcared already called Friends. To set up a for each/next loop to process (display their names) of all the friends contained within the friends list, might look like this. Sample For Each / Next loop
Simple huh. FACTS: * For Each must be paired with a closing Next statement. The Next statement doesn't require a variable * You can exit from For Each/Next loop anytime with the Exit statement. * A List() variables internal pointer will be NULL (invalid) at the conclusion of a For Each /Next loop * For/Each loops change the List() variables internal (current object) pointer. Mini Tutorial #1: This example uses the linked list mechanism to store a list of our of friends names. Before we can get the iterating through them, we first have to declare our tPerson type, define our Friends variable as a list and add some names to the list. Once that's done, it's just matter of using our For Each / Next controls to run and print them out.
Mini Tutorial #2: This example is similar to the previous one, expect this time we're creating something a little more like you'd see in a game to handle object spawn and display. The first step of the example is to initializes a tObject type, which will hold the various properties about each character together, such as it's position and colour in this case. Next we dimension a typed variable called Alien with list support. We'll use the Alien variable to manage as many aliens as we need through the one interface. In this example, we're looping through and adding 5 randomly position objects to the list. And finally when we're done we use the For each/next loop to ran through and display them
Mini Tutorial #3: This a more robust example of how Link list management can be used to control an almost infinite number of objects in your program. The example demonstrates how to add new objects to a list, process them (in this case move and draw them) and how to remove objects that are no longer needed.
|
Related Info: | Continue | Do | Exit | ExitFor | List | Loops | Next | Repeat | Step | Types | While : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |