# "wait until" syntax?

**URL:** <https://discourse.processing.org/t/wait-until-syntax/15838>\
**Category:** Coding Questions\
**Created:** [November 26, 2019, 4:16pm UTC](https://discourse.processing.org/t/wait-until-syntax/15838 "2019-11-26T16:16:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![benross](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@benross](https://discourse.processing.org/u/benross)\
**Post date:** [November 26, 2019, 4:16pm UTC](https://discourse.processing.org/t/wait-until-syntax/15838/1 "2019-11-26T16:16:10Z")

</div>

Hi all - is there any way I can make some section of code in draw() only after a certain event has happened?

For example can I only start drawing things after a mouseclick or if the user presses a set key?

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 26, 2019, 5:00pm UTC](https://discourse.processing.org/t/wait-until-syntax/15838/2 "2019-11-26T17:00:58Z")

</div>

Yes, just use a boolean.

```auto
boolean start = false;

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

void draw() {
   if (start) {
      ellipse(100,100,50,50);
   }
}

void mousePressed() {
   start = true;
}

```

---

<div class="post-metadata">

**Author:** ![Ihsanduru](https://avatars.discourse-cdn.com/v4/letter/i/edb3f5/32.png) [@Ihsanduru](https://discourse.processing.org/u/Ihsanduru)\
**Post date:** [November 27, 2019, 10:23pm UTC](https://discourse.processing.org/t/wait-until-syntax/15838/3 "2019-11-27T22:23:29Z")

</div>

Thank you so much. I liked my game much more because of your code. Thank you again!

---

<div class="post-metadata">

**Author:** ![benross](https://avatars.discourse-cdn.com/v4/letter/b/91b2a8/32.png) [@benross](https://discourse.processing.org/u/benross)\
**Post date:** [November 28, 2019, 9:49am UTC](https://discourse.processing.org/t/wait-until-syntax/15838/4 "2019-11-28T09:49:05Z")

</div>

I appreciate this, but this must surely slow down the program somewhat?

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [November 28, 2019, 11:19am UTC](https://discourse.processing.org/t/wait-until-syntax/15838/5 "2019-11-28T11:19:15Z")

</div>

? I mean… sure… that‘s how programing works… you add functionality and it makes more things happen, which obviously means finishing a Frame takes longer…

But if you mean that you will notice the difference… no. It‘s just an if statement. The background(0); function alone goes through a Ton of if statements, just like any function you can think of uses on average ~ 50 if statements (not themselves, but the functions they call also count).

---

<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 28, 2019, 5:28pm UTC](https://discourse.processing.org/t/wait-until-syntax/15838/6 "2019-11-28T17:28:06Z")

</div>

> [@Lexyth](#):
>
> just use a boolean.

boolean is one idea.

When you have different screens (and not only two) you can use an int variable, in the forum mostly referred to as state.

```auto
int state = 0; 

....

if (state==0){
    // screen 1
}
else if (state==1){
    // screen 2
}
else if (state==2){
    // screen 3
}

```

---

<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:** [December 8, 2019, 7:46pm UTC](https://discourse.processing.org/t/wait-until-syntax/15838/7 "2019-12-08T19:46:12Z")

</div>

> [@benross](#):
>
> I appreciate this, but this must surely slow down the program somewhat?

> [@Lexyth](#):
>
> It‘s just an if statement.

Yes – single if statements themselves are basically free – like putting a grain of sand in a truck. If you add many millions of grains of sand, you could eventually slow the truck down, but it will take a _lot_ – it is never worth counting them individually. When in doubt, test.

Instead of how many ifs, the key to performance is how much (is the if in a for loop?) of whatever is inside them – including the if condition statement. On my laptop this sketch runs smoothly even when testing 2,147,483,647 if statements per frame, 60fps.

On the other hand, when you switch to a modulo or a random number inside each if condition, the sketch might start to slow down a lot after a mere 10,000,000 ifs per frame or so… only 60,000,000 ifs per second. Now you are filling your truck with gravel, or pebbles. Still not worth counting individually – but it takes fewer to fill up.

```auto
/**
 * How Many Ifs
 * 2019-12 Processing 3.4
 * https://discourse.processing.org/t/wait-until-syntax/15838/3
 */

int ifs = 10;

void draw() {  
  background(0);

  if (frameCount%180==0) ifs *= 10;
  if (ifs > 1000000000 || ifs < 0) ifs = MAX_INT;

  int x=0;
  int rnd = (int)random(ifs);
  for (int i=0; i<ifs; i++) {
    if (i>rnd) { // simple test, full speed
    // if (ifs%(i+1)==0) { // slightly harder math, eventually slows
    // if (random(1)>0.5){ // random(), much heavier lift, eventually crawls
      x++;
    }
  }
  text(frameRate, 10, 25);
  text(str(ifs) + "\n~ifs/frame", 10, 45);
}

```
