0

So my program runs and draws a row of diamonds starting at the top right. It then moves down a row and repeats. It SHOULD stop at 4 rows (due to canvas size) but it won't!

First for statement draws a diamond. Second for statement moves to the next diamond. Repeat 7 times.

The third for statement (and this is where I'm assuming the issue is..) moves down a row and repeats the first two.

all of which runs perfectly! Right up until it doesn't STOP!

It just keeps repeating indefinitely!

import Media.*;
import static java.lang.Math.*;

public class DiamondTiles2 {

private TurtleDisplayer display;
private Turtle            steve;

public DiamondTiles2 (){

display = new TurtleDisplayer();
steve = new Turtle();
display.placeTurtle(steve);

steve.setSpeed(15);
steve.forward(140);             // Moves Steve to the furthest right he needs to be
steve.left(PI/2);
steve.forward((float)1.5*(40*sqrt(3.0)));  // Moves Steve to the top (up 1.5 times the height of a diamond)
steve.left(PI/6);
steve.penDown();

for (int j=1 ; j<=7 ; j++){  //Diamond Drawing
  steve.forward(40);
  steve.left(2*PI/3);        
  steve.forward(40);
  steve.left(PI/3);
  steve.forward(40);
  steve.left(2*PI/3);
  steve.forward(40);
  steve.left(PI/3);
  for (int i=1 ; i<=1 ; i++){ //Move to Next Diamond
    steve.penUp();
    steve.left(PI/3);
    steve.forward(40);
    steve.right(PI/3);
    steve.penDown();         
    for (int k=1 ; (j>=7)&&(k<=4) ; j=j-7 , k++){ //Move to next row
      steve.penUp();
      steve.right(7*PI/6);
      steve.forward((float)40*(sqrt(3.0)));
      steve.left(PI/2);
      steve.forward(280);
      steve.left(2*PI/3);
      steve.penDown();
    }
  }
}  
display.close();
};
public static void main ( String[] args ) { DiamondTiles2 d = new DiamondTiles2(); };
}

The way I'm seeing it: the last for loop SHOULD start as k==1 and then increase k by one each time. and it SHOULDN'T repeat past 4 because of

for (int k=1 ; (j>=7)&&**(k<=4)** ; j=j-7 , k++)

please help! :)

EDIT: Okay, so j is definitely the problem... to explain WHY I'm resetting J: When I didn't reset 7 at seven it would draw the first row and then it would infinitely loop the step that Moves to a new row. So it would stop drawing and just move "Down, Right, Down, Right, Down, Right... etc.

Adding j=j-7 fixed that issue but started this new one.. Which i THOUGHT would be fixed because of k<=4 NOT being true... any reason it seems to be "ignoring" that Boolean?

EDIT2: Fixed: Changed the last for statement to this IF statement:

 if ((j>=7)&&(x<=3)){ //Move to next row
      steve.penUp();
      steve.right(7*PI/6);
      steve.forward((float)40*(sqrt(3.0)));
      steve.left(PI/2);
      steve.forward(280);
      steve.left(2*PI/3);
      steve.penDown();
      j=j-7;
      x=x+1;

Thank you to everyone who helped me! Amazing response on this site!

Michael MacDonald
  • 413
  • 2
  • 6
  • 17
  • It's not ignoring k -- k gets reset to 1 everytime the outermost loop relaunches the innermost loop. It then iterates from 1-4, but when the outermost loop comes back around... gets reset to 1. – coderabbi Oct 15 '12 at 01:53

4 Answers4

0

Is it possible that the j=j-7 in the line you referenced isn't screwing up your intention in the first for loop on j?

Kirby
  • 3,649
  • 3
  • 26
  • 28
  • Appreciate the speedy reply! Not 100% sure what you mean.. I'm using the j=j-7 to reset j to 0 each time it runs the loop so that it will repeat at 7. Which is working. – Michael MacDonald Oct 15 '12 at 01:43
  • So I realize that j IS still set to 0 at the end of the fourth repeat, but shouldn't it be "overridden" by the fact that k is NOT less than or equal to four? doesn – Michael MacDonald Oct 15 '12 at 01:44
  • Increment happens, and then it checks the condition again. So the value is reset, the check happens, the innermost loop breaks, but the value is still reset in the process. – Kirby Oct 15 '12 at 01:47
0

You increment j in your outermost loop and you reset j in the innermost loop. That seems like asking for trouble. The innermost loop guarantees that j will never be 7 or more, and therefore the outermost loop will never terminate.

I would refactor so that you don't modify j other than in the outer for statement.

I would also recommend stepping through with a debugger to see what's going on.

Trott
  • 66,479
  • 23
  • 173
  • 212
0

It looks like the problem is j=j-7 - your loop will only stop if j is greater than or equal to 7 and k is less than 4. Because j is decremented by 7 on each iteration, your condition can never be met.

Kelvin
  • 5,227
  • 1
  • 23
  • 36
0

It doesn't repeat past 4... in that loop. The issue is that the innermost loop is being run an infinite number of times, so it's looping from 1-4 and then from 1-4 and then from 1-4, etc.

Why is that loop being run an infinite number of times?

It seems that your terminators in the innermost & outermost loops are at odds. Since you subtract 7 from j in the innermost loop every time it increases beyond 7, and the outermost loop will run so long as j is less than or equal to 7, j never is allowed to become large enough for the outermost loop to terminate. This means that the innermost loop is run over and over and over again (because the outermost loop in endless), 1-4, 1-4, 1-4...

coderabbi
  • 2,261
  • 16
  • 18
  • Understood; both of you! But don't BOTH Boolean statements HAVE to be true in order for it to execute? And since K ISN'T less than or equal to 4... it should stop? No? – Michael MacDonald Oct 15 '12 at 01:45
  • It does stop... that time. But then that iteration of the outermost loop ends and the decision is made whether to run again - since j will always be > 7, the outermost loop will never terminate and another set of the innermost loop will be launched (stooping again at 4, of course. Since you declare k in the innermost loop, it's always going to begin at 1 for every iteration of the outermost loop(even if it was at 4 at the end of the outermost loop's previous iteration). – coderabbi Oct 15 '12 at 01:51
  • So... could I add the boolean k<=4 to the innermost loop? Instead of the outer? – Michael MacDonald Oct 15 '12 at 01:54
  • K is part of the innermost loop already. – coderabbi Oct 15 '12 at 01:55
  • I'm not sure the ramifications, because I'm not 100% sure what you want to accomplish in the end, but you might try declaring k to be 1 before all of the loops. Then the declaration of the innermost loop would be: for ( ; (j>=7) && (k<=4) ; j=j-7 , k++) – coderabbi Oct 15 '12 at 01:56
  • sorry, had my inner and outer mixed up :p That's what I was meaning then, to add k to the other loop as well? Is there any way to just force a break at "x" point? I'm very new to programming in general and especially java, so thank you for bearing with me! – Michael MacDonald Oct 15 '12 at 02:00
  • Again, I'm not sure I understand the output you want, but if you want the outermost loop to also end when the innermost k reaches 4, you should set k to 1 before opening the outermost loop and then add if(k==4){break;} right before you close the outermost loop. It's not the cleanest solution, but it should get you where you want to go. – coderabbi Oct 15 '12 at 02:02
  • FINALLY fixed! Not exactly what you said, but you reminded my dumbass self about if statements.... simplest fix! I just changed the last loop to an If statement instead of a for! (See my edit on the OP) Thank you so much for your help! I'd hug you if I could :p – Michael MacDonald Oct 15 '12 at 02:10