# Code "if' and 'else if' error

Hello,
This is my first step in processing and I have had some coding lessons behind me. I have to make a pattern generator and I have almost 1 pattern behind me. However, I have a problem when it comes to code '50: 50 chance ’ using ‘if’ and ‘else if’ etc. . I think the code looks fine, but this shows up as “Duplicate local variable count”. I’ve tried several ways to fix it, but unfortunately, it failed. What I mean, I would like there to be a 50% chance of a regular pattern appearing through if and else if, and a 50% chance for that pattern to appear (such as this one). Do you see the error that I got in this one and how should it be properly resolved?

This is the code to my pattern:
void setup ()
{
size (650, 750);
background(255);

}

void draw()
{
noStroke();
//circle( 40,40,50);

for (int licznikA= 0; licznikA <6; licznikA= licznikA+1)
{
for (int licznikB= 0; licznikB <6; licznikB= licznikB+1)
{
fill(255, 179, 51); ///zloty
circle( 40+ licznikA* 120, 40+licznikB*150,80);
//scale(random (0,2));
}
}

``````for (int licznikA= 0; licznikA < 5; licznikA= licznikA+1)
``````

{
for (int licznikB= 0; licznikB < 7; licznikB= licznikB+1)
{

``````  fill(109, 0, 0); ///bordo
pushMatrix();
translate(100+ licznikA* 120, 120+licznikB*150);
``````

scale(random (0,2));
//circle( 100+ licznikA* 120, 120+licznikB*150,40);
circle (0,0,40);
popMatrix();

///circle( 40+ licznikA* 120, 40+licznikB*150,40);
}
}

noLoop();

And this is the ‘if’/'else if" ‘ERROR’ one:

void setup ()
{
size (650, 750);
background(255);

}

void draw()
{
int los = int( random(0,2));

for (int licznikA= 0; licznikA <6; licznikA= licznikA+1)
{
for (int licznikB= 0; licznikB <6; licznikB= licznikB+1)
{

``````  if (los==0)
{
fill(109,0,0); ///bordo
``````

circle( 40+ licznikA* 120, 40+licznikB*150,80);
//scale(random (0,2));
}
else if (los==1)
{
for (int licznik= 0; licznikA < 5; licznikA= licznikA+1)
{
for (int licznik= 0; licznikB < 7; licznikB= licznikB+1)
{

``````  fill(109, 0, 0); ///bordo
pushMatrix();
translate(100+ licznikA* 120, 120+licznikB*150);
``````

scale(random (0,2));
//circle( 100+ licznikA* 120, 120+licznikB*150,40);
circle (0,0,40);
popMatrix();

///circle( 40+ licznikA* 120, 40+licznikB*150,40);
}
}
}
noLoop();
}
}
}

Hello,

Welcome to the forum.

https://discourse.processing.org/faq#format-your-code

`:)`

It’s a bit hard to understand since you don’t have a variable count in the first place.

This error would be caused when you say twice int count or int los within the same function draw() or the same scope

OK I added some notes that may explain the problem. I hope you like it! Anything in caps is NOT yelling. I just did that for emphasis.

the brackets are in the wrong place. They should be after the IF command is finished, not after the whole program.

NEVER use the “else” command, always use if(!()) instead since the ! means not.

licznik= 0; licznikA it’s not named properly. Also neither “licznik” or “licznikA” is a good name since they were used above in the line. RENAME IT licznikC and licznikD. That should help.

Here’s the GLITCH code with notes.

``````void setup ()
{
size (650, 750);
background(255);

}

void draw()
{
int los = int( random(0,2));

for (int licznikA= 0; licznikA <6; licznikA= licznikA+1)
{
for (int licznikB= 0; licznikB <6; licznikB= licznikB+1)
{

if (los==0)
{
fill(109,0,0); ///bordo
circle( 40+ licznikA* 120, 40+licznikB*150,80);
//scale(random (0,2));
}

// another problem is that the brackets are in the wrong place. They should be HERE but you have them at the end of the program
//}//<- this is the right place for the brackets
//}//<- this is the right place for the brackets

// DO NOT EVER use the "else" command. Always say "if(!(los==0)&&los==1)" because the ! sign means NOT, but really, this isn't neccessary since if los is 1 than it cannot be 0.
else if (los==1)
{
//    RIGHT HERE is the problem. licznik= 0; licznikA it's not named properly. Also neither "licznik" or "licznikA" is a good name since they were used above in the line 13 and line 15. RENAME IT licznikC and licznikD
//         |
//         V
for (int licznik= 0; licznikA < 5; licznikA= licznikA+1)
{
for (int licznik= 0; licznikB < 7; licznikB= licznikB+1)
{

fill(109, 0, 0); ///bordo
pushMatrix();
translate(100+ licznikA* 120, 120+licznikB*150);
scale(random (0,2));
//circle( 100+ licznikA* 120, 120+licznikB*150,40);
circle (0,0,40);
popMatrix();

///circle( 40+ licznikA* 120, 40+licznikB*150,40);
}
}
}
noLoop();
} // this is not where this goes ->}
} // this is not where this goes ->}
}
``````

Here’s the FIXED code with notes

``````void setup ()
{
size (650, 750);
background(255);

}

void draw()
{
int los = int( random(0,2));

// this line below is the line I am talking about
for (int licznikA= 0; licznikA <6; licznikA= licznikA+1)
{
for (int licznikB= 0; licznikB <6; licznikB= licznikB+1)
{

if (los==0)
{
fill(109,0,0); ///bordo
circle( 40+ licznikA* 120, 40+licznikB*150,80);
//scale(random (0,2));
}

// another problem is that the brackets are in the wrong place. They should be HERE but you have them at the end of the program
}//<- this is the right place for the brackets
}//<- this is the right place for the brackets

// DO NOT EVER use the "else" command. Always say "if(!(los==0)&&los==1)"  but really, this isn't neccessary since if los is 1 than it cannot be 0.

if (!(los==0)&&los==1)
{
//    RIGHT HERE is the problem. licznik= 0; licznikA it's not named properly. Also neither "licznik" or "licznikA" is a good name since they were used above in the line. RENAME IT licznikC and licznikD
//         |
//         V
for (int licznikC= 0; licznikC < 5; licznikC= licznikC+1)
{
for (int licznikD= 0; licznikD < 7; licznikD= licznikD+1)
{

fill(109, 0, 0); ///bordo
pushMatrix();
translate(100+ licznikC* 120, 120+licznikD*150);
scale(random (0,2));
//circle( 100+ licznikC* 120, 120+licznikD*150,40);
circle (0,0,40);
popMatrix();

///circle( 40+ licznikC* 120, 40+licznikD*150,40);
}
}

}
noLoop();
// this is not where this goes ->}
// this is not where this goes ->}
}
``````

And one more thing pal

``````this is how you format
``````

` ← use 3 of these, they should be right next to your number 1 key. Please format your code because that’s easier to copy and paste

`` (three above)
code goes here
`(three below)