# Have code inside if else statement run continuosly

**URL:** <https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765>\
**Category:** Processing.py\
**Created:** [January 23, 2019, 4:28pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765 "2019-01-23T16:28:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiedbranches](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tiedbranches](https://discourse.processing.org/u/tiedbranches)\
**Post date:** [January 23, 2019, 4:28pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765/1 "2019-01-23T16:28:33Z")

</div>

Hi, I’m trying to have different bits of code inside draw() run continuously but be activated by different key presses.

The problem is that what is inside the if else statement only runs while I am pressing the key.

What I’d like is to be able to do, in general, but I’m having trouble implementing it in Processing, is to switch to different “modes”, each of which should continuously once activated, just like a simple draw() function would do.

Any suggestions will be greatly appreciated!

Here is an example of the issue:

```auto
def setup():
    size(400,400)
    background(255)
               
class Point(object):
    
    def __init__ (self,position):
        self.position=position
        
    def vector(self):
        stroke(0)
        #self.position.mult(0.3)
        line(self.position.x,self.position.y,mouseX,mouseY)
        
        
def draw():
    
    
    if keyPressed==True and key=="a":
        background(255)
        for i in range(0,width,20):
            a=Point(PVector(i,0))
            a.vector()
            b=Point(PVector(i,height))
            b.vector()        
        for j in range(0,height,20):
            c=Point(PVector(0,j))
            c.vector()
            d=Point(PVector(width,j))
            d.vector()
            
    elif keyPressed==True and key=="s":
        background(255)
        for i in range(0,width,40):
            a=Point(PVector(i,0))
            a.vector()
            b=Point(PVector(i,height))
            b.vector()        
        for j in range(0,height,40):
            c=Point(PVector(0,j))
            c.vector()
            d=Point(PVector(width,j))
            d.vector()

```

---

<div class="post-metadata">

**Author:** ![Bluu](https://avatars.discourse-cdn.com/v4/letter/b/a88e4f/32.png) [@Bluu](https://discourse.processing.org/u/Bluu)\
**Post date:** [January 23, 2019, 4:33pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765/2 "2019-01-23T16:33:22Z")

</div>

You should probably have the else function that recursively calls itself, I don’t know python, but here’s a psuedocode interpretation:

```auto
keypressed(){
if(key=="f"){
recurse();
}
}
recurse(){
dosomething();
recurse();
}

```

---

<div class="post-metadata">

**Author:** ![tiedbranches](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tiedbranches](https://discourse.processing.org/u/tiedbranches)\
**Post date:** [January 23, 2019, 4:48pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765/3 "2019-01-23T16:48:38Z")

</div>

If I call the function wherein the if statement is contained—draw()—at the end of the if statement, Processing feels I have exceeded the maximum recursive depth an outputs this:

Runtime Error: maximum recursion depth exceeded(Java StackOverflowError)

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 23, 2019, 5:08pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765/4 "2019-01-23T17:08:00Z")

</div>

You could probably make an integer or some other value, indicating what state it is right now, and then use that.

```auto
state = 0
...
def draw():
  if keyPressed==True and key=="a":
    state = 1
  elif keyPressed==True and key=="s":
    state = 2

  if state==1:
    #Do stuff
  elif state==2:
    #Do other stuff
```

---

<div class="post-metadata">

**Author:** ![tiedbranches](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tiedbranches](https://discourse.processing.org/u/tiedbranches)\
**Post date:** [January 23, 2019, 5:23pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765/5 "2019-01-23T17:23:44Z")

</div>

I tried, but it keeps telling me the local variable “state” was referenced before assignment. I’ve tried assigning “state” in setup() and before setup(), have even asked setup() to print the variable “state” after having assigned it, and it does, but then says it is not assigned!

```auto
def setup():
    state=0
    print(state)
    size(400,400)
    background(255)

def draw():
    
    
    if keyPressed==True and key=="a":
        state=1
        
    elif keyPressed==True and key=="s":
        state=2
            
    if state==1:
       _#1st mode_
            
    elif state==2:
        _# 2nd mode_
       

```

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [January 23, 2019, 5:30pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765/6 "2019-01-23T17:30:15Z")

</div>

Hi,

You need to make your variable `global`

```auto
state = 0

def setup():
    size(400, 400)
    fill('#000000')

def draw():
    background('#ffffff')
        
    text('STATE = %i' % state, width>>1, height>>1)
        
    
def keyPressed():
    global state

    if key == 'a': state = 1
    elif key == 's': state = 2

```

---

<div class="post-metadata">

**Author:** ![tiedbranches](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@tiedbranches](https://discourse.processing.org/u/tiedbranches)\
**Post date:** [January 23, 2019, 5:45pm UTC](https://discourse.processing.org/t/have-code-inside-if-else-statement-run-continuosly/7765/7 "2019-01-23T17:45:51Z")

</div>

Yes! That’s it, thanks @solub and all.
