ceating grid using single loop or without nested loop?

I know you can create grids using nested loops

for(int i=0;i<3;i++){
    for(int j=0;j<2;j++){

Can we create same grid with just one loop, no nested loop?

   for(int i=0;i<6;i++){


  • edited June 2018 Answer ✓

    Use i % columns for x and i / columns for y. So here that would be

    rect(w * (i % 3), h * (i / 3), w, h);

    The / is using integer division here, you don't want it to return a fractional value, so i should be an int. I've added brackets to make it clearer and to rule out it returning a fractional value should h be a float.

  • Answer ✓

    W/o parenthesis: rect(i%3 * w, i/3 * h, w, h); :ar!

  • I am stupid! Thanks @Koogs :) :)

    GoToLoop thank you too for correct :) :)

  • Don't rely on operator precedence for stuff like this. It is cryptic and it'll bite you sooner or later.

  • edited June 2018

    The modulo (A.K.A. remainder) % operator, b/c it's related to the division / operator, it's got its same precedence, along w/ the multiplication * operator, like we've all learnt from actual math! :P

  • edited June 2018

    Hmm. For most people -- especially in a community with beginners in one or more languages -- I think it is better that they be really clear and not assume they have internalized Java operator precedence. Especially in a community with Java, JavaScript, Python, Ruby, and R flavors et cetera.

    Here are operator precedence descriptions for ~60 languages, and while the whole extended C family is mostly the same on the basics, gotchas abound....

    Bonus code golf game: Operator precedence: How wrong can I be?

  • ... and not assume they have internalized Java operator precedence.

    @jeremydouglass, addition, subtraction, multiplication & division's precedence is learned at basic school. :-B

    Just a lil' twist that the modulo (remainder) operator is related to division. ;;)

  • I think that % twist is the whole point.

    If you are expected to put А В Б in order, it doesn't matter that you learned A B in grade school. If you don't know one of them, you don't know the order.

    I'm not against people learning, of course. I'm just saying you can't assume that Oracle's operator precedence table should be intuitive or common knowledge for people with basic math.

  • edited June 2018

    I think using a nested for loop in setup() and putting the data into an array of a class Cell that holds the position and values of the cells is the best approach

    I personally think the code with nested for loop has a better readability than % stuff anyway ..... and for me readability and maintainability have priority in writing code.


    Cell [] grid;
    // ---------------------------------------------------------
    void setup() {  
      size(400, 400);
      String[] list = {"b", "c", "d", "q"};
      int unit = 200; // size of one tile
      int wideCount = width / unit;
      int highCount = height / unit; 
      int count = wideCount * highCount;
      grid = new Cell[count];
      int index = 0;
      for (int x=0; x < wideCount; x++) {
        for (int y=0; y < highCount; y++) {
          //start creating objects
          grid[index] = new Cell (x*unit, y*unit, unit, list[index]);
    void draw() {
      for (int i=0; i < grid.length; i++) {
    // ========================================================================
    // and the class
    class Cell {
      float x, y;
      float side;  // size 
      String text=""; 
      Cell (float tempX, float tempY, 
        float tempSide, 
        String tempText) {
        x = tempX;
        y = tempY;
        side = tempSide;
        text = tempText; 
        float fate = random(2);
      void display() {
        if (mouseX>x&&
          fill(122, 0, 0); // dark
          fill(255, 0, 0);  // light 
        rect (x, y, side, side);
        text(text, x+55, y+60);
  • Fair enough -- I suppose the whole point of doing it in a single loop, or with no parens, or in as few chars as possible is "can this work?" -- and not trying to find the most readable or maintainable solution.

  • In jruby_art and propane we have created a grid convenience method after Nodebox it is both really convenient and efficient.

  • thanks I just edited my post above with some code example

  • 74 lines of code answering a question that wasn't asked. 8)

    you people must have so much spare time.

Sign In or Register to comment.