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

  
; Declare the For Each /Next loop
  For Each Friends()
     
   ; Display the name of each person in your friends list
     Print Friends.Name$
     
  Next
  



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.

  
  
; Declare the type tPerson with a single field called Name$
  Type tPerson
     Name$
  EndType
  
;  Dim the typed variable FRIENDS with added linked LIST support
  Dim Friends  As tPerson List
  
; Add a new Type to the head of the list
  Friends = New tPerson
; Init this  friends name
  Friends.Name$ = "Olivia"
  
;  Add another person to my friends list
  Friends = New tPerson
  
; Init this  friends name
  Friends.Name$ = "Tess"
  
  
; Run through and display my list of friends with FOR EACH
  For Each Friends()
     Print Friends.Name$
  Next
  
; Display the screen and wait for a key press.
  Sync
  WaitKey
  
  






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


  
  
; Declare type tObject to hold the information about our object
  Type tObject
     x#,y#          ; position
     Colour     ; colour
  EndType
  
; Declare Alien as a link list of type tOBJECT
  Dim Alien As tObject List
  
; --------------------------------------------
; Add 5 objects to the alien list
; --------------------------------------------
  
  For lp=1 To 5
     
   ; Create a new TObject
   ; and add it to head of
   ; Alien list
     Alien = New tobject
     
   ; init it's position
     Alien.x#=Rnd(800)
     Alien.y#=Rnd(600)
     
   ; init it's colour
     Alien.Colour=RndRGB()
  Next
  
  
; --------------------------------------------
; draw the Aliens as circles to the screen
; --------------------------------------------
  
; use FOR EACH to iterate through the
; linked Alien() list
  For Each Alien()
   ; draw this object as a filled circle of radius 10
     CircleC Alien.x#,Alien.y#,10,true,Alien.Colour
  Next
  
  
  Sync
  WaitKey
  
  
  






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.


  
; Declare tObject type to hold the information about our object
  Type tObject
     x#,y#          ; position
     Angle#     ; Direction object is moving in
     Speed#     ; speed
     Colour     ; colour
  EndType
  
  
; Declare Alien variable as type tObject with linked list support
  Dim Alien As tObject List
  
  
  
; Tell PB to limit this programs speed to 60 frames
; per second or less.
  SetFPS 60
  
  
; Start of Do /loop
  Do
   ; Clear the Screen
     Cls RGB(20,30,40)
     
     
   ; Check if the Space Bar was press ?
     If SpaceKey()=true
        
        Alien = New tobject
        
      ; init it's position
        Alien.x#=Rnd(GetScreenWidth())
        Alien.y#=Rnd(GetScreenHeight())
        
        Alien.Angle#=Rnd#(360)
        Alien.Speed#=RndRange#(5,10)
        
      ; init it's colour
        Alien.Colour=RndRGB()
        
     EndIf
     
     
     
   ; use FOR EACH to iterate through
   ; Alien() list and move them.
     For Each Alien()
        
      ; Get the Speed and direction this alien is moving
        Speed#=Alien.Speed#
        Angle#=Alien.Angle#
        
      ; Calc New X & Y coordinates of this alien
        x#=Alien.x#+CosRadius(Angle#,Speed#)
        y#=Alien.y#+SinRadius(Angle#,Speed#)
        
      ; Check if the Alien has left the screen ?
        If PointInBox(x#,y#,-50,-50,GetScreenWidth(),GetScreenHeight())=true
           
         ; If the Alien is still on the screen, lets draw it
         ; as a filled circle of radius 10
           CircleC x#,y#,10,true,Alien.Colour
           
         ; record the aliens new position
           Alien.x#=x#
           Alien.y#=y#
           
        Else
           
         ; This alien is no longer side the screen bounrds
         ; and has thus moved off the screen.  So lets delete it
         ; from the Alien list.
           Alien = null
           
        EndIf
        
      ; Continue processing all of the aliens in the list
     Next
     
     
     CenterText GetScreenWidth()/2,10,"Press Space"
     Sync
  Loop
  
  


 
Related Info: Continue | Do | Exit | ExitFor | List | Loops | Next | Repeat | Step | Types | While :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com