1

The very last for loop listed (int variables n and o).. I'm very new to any type of programming and in our introductory course we use Dr Java and some Basic media stuff to make what is essentially a turtle pen. It draws things on a display.

My last for loop seems to (in my eyes) say that it should initiate when n<=o which they are at the first. So it initiates. It should then update so that n=2 which is NOT <=o (1).. but yet it just keeps infinitely looping the very last part...

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 m=1 ; m<=4 ; m++){
    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++){
        steve.penUp();
        steve.left(PI/3);
        steve.forward(40);
        steve.right(PI/3);
        steve.penDown();
        for (int k=1 ; j>=7 ; k++){
          for (int n=1, o=1 ; n<=o ; n=n+2){
            steve.right(7*PI/6);
            steve.forward((float)40*(sqrt(3.0)));
            steve.left(PI/2);
            steve.forward(280);
            steve.left(2*PI/3);*
          }
        }
      }
    }
  }  
  display.close();
};

Basically the end result should be that the last loop (n,o) only initiates once every 7 loops of j. What it does is draw 7 diamonds and then the n,o loop moves it down to start a new row where it would then draw another 7 diamonds! But it just keeps moving over and down indefinitely..

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Michael MacDonald
  • 413
  • 2
  • 6
  • 17

3 Answers3

5

You check j instead of k.

This: for (int k=1 ; j>=7 ; k++)

Should be: for (int k=1 ; k<=7 ; k++)

Also, you don't increment o, so it's always zero and the inner-most loop runs only once every time.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • 3
    Actually, it's more likely that it should be `k<=7` – jonvuri Oct 03 '12 at 19:03
  • @vandey - I agree, I added it as a side note, as it means that the inner loop will run **only** once – MByD Oct 03 '12 at 19:15
  • still not working.. not sure if that is just me not doing what your saying correctly or me not explaining myself correctly. Let me try posing a different question: is it possible to have a definite boolean expression? In other words n=1.. not n>=1 or n<=1... does it always have to have ">" or "<"? I've tried just putting it and it doesn't allow me to. But I thought maybe there was a different way of writing it. – Michael MacDonald Oct 03 '12 at 23:08
2

Look at this cycle of yours:

for (int k=1 ; j>=7 ; k++) {
    ...
}

When j takes the value of 7, this loop will never end, because inside of it j is not changed.

icza
  • 389,944
  • 63
  • 907
  • 827
  • sorry; can you clarify what you mean? is there a way I can make j change? – Michael MacDonald Oct 03 '12 at 23:11
  • Hmm.. somewhat took this advice and it fixed my first issue but created another. I changed: `for (int k=1 ; j>=7 ; k++){` to: `for (int k=1 ; j>=7 ; j=j-7){` – Michael MacDonald Oct 03 '12 at 23:17
  • thus each time it hits seven it runs the innermost loop once and then resets j back to 1 thus repeating itself... but now it won't stop drawing rows.. so its working but again it won't stop. I thought the outermost ("m") for loop would prevent it but it doesn't... – Michael MacDonald Oct 03 '12 at 23:22
0

where is the break ?

for (int k=1 ; j>=7 ; k++){
Anshu
  • 7,783
  • 5
  • 31
  • 41