# Breaking out of For Loop when mouse is depressed?

**URL:** <https://discourse.processing.org/t/breaking-out-of-for-loop-when-mouse-is-depressed/6071>\
**Category:** Coding Questions\
**Created:** [November 29, 2018, 1:16pm UTC](https://discourse.processing.org/t/breaking-out-of-for-loop-when-mouse-is-depressed/6071 "2018-11-29T13:16:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkEco](https://avatars.discourse-cdn.com/v4/letter/d/a4c791/32.png) [@DarkEco](https://discourse.processing.org/u/DarkEco)\
**Post date:** [November 29, 2018, 1:16pm UTC](https://discourse.processing.org/t/breaking-out-of-for-loop-when-mouse-is-depressed/6071/1 "2018-11-29T13:16:46Z")

</div>

Hi, i’m quite new to coding in general and having a really hard time with this. Currently my loop is running from 0-1000 in a single click, but i need it to break out of the loop when i let go of the mouse. I’m guessing my If to trigger the break is placed incorrectly, but i don’t understand why isPressed isn’t being checked every time the loop executes.

Here’s a simplified sketch of the problem:

```auto
var isPressed = false; 

function setup() {
createCanvas(1000, 500);
  background(0);
}

function draw() {
  test();
}

function test()
{
  if (mouseX>0 && mouseX<width && mouseY<height && mouseY>0 && isPressed === true)
  {
      testLoop:
      for (var i=0; i<1000; i++)
      {
        if (isPressed === false)
        {
          break testLoop;
        }
        else 
        {
          print("push");
        }
      
      }   
  }
}

function mousePressed()
  {
    isPressed = true;
  }
  
function mouseReleased()
  {
    isPressed = false;
  }
```

Here’s my actual code:

```auto
//--------------------------------------------
//DRAWS BEATERS ON-SCREEN

function BeaterControl()
{
  
  if (mouseX>0 && mouseX<width && mouseY<height && mouseY>0 && isPressed === true)
  {
      arrayLoop:
      for (var beaterID=0; beaterID<1000; beaterID++)
      {
        if (isPressed === false)
        {
          break arrayLoop;
        }
        else 
        {
          beaterArray[beaterID] = beaterArray.push(new Beater());
        }
      
        console.log(beaterArray[10]);
        //console.log(beaterArray[beaterID].y);
      }
        
      if (beaterSize <= 20)
      {
          beaterSize = (beaterSize+0.2);
      }
       
  }
      
  if (isPressed === false)
  {
    beaterSize = 0;
  }
 
    
}

function Beater()
{
  this.x = mouseX;
  this.y = mouseY;
  this.diameter = beaterSize;
  
  this.beaterFill = fill(250,250,0,250);
  this.beaterDisplay = ellipse(this.x,this.y,10,10);
}
```

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [November 29, 2018, 2:46pm UTC](https://discourse.processing.org/t/breaking-out-of-for-loop-when-mouse-is-depressed/6071/2 "2018-11-29T14:46:18Z")

</div>

Hi! Mouse and keyboard state is only updated **between** animation frames, never in the middle of running `draw`. By default that means that they are updated every 16.66 milliseconds (at 60 fps) (as far as I understand).

What are you trying to achieve with counting up to 1000?

btw. when a mouse is depressed attempts to break out of _For Loop_ often fail. The mouse needs to be very focused to attempt such a escape, so in these situations I always suggest mice to do training first to end the depression. Then, when the moment comes, it will be able to react quickly and sneak out of _For Loop_ without being noticed by those insensitive _Loop_ guards.

😛

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 30, 2018, 1:20am UTC](https://discourse.processing.org/t/breaking-out-of-for-loop-when-mouse-is-depressed/6071/3 "2018-11-30T01:20:15Z")

</div>

i only see a way like

```auto
int ccount=0, count=0, countend=100;
boolean reported = false;

void setup() {
  size(200, 200);
  background(200, 200, 0);
  fill(200, 0, 0);
  noStroke();
  frameRate(10);
}

void draw() {
  if (( count <= countend ) && mousePressed ) {
    ellipse(random(200), random(200), 10, 10);
    ccount++;
    print("circles "+ccount+" ");
  }
  println( "count: "+count);
  count++;
  if ( count > countend && !reported ) { 
    println("you made "+ccount+" circles"); 
    reported = true; 
    exit();
  }
}

```

that gives you 100 loops to draw 100 circles, but only when the mouse is pressed;
