# Missing Something Basic about if statements

**URL:** <https://discourse.processing.org/t/missing-something-basic-about-if-statements/19147>\
**Category:** Coding Questions\
**Created:** [March 29, 2020, 4:01pm UTC](https://discourse.processing.org/t/missing-something-basic-about-if-statements/19147 "2020-03-29T16:01:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![VytautasTheGreat](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@VytautasTheGreat](https://discourse.processing.org/u/VytautasTheGreat)\
**Post date:** [March 29, 2020, 4:01pm UTC](https://discourse.processing.org/t/missing-something-basic-about-if-statements/19147/1 "2020-03-29T16:01:48Z")

</div>

Hi everyone. I’m a complete beginner so please forgive what is probably an obvious question. In the code below, I am creating two global booleans. I want one to toggle after each mouse click, and one to permanently flip to “true” after the second click.

With what I’ve written, I expect to see the “Lights On!” message only once, after the second click. However, I see it every two clicks, even thought the “LightsExist” variable keeps reporting as true. Can anyone point out where I’m going wrong?

```auto
void setup(){
  size(800,800);
  background(0);
}

boolean NewPosition = true;
boolean LightsExist = false;

void draw(){
}

void mousePressed() {
  println(LightsExist);
  if (NewPosition) {
    NewPosition = false;
  }
  else {
    NewPosition = true;
    if (!LightsExist);{
      LightsExist = true;
      println("Lights On!");
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [March 29, 2020, 4:12pm UTC](https://discourse.processing.org/t/missing-something-basic-about-if-statements/19147/2 "2020-03-29T16:12:12Z")

</div>

> [@VytautasTheGreat](#):
>
> I want one to toggle after each mouse click, and one to permanently flip to “true” after the second click.

```auto
boolean toggles;
boolean true_after_two;
int clicks = 0;

void mousePressed(){
  if (clicks < 2){
    clicks++;
  }
  if( clicks == 2 ){
    true_after_two = true;
  }
  toggles = !toggles;
}

```

---

<div class="post-metadata">

**Author:** ![VytautasTheGreat](https://avatars.discourse-cdn.com/v4/letter/v/6f9a4e/32.png) [@VytautasTheGreat](https://discourse.processing.org/u/VytautasTheGreat)\
**Post date:** [March 29, 2020, 4:20pm UTC](https://discourse.processing.org/t/missing-something-basic-about-if-statements/19147/3 "2020-03-29T16:20:38Z")

</div>

EDIT: never mind, I figured out that I just need to take the click incrementer out of its if statement. Thank you!

Now I just have to figure out why my original code didn’t work…

Hi TfGuy,

Thanks for your reply. Your code actually still doesn’t do what I want. If I add a println:

```auto
boolean toggles;
boolean true_after_two;
int clicks = 0;

void draw(){
}

void mousePressed(){
  if (clicks < 2){
    clicks++;
  }
  if( clicks == 2 ){
    true_after_two = true;
    println("Lights On");
  }
  toggles = !toggles;
}

```

I still see “Lights On” after EVERY click from the 2nd, instead of seeing it only once on the second click. This is what I’m confused about.

---

<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:** [March 29, 2020, 4:28pm UTC](https://discourse.processing.org/t/missing-something-basic-about-if-statements/19147/4 "2020-03-29T16:28:43Z")

</div>

```auto

boolean toggles;
boolean true_after_two;
int clicks = 0;

void draw() {
}

void mousePressed() {
  if (clicks < 2) {
    clicks++;
  }
  if ( clicks == 2 ) {
    true_after_two = true;
    if (toggles)
      println("Lights On");
    else println("Lights Off");
  }
  toggles = !toggles;
}

```

---

<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:** [March 29, 2020, 4:32pm UTC](https://discourse.processing.org/t/missing-something-basic-about-if-statements/19147/5 "2020-03-29T16:32:39Z")

</div>

> [@VytautasTheGreat](#):
>
> I still see “Lights On” after EVERY click from the 2nd, instead of seeing it only once on the second click. This is what I’m confused about.

That’s because `clicks` stays 2 after becoming 2 (first if-clause)

Therefore, the 2nd if-clause is true every time the mouse is pressed once clicks has reached 2.

`toggles ` never gets evaluated

And welcome to the forum!

Great to have you here!

TRY this

```auto

boolean toggles;
boolean true_after_two;
int clicks = 0;

void draw() {
  background(111);
  text(clicks, 15, 12);
}

void mousePressed() {

  clicks++;

  if ( clicks == 2 ) {
    true_after_two = true;
    println("Lights On");
  }
  toggles = !toggles;
}

```
