Issue with expecting }, found 'else'

I wrote this code, but Processing keeps telling me about the “expecting }, found ‘else’” error. I checked my code many times, but I just stick at missing out the mistake. Please help out, if you spot it.

void draw()
{
stroke(255, 255, 255);
strokeWeight(2);
point(random(width), random (height));

noStroke();

if ((frameCount % 50) < 25);
{
fill(255, 255, 0);
}
else
{
fill(215, 215, 0);
}

triangle(width/2, 0,
width/2 - 30, 60,
width/2 + 30, 60);

triangle(width/2 - 30, 20,
width/2 + 30, 20,
width/2, 80);

noStroke();
fill(255, 255, 255);
rect(0, 0.9 * height, width, 0.1 * height);
}

I found the issue. in the next reply ill point it out

you used “;” . You should have used “{” so it works now.

the if( <boolean> ) <function> ;
it is the same as:

if( <boolean> ) {
   <function>;
}

and is used for this

void draw() {
  if(mouseX > width/2) background(255); else background(0);
}

however there can only be one function used in the if(); statement

I spent hours looking for the mistake that you pointed out right away. It worked. Thank you so much.

It’s just that I fell in love with the if( <boolean> ) <function> ; It’s insanely useful for shortening the code

2 Likes

Will definitely try that out.

here is a program I just wrote that shows it’s usefulness!

void draw() {
  if (mouseX < width/2 && mouseY < height/2) background(255, 0, 0); 
  else if (mouseX > width/2 && mouseY < height/2) background(0, 0, 255); 
  else if (mouseX < width/2 && mouseY > height/2) background(0, 255, 0); 
  else if (mouseX > width/2 && mouseY > height/2) background(255, 255, 0);
}
1 Like

here is a comparison! If mode == 1 it uses the old way, if mode == 0 it uses the shorter way

int mode = 1;
void draw() {
  if (mode == 0) {
    
    if (mouseX < width/2 && mouseY < height/2) background(255, 0, 0); 
    else if (mouseX > width/2 && mouseY < height/2) background(0, 0, 255); 
    else if (mouseX < width/2 && mouseY > height/2) background(0, 255, 0); 
    else if (mouseX > width/2 && mouseY > height/2) background(255, 255, 0);
    
  } else if (mode == 1) {
    
    if (mouseX < width/2 && mouseY < height/2) {
      background(255, 0, 0);
    } else if (mouseX > width/2 && mouseY < height/2) {
      background(0, 0, 255);
    } else if (mouseX < width/2 && mouseY > height/2) {
      background(0, 255, 0);
    } else if (mouseX > width/2 && mouseY > height/2) {
      background(255, 255, 0);
    }
    
  }
}

1 Like