# Int can't be a value?

**URL:** https://discourse.processing.org/t/int-cant-be-a-value/4159
**Category:** Coding Questions
**Created:** [October 5, 2018, 9:51am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159 "2018-10-05T09:51:54Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)
#### Post date: [October 5, 2018, 9:51am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/1 "2018-10-05T09:51:54Z")

</div>

Is there a way to make a statement where you declare that an int value cannot be a certain number? For example, if you say `int test = 5;` would there be a way to say `test ≠ 20`

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [October 5, 2018, 9:55am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/2 "2018-10-05T09:55:49Z")

</div>

Not really but why would you do that? There are surely other solutions to prevent that depending on what you want to do.

---

<div class="post-metadata">

### Author: ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)
#### Post date: [October 5, 2018, 9:58am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/3 "2018-10-05T09:58:27Z")

</div>

@jb4x

I am controlling the size of a rectangle by pressing up or down. However, by pressing down too many times you can make the size negative which reverses the effect (eg down increases the size, and up decreases the size). This messes up many other things that are based off of the size of the rectangle. I can include part of the code if necessary.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [October 5, 2018, 10:00am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/4 "2018-10-05T10:00:00Z")

</div>

It would be better to include code yes.

Now you can usually achive your effect using simple if condition:

```auto
if (var > 20) {
  var = 20;
}

```

It prevents your variable to go over 20.

---

<div class="post-metadata">

### Author: ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)
#### Post date: [October 5, 2018, 10:08am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/5 "2018-10-05T10:08:33Z")

</div>

@jb4x

This code has another problem. It always has my int value at the number you set it equal to. For example, in your code, it would always keep var = to 20 no matter what.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [October 5, 2018, 10:14am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/6 "2018-10-05T10:14:03Z")

</div>

Can you post some code please?

---

<div class="post-metadata">

### Author: ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)
#### Post date: [October 5, 2018, 10:17am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/7 "2018-10-05T10:17:07Z")

</div>

@jb4x

```auto
//Add possibility to change shapes (square, circle, arc, triangle)
//Add undo button
//Add eraser 
//Connect shapes so there isn't a gap when you draw 
//Add transparency/tint feature
PGraphics circles; //PGraphics layer
boolean randomCol; //Random color variable
int size = 50; //Size of circle and square
color col; //Color Variable 
float stop = 95; //Disables you to draw on text
int toDraw = 1; //Switching between left and right (circle and square)
String v = "Hold down the mouse or trackpad to paint. When trying to use different colors, remember that lowercase letters and uppercase letters do different things."; //Text variables
String s = "b = Blue, B = Black, c = Cyan, g = Green, G = Gray, m = Magenta, M = Maroon (Brown), o = Orange, p = Purple, P = Pink, r = Red, w = White, y = Yellow, Tab = Random.";
String e = "ESC = Exit, Space Bar = Erase the whole drawing, Up Arrow Key = Increase size, Down Arrow Key = Decrease size, Left Arrow Key = Circle, Right Arrow Key = Square.";

void setup() //Setup the canvas
{
  frameRate(6000);
  fullScreen();
  background(255);
  fill(0);
  circles = createGraphics(2000, 2000); //Setup the PGraphics layer
  randomCol = false; //Disables random color at the start of the program
}
void draw() //Draw
{
  background(255);
  noStroke();
  noCursor();
  if (randomCol) //Setting the random color variable
  {
    col = color(random(255), random(255), random(255));
  } 
  if (mousePressed) //If mouse is pressed then draw
  {
    if (mouseY > stop) //If mouseY variable is underneath the text, then you can draw 
    {
    drawCircles();
    }
  }
  image(circles, 0, 0); //Print the second plane
  fill(col);
  if (toDraw == 0) //Switches to a square or circle if left or right is pressed
  {
    fill(col);
    rect(mouseX-(size/2), mouseY-(size/2), size, size);
  }
  else if (toDraw == 1)
  {
    fill(col);
    ellipse(mouseX, mouseY, size, size);
  }
    drawText(); //Writes the text and the line
}
void drawCircles() //The PGraphics layer
{
  if (toDraw == 0) //Draw the square if right is pressed
  {
    circles.beginDraw();
    circles.noStroke();
    circles.fill(col);
    circles.rect(mouseX-(size/2), mouseY-(size/2), size, size);
    circles.endDraw();
  }
  else if (toDraw == 1) //Draw the circle if left is pressed
  {
    circles.beginDraw();
    circles.noStroke();
    circles.fill(col);
    circles.ellipse(mouseX, mouseY, size, size);
    circles.endDraw();
  }
}
void drawText() //Here is where we write the text and draw the line separating it
{
  fill(0);
  stroke(0);
  text(v, 10, 20);
  text(s, 10, 40);
  text(e, 10, 60);
  line(0, 70, 2000, 70);
}
void keyPressed() //Has all the key press inputs
{
  if (key == CODED)
  {
    if (keyCode == UP) //Up and down change the size
    {
      size=size+5;
      stop=stop+2.5;
    }
    if (keyCode == DOWN)
    {
      size=size-5;
      stop=stop-2.5;
    }
    if (keyCode == RIGHT) //Left and right make it a circle or square 
    {
      toDraw = 0;
    }
    if (keyCode == LEFT)
    {
      toDraw = 1;
    }
  }
  if (key == TAB) //If tab is pressed, random colors
  {
    randomCol = !randomCol;
  }
  if (key == 'b') //Different keys activate different colors
  {
    randomCol = false; //Disables random colors
    col = color(0, 0, 255); //Switches to the desired color
  }
  if (key == 'B')
  {
    randomCol = false; 
    col = color(0);
  }
  if (key == 'c')
  {
    randomCol = false; 
    col = color(0, 255, 255);
  }
  if (key == 'g')
  {
    randomCol = false; 
    col = color(0, 255, 0);
  }
  if (key == 'G')
  {
    randomCol = false; 
    col = color(128, 128, 128);
  }
  if (key == 'm')
  {
    randomCol = false; 
    col = color(255, 0, 255);
  }
  if (key == 'M')
  {
    randomCol = false; 
    col = color(139, 69, 19);
  }
  if (key == 'o')
  {
    randomCol = false; 
    col = color(255, 140, 0);
  }
  if (key == 'p')
  {
    randomCol = false; 
    col = color(148, 0, 211);
  }
  if (key == 'P')
  {
    randomCol = false; 
    col = color(255, 192, 203);
  }
  if (key == 'r')
  {
    randomCol = false; 
    col = color(255, 0, 0);
  }
  if (key == 'w')
  {
    randomCol = false; 
    col = color(255);
  }
  if (key == 'y')
  {
    randomCol = false; 
    col = color(255, 255, 0);
  }
  if (key == ' ')
  {
    circles.beginDraw();
    circles.background(255);
    circles.endDraw();
  }
}

```

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [October 5, 2018, 11:56am UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/8 "2018-10-05T11:56:08Z")

</div>

I can’t see why you can’t apply the concept.

```auto
if (keyCode == DOWN)
{
  size=size-5;
  if (size < 0) {
    size = 0;
  } else {
    stop=stop-2.5;
  }
}

```

---

<div class="post-metadata">

### Author: ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)
#### Post date: [October 5, 2018, 12:16pm UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/9 "2018-10-05T12:16:35Z")

</div>

@jb4x

Thank you.

I believe the reason why it wasn’t working before is because I put the

> [@jb4x](#):
>
> `if (var > 20) { var = 20; }`

Not inside the key code. 😌

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 13, 2018, 9:20pm UTC](https://discourse.processing.org/t/int-cant-be-a-value/4159/10 "2018-10-13T21:20:39Z")

</div>

Note that Processing also has some built-in functions to express this idea in general.

For example, if you want to set lowest value, take the max of two values – your update or your defined minimum:

```auto
int x = 10;
x = x - 25; // -15
x = max(0, x); // minimum is 0, not -15

```

- [https://processing.org/reference/max\_.html](https://processing.org/reference/max_.html)

Or if you want to set the highest value, do the same thing – take the min of two values:

```auto
int x = 10;
x = x + 25; // 35
x = min(20, x); // maximum is 20, not 35

```

- [https://processing.org/reference/min\_.html](https://processing.org/reference/min_.html)

You can also enforce both a min and a max with constrain:

```auto
int x = 50;
x = x + 99; // 149
x = constrain(x, 0, 100); // to max of 100
x = x - 999; // -899
x = constrain(x, 0, 100); // to min of 0

```

- [https://processing.org/reference/constrain\_.html](https://processing.org/reference/constrain_.html)

A concise way of making a change and constraining it is to include the change as the argument.

```auto
int x = 0;
x = constrain( x + (int)random(-999, 999), -100, 100);

```

This will update x to some value between -999 and 999, then constrain it to the range -100 / 100.
