# Setting a value once by turning off a boolean doesn't work

**URL:** https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467
**Category:** Beginners
**Created:** [November 11, 2021, 11:31pm UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467 "2021-11-11T23:31:59Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Kenga123456](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kenga123456](https://discourse.processing.org/u/Kenga123456)
#### Post date: [November 11, 2021, 11:32pm UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467/1 "2021-11-11T23:32:00Z")

</div>

```auto
float x1 = random(50, 450);
float y1 = random(50, 450);

int size1 = 100;

float inc1X = 3;
float inc1Y = 3;

float x2 =x1;
float y2 = x1;

int size2 = 50;

float inc2X = 3;
float inc2Y = 3;

boolean place1 = true; 

void setup () {
size(500, 500);	
	
}

void draw () {
	
	if (place1 = true) { //supposed to only work once
		float x2 =x1;
float y2 = y1;
		place1 = false;
	}
	
	background(255);
	rectMode(CENTER);
	
	fill(120);
	rect(x1, y1, size1, size1);
x1+=inc1X;
	y1+=inc1Y
	
	if (x1>width-size1/2) {
		inc1X = inc1X * -1;
	}
	
	if (x1<size1/2) {
		inc1X = inc1X * -1;
	}
	
	if (y1>height-size1/2) {
		inc1Y= inc1Y * -1;
	}
	
	if (y1<size1/2) {
		inc1Y = inc1Y * -1;
	}
	
	
	
	
	fill(255);
	rect(x2, y2, size2, size2);
	x2+=inc2X;
	y2+=inc2Y
	
}

```

My code is supposed to set the little square’s position only once to the big square at the beginning, but does it every frame. Can someone help me?

---

<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: [November 12, 2021, 12:06am UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467/2 "2021-11-12T00:06:43Z")

</div>

> [@Kenga123456](#):
>
> `if (place1 = true) { //supposed to only work once`

> **[== (equality) / Reference](https://processing.org/reference/equality.html)**
>
> Determines if two values are equivalent. Please note the equality operator (==) is different from the assignment operator (=) and although they look similar, they have a different use. If you're compa…

---

<div class="post-metadata">

### Author: ![Kenga123456](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kenga123456](https://discourse.processing.org/u/Kenga123456)
#### Post date: [November 12, 2021, 8:50am UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467/3 "2021-11-12T08:50:09Z")

</div>

Hmm, I forgot that. I changed it to this:

```auto
if (place1 == true) {

```

Still doesnt work though. Now the rect completely disappears.  
I tried to print the values x2 and y2 and im getting undefined

---

<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: [November 12, 2021, 10:42am UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467/4 "2021-11-12T10:42:47Z")

</div>

> [@Kenga123456](#):
>
> ```auto
> float x2 =x1;
> float y2 = y1;
> place1 = false;
> 
> ```

Not correct to repeat the word float here

Delete both words float

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [November 12, 2021, 11:01am UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467/5 "2021-11-12T11:01:12Z")

</div>

Hello,

An important topic:

> **[Variable Scope / Examples](https://processing.org/examples/variablescope.html)**
>
> Variables have a global or local "scope". For example, variables declared within either the setup() or draw() functions may be only used in these functions. Global variables, variables declared outsid…

`:)`

---

<div class="post-metadata">

### Author: ![Ignotus](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ignotus/32/14922_2.png) [@Ignotus](https://discourse.processing.org/u/Ignotus)
#### Post date: [November 15, 2021, 1:10am UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467/6 "2021-11-15T01:10:54Z")

</div>

An old programing trick to avoid the confusion of assignment and equality testing is to invert the order of the test:

`> if (true == myBoolean) doSomething();`

Since you true can’t be assigned a value, this will always throw an error if you get it wrong.

---

<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: [November 15, 2021, 1:28am UTC](https://discourse.processing.org/t/setting-a-value-once-by-turning-off-a-boolean-doesnt-work/33467/7 "2021-11-15T01:28:01Z")

</div>

A much better approach is to never directly check for the keywords `true` & `false` at all.

Rather just use the `boolean` variable itself inside `if () {}` w/o comparing it to anything.

Instead of `if (myBoolean == true) {}` just shorten it to just `if (myBoolean) {}`.

And instead of `if (myBoolean == false) {}` prefix the variable w/ the logical not operator `!`:  
`if (!myBoolean) {}`.

> **[Reference](https://processing.org/reference/logicalNOT.html)**
>
> Inverts the Boolean value of an expression. Returns true if the expression is false and returns false if the expression is true. If the expression (a\>b) evaluates to…
