# Trying to limit println()

**URL:** <https://discourse.processing.org/t/trying-to-limit-println/20379>\
**Category:** Beginners\
**Created:** [May 2, 2020, 7:03am UTC](https://discourse.processing.org/t/trying-to-limit-println/20379 "2020-05-02T07:03:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 2, 2020, 7:03am UTC](https://discourse.processing.org/t/trying-to-limit-println/20379/1 "2020-05-02T07:03:04Z")

</div>

I am trying to run println(); once in an if statement, the if statement can be true for many frames, but the print(); should only run once.  
Many thanks,  
Adi

---

<div class="post-metadata">

**Author:** ![Dennis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dennis/32/1111_2.png) [@Dennis](https://discourse.processing.org/u/Dennis)\
**Post date:** [May 2, 2020, 7:14am UTC](https://discourse.processing.org/t/trying-to-limit-println/20379/2 "2020-05-02T07:14:18Z")

</div>

Good morning ADI, I think the best way to understand your issue is to post the code.

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 2, 2020, 7:30am UTC](https://discourse.processing.org/t/trying-to-limit-println/20379/3 "2020-05-02T07:30:18Z")

</div>

I was just about to post an example, the real code is long and only a part of it has a problem. The problem is that the println(); is running many times, because it is true many times, but I can’t stop the draw from looping or slow it down. I only want the println(); to run once until it is not true, not each time it is true. Also it is 12:30 am, rightnow I will probably, be able to reply later.  
Thanks,  
Adi

```auto
float i = 1;
void setup() {
}
void draw() {
  mm();
}
void mm () {
  if (mousePressed == true) {
    println(i);//I want this done once, until it is not true, not each time this is true.
  } else println();
  i = i + 1;//Same over here.
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 2, 2020, 7:48am UTC](https://discourse.processing.org/t/trying-to-limit-println/20379/4 "2020-05-02T07:48:36Z")

</div>

You might try this

```auto
boolean allowPrint = true;

void setup() {
}
void draw() {
  mm();
}
void mm () {
  if (mousePressed == true && allowPrint == true) {
    println(frameCount + " @ " + millis());
    allowPrint = false; // this stops repetition
  } 
  if (mousePressed == false) {
    allowPrint = true; // This allows the println the next time the mouse is pressed
  }
}

```

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [May 2, 2020, 6:36pm UTC](https://discourse.processing.org/t/trying-to-limit-println/20379/5 "2020-05-02T18:36:01Z")

</div>

One more request, what if I want the same thing happening in an if statement like this,

```auto
float a = random(0,100);
float b = random(0,100);
boolean allowprint = true;
void setup(){}
void draw(){
 rect(a,b,10,10);
    mm();
}
 void mousePressed() {
  rectX = mouseX;
  rectY = mouseY;
}
void mm (){
if (x1==a && y1==b) {
 if (x1==a && y1==b) {
     allowPrint = true;
}
    if (allowPrint==true) {
      println("hi");
      allowPrint=false;
}
if (x1==a&& y1==b== false) {
    allowPrint = false; 
  }
 }
}

```

(the if statements can be true for many frames), is there a way to keep, `allowprint`, false, and make it true only when the person wants it to be true. Also, this code might need some other corrections.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [May 3, 2020, 8:23am UTC](https://discourse.processing.org/t/trying-to-limit-println/20379/6 "2020-05-03T08:23:08Z")

</div>

> [@Adi](#):
>
> Also, this code might need some other corrections.

Since the code won’t run then there are obvious errors which need fixing. In this case several variable have not been declared so you need to fix that.  
So a quick primer on boolean expressions, boolean variables and conditional statements

A boolean expression is anything that evaluates to `true` or `false` and are used in conditional statements (e.g. `if`, `while`). A boolean variable is also a boolean expression that does not need evaluating because it is already `true` or `false` so the statement

`if(allowPrint == true){...` can be written `if(allowPrint){...`

and for false we can use the ‘not’ operator (!). `!allowPrint` means _not allowPrint_ so if `allowPront` is true then `!allowPrint` is false so the statement

`if(allowPrint == false){...` can be written `if(!allowPrint){...`

Now we come to the statement in your code `y1==b== false` at first glance I thought this was a mistake but the syntax is permitted even if the meaning is unclear. Anyway I did work it out and

`y1==b== false` evaluates to true if `y1` is _not equal to_ `b` so can be written `y1 != b`

Now we come to the first two statements inside the method `mm`

```auto
  if (x1==a && y1==b) {    
    if (x1==a && y1==b) {

```

Now think about it, the second statement will _only be executed_ if the boolean expression in the first statement is true. What this means is

```auto
  if (x1==a && y1==b) {    
    // We only get here if (x1==a && y1==b) is true
    if (x1==a && y1==b) { // which means this will also be true so redundant

```

So taking these into account the function can be rewritten as

```auto
void mm () {
  if (x1 == a && y1 == b) {
    allowPrint = true;
    if (allowPrint) {
      println("hi");
      allowPrint=false;
    }
    if (x1 == a && y1 != b) {
      allowPrint = false;
    }
  }
}

```

If you need further help then please modify your code so it run and give a clearer explanation of what you are trying to achieve.
