# Why does my int give me 3 results?

**URL:** <https://discourse.processing.org/t/why-does-my-int-give-me-3-results/10479>\
**Category:** Beginners\
**Created:** [April 20, 2019, 8:46am UTC](https://discourse.processing.org/t/why-does-my-int-give-me-3-results/10479 "2019-04-20T08:46:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewwolf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dewwolf/32/4737_2.png) [@dewwolf](https://discourse.processing.org/u/dewwolf)\
**Post date:** [April 20, 2019, 8:46am UTC](https://discourse.processing.org/t/why-does-my-int-give-me-3-results/10479/1 "2019-04-20T08:46:39Z")

</div>

Hello! I am re-entering the world of processing, and have hit a learning snag. With what I have right now, It is giving me 10, 11, and 12 as an answer. I understand the 11, but not the other 2. Can anyone explain? Thank you in advance!

```auto
void setup(){
  size(400,400);
  int x;
}
int x=1;
void draw(){
  x = (x%3) +10;
  println(x);
  rect(mouseX,mouseY,x,x);
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 20, 2019, 8:56am UTC](https://discourse.processing.org/t/why-does-my-int-give-me-3-results/10479/2 "2019-04-20T08:56:25Z")

</div>

`x % 3` is either `0`, `1` or `2`. Adding `10` is then either `10`, `11` or `12`. 👨‍🏫

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 20, 2019, 9:01am UTC](https://discourse.processing.org/t/why-does-my-int-give-me-3-results/10479/3 "2019-04-20T09:01:36Z")

</div>

**Explanation**

draw loops endlessly;

- first frame x is 1, 1 modulo (%) 3 is 1, 1 plus 10 = 11

- 2nd frame: 11 modulo 3 = 2, 2+10 = 12

- 3rd: 12 modulo 3: 0, 0 + 10 = 10

- 4th 10 mod 3: 1 , 1 + 10 = 11

- …

**Alternative formula**

> [@dewwolf](#):
>
> x = (x%3) +10;

Or use

x = (x+10) % 3;

**Image of Values**

![test1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bae2ac1ffb328d8664c25546fec2f91aad611e66.jpeg)

**Sketch for the Table**

here is a sketch to record the values of `x, x mod 3 and + 10` in a table:

```auto
int x=1;
int i=0; 

// class with tools 
TableTools tools = new TableTools(); 

// the table
Table tValuesFromDraw; 

void setup() {
  size(400, 400);
  background(90); 

  // setting up table (column names / headlines)
  tValuesFromDraw = tools.newTable("Frame", "x", "x mod 3", "+ 10");
}

void draw() {

  background(90); 

  // When i is >= 12 ------------
  if (i>=12) {
    // show table
    tools.showTable(tValuesFromDraw, 
      22, 22);
    return; // leave here and restart draw
  }

  // normal sketch : --------------------
  // adding data (must match headlines)
  tValuesFromDraw = tools.tableAddDataInt ( tValuesFromDraw, frameCount, x, x % 3, (x % 3) + 10 );

  x = (x%3) +10;
  println(x);
  rect(mouseX, mouseY, x, x);

  i++;
}

void keyPressed() {
  if (key==' ')
    save("test1.jpg");
}

//===============================================================================
// Tools collection 

class TableTools {

  // class not like a car class Car for an object but a collection of tools. 

  final color RED = color(255, 0, 0); 
  final color GREEN = color(0, 255, 0); 
  final color BLUE = color(0, 0, 255); 

  final color WHITE = color(255); 
  final color BLACK = color(0); 
  final color GRAY = color(255/2); 

  // ----------------------------------------------------------------

  void showTable(Table tableBtn, 
    int x, int y) { 

    int factorX=78; // column width 

    // rect
    stroke(WHITE);
    noFill();
    rect( x, y, 
      tableBtn.getColumnCount()*78-6, (tableBtn.getRowCount()+1) * 22 + 10 );

    // headline 
    showTableHeadline(tableBtn, x+6, y+19, factorX);

    // horizontal line 
    stroke(WHITE);
    line( x+2, y+5+19, 
      6+x+(tableBtn.getColumnCount())*factorX-13, y+5+19);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x) 
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {

        fill(WHITE);
        text(row.getString(i2), 
          i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15);

        if (mouseInside(i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15)) {
          text (row.getString(i2), 
            20, height-22);
        }//if

        // vertical line 
        line( i2*factorX+x, +y, 
          i2*factorX+x, tableBtn.getRowCount() * 22 + y + 31);
      }//for
    }//for
  }// method 

  boolean mouseInside( float x_, float y_, 
    float w_, float h_) {
    return mouseX>x_ &&
      mouseX<x_+w_ &&
      mouseY>y_ &&
      mouseY<y_+h_;
  }

  void showTableHeadline(Table tableBtn, 
    int x, int y, 
    int factorX) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      fill(GREEN);
      text(row0.getColumnTitle(i), 
        i*factorX+x, y-2);
    }
  }//method 

  // ---
  // make table 

  Table newTable (String... listColumnNames) { 

    Table newT = new Table();

    // make columns
    for (String s1 : listColumnNames) {
      newT.addColumn(s1);
    }

    return newT;
  }//method

  Table tableAddData( Table table1, String... data1 ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (String s1 : data1) {
      newRow.setString(newRow.getColumnTitle(i), s1);
      i++;
    }
    return table1;
  }//method

  Table tableAddDataFloat ( Table table1, float... data1 ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (float f1 : data1) {
      newRow.setFloat(newRow.getColumnTitle(i), f1);
      i++;
    }
    return table1;
  }//method

  Table tableAddDataInt( Table table1, int... data1 ) {

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (int f1 : data1) {
      newRow.setInt(newRow.getColumnTitle(i), f1);
      i++;
    }
    return table1;
  }//method

  // --- 

  void printlnTable(Table tableBtn) { 

    // rect
    stroke(WHITE);
    noFill();

    println("------------------------------------");

    // headline 
    printlnTableHeadline(tableBtn);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x)
      String s1="";
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {
        s1+=" "+row.getString(i2);
        //
      }//for
      println(s1);
    }//for
    println("===========================================");
    //
  } // method 

  void printlnTableHeadline(Table tableBtn ) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      print(" "+row0.getColumnTitle(i));
    }
    println("");
  }//method 
  //
}//class
//

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 20, 2019, 9:02am UTC](https://discourse.processing.org/t/why-does-my-int-give-me-3-results/10479/5 "2019-04-20T09:02:42Z")

</div>

I answered during you wrote your post

See above

---

<div class="post-metadata">

**Author:** ![dewwolf](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dewwolf/32/4737_2.png) [@dewwolf](https://discourse.processing.org/u/dewwolf)\
**Post date:** [April 20, 2019, 9:03am UTC](https://discourse.processing.org/t/why-does-my-int-give-me-3-results/10479/6 "2019-04-20T09:03:55Z")

</div>

Apologies, I did not see the update before posting. Thank you both!
