# Code "if' and 'else if' error

**URL:** https://discourse.processing.org/t/code-if-and-else-if-error/34804
**Category:** Coding Questions
**Tags:** homework
**Created:** [January 23, 2022, 3:45pm UTC](https://discourse.processing.org/t/code-if-and-else-if-error/34804 "2022-01-23T15:45:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Copious-plains](https://avatars.discourse-cdn.com/v4/letter/c/8baadc/32.png) [@Copious-plains](https://discourse.processing.org/u/Copious-plains)
#### Post date: [January 23, 2022, 3:45pm UTC](https://discourse.processing.org/t/code-if-and-else-if-error/34804/1 "2022-01-23T15:45:50Z")

</div>

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?  
Thanks for your help

**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);
    //rotate( random (radians(45) ));

```

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);
    //rotate( random (radians(45) ));

```

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

 ![Zrzut ekranu (702)](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/17877bd978b6763dd6a8ee700dd95f5508eef14f.png)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [January 23, 2022, 3:51pm UTC](https://discourse.processing.org/t/code-if-and-else-if-error/34804/2 "2022-01-23T15:51:45Z")

</div>

Hello,

Welcome to the forum.

> [@Copious-plains](#):
>
> Do you see the error that I got in this one and how should it be properly resolved?

Please format your code. It helps us help you.

Format Your Code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<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: [January 23, 2022, 7:34pm UTC](https://discourse.processing.org/t/code-if-and-else-if-error/34804/3 "2022-01-23T19:34:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)
#### Post date: [January 25, 2022, 1:40am UTC](https://discourse.processing.org/t/code-if-and-else-if-error/34804/4 "2022-01-25T01:40:49Z")

</div>

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.

```auto
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);
    //rotate( random (radians(45) ));
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

```auto
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);
    //rotate( random (radians(45) ));
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 ->}
}

```

---

<div class="post-metadata">

### Author: ![PhoenixStormJr](https://avatars.discourse-cdn.com/v4/letter/p/43a26b/32.png) [@PhoenixStormJr](https://discourse.processing.org/u/PhoenixStormJr)
#### Post date: [January 25, 2022, 1:46am UTC](https://discourse.processing.org/t/code-if-and-else-if-error/34804/5 "2022-01-25T01:46:10Z")

</div>

And one more thing pal

```auto
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)
