# While (mouseIsPressed) infinite loop nasty

**URL:** https://discourse.processing.org/t/while-mouseispressed-infinite-loop-nasty/7962
**Category:** Coding Questions
**Created:** [January 30, 2019, 2:46pm UTC](https://discourse.processing.org/t/while-mouseispressed-infinite-loop-nasty/7962 "2019-01-30T14:46:08Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hellonearthis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hellonearthis/32/3482_2.png) [@hellonearthis](https://discourse.processing.org/u/hellonearthis)
#### Post date: [January 30, 2019, 2:46pm UTC](https://discourse.processing.org/t/while-mouseispressed-infinite-loop-nasty/7962/1 "2019-01-30T14:46:08Z")

</div>

I was trying to use a while statement in the draw loop.  
I was thinking it would stop the draw loop until the mouse was released,  
but it just goes into an infinite loop.

```auto
let redoLines = true

function draw() {
  while (mouseIsPressed) { redoLines = true } 
  if (redoLines){ 
     doLines()
     redoLines = false
  }
}

```

eak, I guess it was to nasty bit of nasty code.  
So going have to use the not so nasty,

```auto
function mouseReleased() {
     redoLines = false
}

```

Still, I was hoping my nasty code would have worked.

---

<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: [January 30, 2019, 3:11pm UTC](https://discourse.processing.org/t/while-mouseispressed-infinite-loop-nasty/7962/2 "2019-01-30T15:11:54Z")

</div>

> [@hellonearthis](#):
>
> Still, I was hoping my nasty code would have worked.

Given the thread responsible to change the value of _mouseIsPressed_ is the same 1 executing your `while ()` “infinite” loop, it means it’ll never get the chance to do so. 😬

---

<div class="post-metadata">

### Author: ![HD161693](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@HD161693](https://discourse.processing.org/u/HD161693)
#### Post date: [January 30, 2019, 6:06pm UTC](https://discourse.processing.org/t/while-mouseispressed-infinite-loop-nasty/7962/3 "2019-01-30T18:06:12Z")

</div>

It’s not nasty, it’s a totally reasonable idea! And you did successfully stop the draw loop.  
If you swap that `while` out for an `if`, you are in business.
