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 } while (jed.isFlower(AHEAD)) { jed.hop(); jed.pick(); // Keep picking flowers as long as there are flowers ahead }
This could be changed into an original method that any Jeroo could use as follows:
method pickARow() // Pick a row of flowers that is somewhere ahead { while( isClear(AHEAD)) { hop(); // Keep hopping as long as it is clear ahead } while( isFlower(AHEAD)) { hop(); pick(); // Keep picking flowers as long as there are flowers ahead } }
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.