# infinite loop has me stumped

• 02-16-2012, 10:18 AM
D.good
Help!!!! infinite loop
Hello folks

So I am working on my first assignment for Stanfords see program. The title of the of the first assignment that I choose to do first that is, is problem 3 checkerboardkarel. Attachment 3038
here is my code for the first leg, as well as my problem
Code:

```/*  * File: CheckerboardKarel.java  * ----------------------------  * When you finish writing it, the CheckerboardKarel class should draw  * a checkerboard using beepers, as described in Assignment 1.  You  * should make sure that your program works for all of the sample  * worlds supplied in the starter folder.  */ import stanford.karel.*;       public class CheckerboardKarel extends SuperKarel {       public void run(){             turnLeft();             layRow(); }   private void layRow(){         while(frontIsClear()){                         putBeeper();                         move();                         if(frontIsBlocked()){                                 turnRight();                 }else                         move();                 } } }```
for some reason this code is creating an infinite loop causing karel to lay beepers around the border.
Can some one please!! point me in the right direction to end the infinite loop thanks
• 02-16-2012, 02:55 PM
Norm
Re: infinite loop has me stumped
Where is the infinite loop? Is it the while() statement?
When do you want the loop to exit? Can you add a test for the condition when you want the loop to exit and issue a break statement?
if(exit the loop condition)
break; // exit the loop

When does the frontIsClear() method return false?
• 02-16-2012, 06:33 PM
D.good
Re: infinite loop has me stumped
Quote:

Originally Posted by Norm
Where is the infinite loop? Is it the while() statement?
When do you want the loop to exit? Can you add a test for the condition when you want the loop to exit and issue a break statement?
if(exit the loop condition)
break; // exit the loop

When does the frontIsClear() method return false?

I'm not sure, I do believe the infinite loop is in the while I just don't understand why. The code I put is my test because I kept running into a infinite loop in the while for some reason the if doesn't break the loop it seems to add to it.
• 02-16-2012, 06:39 PM
Norm
Re: infinite loop has me stumped
When should the loop end? Can you test for when and use a break statement?
To insure the loop ends, use a counter. When the vaue of the counter gets big enough, break
Code:

```int cntr = 0; while....{ if(cntr++ > 200)   break; ... }```
• 02-16-2012, 09:37 PM
D.good
Re: infinite loop has me stumped
I'm learning Java through Stanford engineering every where program. So I haven't learned how to do test yet, and I can't use integers
The while is suppose to end when front becomes blocked then the if statement is suppose to take over. The problem is that the if is fulfilled so I guess then the infinet loop is in the if statement but I'm not sure how
• 02-16-2012, 09:43 PM
Norm
Re: infinite loop has me stumped
or post all of the code that is being used.
For example the frontIsClear() method and move() and turnRight etc

Unless we see all the code there is no way to know what it is doing.
• 02-16-2012, 09:50 PM
D.good
Re: infinite loop has me stumped
i can't ask the instructor its a self paced course. I have pasted all the code I have written that caused the infinite loop
• 02-16-2012, 09:57 PM
Norm
Re: infinite loop has me stumped
Then you have to read the API doc for the methods and classes you are using.
I told you earlier some ways to end the loop.
Without the code for all the classes and methods, there is no way to suggest what is wrong.
• 02-16-2012, 10:01 PM
D.good
Re: infinite loop has me stumped
i don't understand, the code I put is all there is, in the constructs of Karl the robot the above code is the classes and methods but thank you kind sir
• 02-16-2012, 10:03 PM
JosAH
Re: Help!!!! infinite loop
Quote:

Originally Posted by D.good
Code:

```        while(frontIsClear()){                         move();                         if(frontIsBlocked()){                                 turnRight();                 }else                         move();                 }```

Imagine you were the robot and you had to do the above; would'nt you be running in circles (squares) ad nauseam?

kind regards,

Jos
• 02-16-2012, 10:23 PM
Norm
Re: infinite loop has me stumped
Take a piece of paper and draw the maze that the Mover is in.
Place the Mover on the maze and play computer with the program that you have posted. Follow its statements one by one and seeif the program is wrong or if the maze has no solution.
Is it possible that there is not a solution? Can the Mover just go in circles and never escape?
• 02-16-2012, 10:48 PM
D.good
Re: Help!!!! infinite loop
Quote:

Originally Posted by JosAH
Imagine you were the robot and you had to do the above; would'nt you be running in circles (squares) ad nauseam?

kind regards,

Jos

I have done just that, before I came to the forums. Am I using the wrong if statement?
• 02-16-2012, 10:49 PM
D.good
Re: infinite loop has me stumped
ok thanks