In this module we will look at conditional loops, and how they extend your ability to write smarter and more efficient programs. STEP 1 - where we introduce while loops and explain the principles Load and run the example file "Adam is exploring". This is what the program file looks like: execute { new basic_bot adam; tell adam: { iterate 8 times { while (frontisclear) { Move 1; } iterate 3 times turnleft; } } } The new thing now is that Adam is moving around AS LONG AS there are no obstacles in his way. Remember, earlier we did a one time check using the if statement, and in some cases that was not really enough (Eve went head first in the wall and sadly terminated). We can use a continous conditional check, called a WHILE LOOP, to help out. A general way of expressing a while loop is: while (condition){ action } Where THE CONDITION is to be enclosed in normal brackets "( )", and can consist of any of the valid RoBOTL EXPRESSIONS as listed in the Reference Guide. ACTION can consist of one or multiple RoBOTL statements, e.g. Move. The total ACTION can consist of multiple statements, in that case you have to remember to enclose the block of statements with the curly brackets "{ }". If you have not yet checked out the reference guide you could open another Browser window and do it now. Examples of conditions we can use are: (FrontIsClear) proceed if there is no obstacle in front of robot (BeepersInBagSTEP 2 - where we modify "Eve circling around" to let Eve make safe turns Load the example file "Eve Circling Around". With your new knowledge of conditional loops, you should be able to change the program, so that Eve can stroll all the way up and down the wall - without your having to count a single square. It doesn't even matter where she starts; she'll make a short cut to a wall, and safely follow the wall around! Did it work all right? Note that the order in which you do things matters a whole lot. Try out different options, and do your best to guess what will happen before you run the program. The better you understand how loops work, the easier it will be to write more complex programs (in any language, actually). If you want additional information about WHILE LOOPS, check out the Reference Guide. STEP 3 - in which we rewrite the "nasty" program from module 4 The idea is for us to let the robots move relatively freely, without having to count their steps at all times. Look at this part of the code from module 4. This is where Adam winds his way to pick up beepers: tell Adam: { iterate 2 times turnleft; move 3; turnleft; move 5; turnleft; move 1; iterate 3 times turnleft; move 5; iterate 3 times turnleft; iterate 6 times { if (BeepersOnFloor > 0) pickbeeper; if (FrontIsClear) move 1; else iterate 3 times turnleft; } } Now look at the modified version, that uses while loops to check for obstacles and lose beepers: tell Adam: { iterate 3 times turnleft; while (BeepersInBag<6) { while (FrontIsClear) { while (BeepersOnFloor) { pickbeeper;} move 1; } turnleft; } } As with IFs, we can use nested WHILEs. In the example above we are 1) proceeding with the search for as long as Adams beeper bag contains less than 6 beepers 2) picking up any beeper on the floor - thus if a square held more than one beeper we would get all in one scoop 3) moving a step forward if there are no obstacles You may wonder about this loop testing for BeepersInBag. If you change the total number of beepers available, and make it LESS than 6, this program will enter an eternal loop - a big flaw we would want to avoid. For now - just leave it like that. Work your way through the exercise, and step on to module 6 for a more complex World. EXERCISE Modify the "Spring Cleaning in Eden" program and try to eliminate all instances where we are counting squares for moves. You can paste in Adam's tell-statement from above as a start, and use that as a guideline for what to tell Eve. If you need additional help, a program that solves the task is available via the View Code/Load Code option. We highly recommend that you try out a few versions before looking at the example. KEYWORDS BeepersInBag, Operators, While
Send questions and comments to: Karen Lemone