# Karel J. Robot

```Karel J. Robot
Chapter 6
Instructions
That Repeat
run a mile long hurdle race
Old Way
public void runRace()
{ jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
}
run a mile long hurdle race
Old Way:
public void runRace()
{ jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
jumpHurdle();
}
New Way:
public void runRace()
{ for (int i=1; i<=8; i++)
{
jumpHurdle();
}
}
Iteration (Loops)


Loops repeat a set of instructions
Two types of loops:


Definite loops ( for ) perform instructions
a definite number of times.
Indefinite loops ( while ) perform
instructions an indefinite number of times
(until a certain test fails)
for Loops
(Known number of iterations)
General form:
for ( <initialize variable>; <test>; <increment>)
{
<instruction list>
}
for Loops (cont.)



<initialize variable> sets a
variable/identifier to a certain value
(variable is usually count, i, or j)
<test> is a test that is evaluated each
time the body is about to be executed
(when false, loop is exited)
<increment> changes the loop control
variable (usually i++ or i--)
for Loops (cont.)


Example:
for (int i=1; i<=8; i++)
{
jumpHurdle ();
}
This causes Karel to jump hurdle 8
times
for Loops (cont.)


Example:
for (int i=1; i<=5; i++)
{
jumpHurdle ();
}
This causes Karel to jump hurdle 5
times
for Loops (cont.)
Flow of for loops:
1.
2.
3.
4.
5.
6.
Initialization statement executes.
If test is true, proceed to step 3; if test is
false, go to step 6.
Instructions in body of loop are executed.
Increment statement executes.
Program proceeds to statements after loop.
Example: turnRight
public void turnRight ()
{ for (int i = 1; i <= 3; i++)
{ turnLeft();
}
}
Example: Harvest a row of
beepers
public void harvestOneRow()
{ for ( int i=1; i<=5; i++)
{ move();
pickBeeper();
}
}
while Loops


(unknown number of iterations)
General form:
while ( <test> )
{
<instruction list>
}
Loop continues until test is false
while Loops (cont.)


Example:
while (frontIsClear ())
{
move ();
}
Causes Karel to move continuously
until there is a wall in front of it
while Loops (cont.)
Flow of while loops
1.
2.
3.
4.
If test is true, proceed to step 2; if
test is false, go to step 4.
Instructions in body of loop are
executed.
Go to step 1.
Statements after loop are executed.
Building a While Loop


Step 1 – Identify what is true when the loop
is finished (this is always easier than
determining what is false while it is still
executing).
Step 2 – Use the opposite form of step 1’s
result as the <test> for the loop.
Building a While Loop (cont’d)


Step 3 – make progress toward the goal
(completion of the loop) within the
loop.
Step 4 – Do whatever is required before
and/or after the loop to ensure that we
solve the given problem.
Remember to ITCH



Initialize
Test
CHange
Avoid Infinite Loops



In a for, while, or do-while loop, it
is possible for the test to never be
false
When this happens, the loop continues
infinitely
Depending on the compiler, application,
and other circumstances, an error may
occur or the app may crash
Example of Infinite loop
while (facingNorth())
{ pickBeeper();
move();
}
Example
public void clearCornerOfBeepers()
{
while (nextToABeeper())
{ pickBeeper();
}
}
Example
public void goToBeeper()
{
while (! nextToABeeper())
{ move();
}
}
Karel is somewhere in the world facing
south. One beeper is on each corner
between Karel’s current position and
the southern boundary wall. There is
no beeper on the corner on which Karel
is currently standing. Write a method
that will pick up all of the beepers.
Name the method clearAllBeepersToThe
Wall
Nesting Loops

One loop can be nested inside of
another.
Good nesting
Example
public void clearAllBeepersToTheWall()
// corner can have more than one beeper
{
while (frontIsClear())
{
while (nextToABeeper())
{ pickBeeper();
}
move();
}
}