While loops

or... how to keep going until you get where you need to go!


What if you didn't know how big an island was, but you knew that somewhere, straight ahead, there was a row of flowers to be picked.

In the same way that Visual Basic allows a program to loop, Jeroo has a WHILE loop to repeat lines of code as long as a condition is met.

The high-level algorithm is: Keep hopping as long as it's clear ahead, then keep picking flowers as long as there are flowers ahead.

The actual code for a Jeroo named Jed is:

While Jed.isClear(AHEAD)
     Jed.hop()                 ' Keep hopping as long as it is clear ahead
End While

While Jed.isFlower(AHEAD)
     Jed.hop()
     Jed.pick()                ' Keep picking flowers as long as there are flowers ahead
End While
 

This could be changed into an original method that any Jeroo could use as follows:

Sub PickARow()         ' Pick a row of flowers that is somewhere ahead
   While isClear(AHEAD)
      hop()            ' Keep hopping as long as it is clear ahead
   End While 

   While isFlower(AHEAD)
      hop()
      pick()           ' Keep picking flowers as long as there are flowers ahead 
   End While
End Sub 

Open GardenIsland and create the following 4 Jeroos and have them each pick a row of flowers as shown below using the PickARow method defined above. Predict what will happen BEFORE you run the program, then give it a try. the Garden island program as shown below is on P:\Jeroo\Programs