Missing Something Basic about if statements

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?

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!");
    }
  }
}

1 Like
boolean toggles;
boolean true_after_two;
int clicks = 0;

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

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:

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.

1 Like




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;
}

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



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;
}
1 Like