Search code examples
javadrjava

Why won't my loop stop; seemingly despite boolean expression


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!


Solution

  • 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...