Repeatedly Executing Instructions |
iterate (iteration-amount) { <loop body statement(s)> } |
Example | Explanation |
void TurnRight() { iterate (3) { TurnLeft(); } } |
The primitive instruction TurnLeft() is repeated three times, which is equivalent to a right turn. |
void Harvest1Row() { PickBeeper(); iterate (4) { Move(); PickBeeper(); } } |
Karel would pick a single beeper and then repeat the sequence
Move(); PickBeeper(); four times.
The new instruction therefore would execute
five PickBeeper() instructions and four Move()'s.
|
Curly braces are needed to mark the beginning and end of a loop body only if the body contains more than one statement. If the body consists of only one statement, the braces may (optionally) be omitted. Also, an ITERATE statement is considered a single statement. Here are some example ITERATE statements considered in terms of if they can be shortened by removing braces.
Original Code | Code With Braces Omitted |
void TurnRight() { iterate (3) { TurnLeft(); } } |
void TurnRight() { iterate (3) TurnLeft(); } |
void TraverseSquare() { iterate (4) { iterate (5) { Move(); } TurnLeft(); } } |
void TraverseSquare() { iterate (4) { iterate (5) Move(); TurnLeft(); } } |
(NOTE: The iterate statement is NOT a standard C / C++
statement, although it can easily be simulated using the standard
for statement. The iterate statement has been introduced
to simplify Karel programming.)
The WHILE statement is made-to-order for this situation:
you can use it to tell Karel to repeat something while a certain predicate
is true; for example to pick up beepers while there any to pick up.
while (predicate) { <loop body statement(s)> } |
Example | Explanation |
void ClearCornerOfBeepers() { while (nextToABeeper) { PickBeeper(); } } |
Karel would pick up all beepers on the current corner, regardless how many there are (if it is a finite number, at least). |
void FaceNorth() { while (notFacingNorth) { TurnLeft(); } } |
Karel will turn left as often as necessary in order to be facing North at the end of executing this function. Note that if this function is invoked when Karel is already facing North, zero turn lefts will occur. Note also how much shorter this implementation of FaceNorth() is compared to the implementation that used a series of three IF statements. |
void PickBeepersToWall() { while (nextToABeeper) PickBeeper(); while (frontIsClear) { Move(); while (nextToABeeper) PickBeeper(); } } |
The logic of these nested WHILE statements has Karel
pick up all beepers between him and the wall ahead of him,
including beepers on Karel's beginning street corner.
Karel stops in front of the wall.
This example also illustrates that braces can be omitted if the loop body consists of only one statement. |
do { <loop body statement(s)> } while (predicate); |
Example | Explanation |
void FaceWestIfFacingSouth() { if (facingSouth) { do { TurnLeft(); } while (notFacingWest); } } |
If initially facing South, Karel will turn left until he is facing to the West. |
void PickBeepersToWall() { if (nextToABeeper) do { PickBeeper(); } while (nextToABeeper); while (frontIsClear) { Move(); if (nextToABeeper) do { PickBeeper(); } while (nextToABeeper); } } |
The logic to the left has Karel pick up all beepers between him and the wall ahead of him, including beepers on Karel's beginning street corner. Karel stops in front of the wall. Note the need to guard the DO...WHILE statements with IF statements to prevent an error shutoff if the corners do not contain any beepers. |