# Scripting - IF ELSE

**URL:** <https://forum.inductiveautomation.com/t/scripting-if-else/39324>\
**Category:** Ignition\
**Created:** [October 2, 2020, 6:36pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324 "2020-10-02T18:36:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 2, 2020, 6:36pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/1 "2020-10-02T18:36:47Z")

</div>

Hi all,

I am learning Ignition, scripting, and Databases all at the same time and I am stuck.

I am trying to add an if else statement to a script I have so I set up a window just to play with. I have 2 text fields and a button with this actionPerformed script.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/2/12472587fe048c72008482e9b58a1ac1006ab125.png)

Line 6-7 never works. if name1 is not empty, the Text Field gets the name1 value.

Something is wrong with line 5 or 6 but I have tried all I can guess to try and can’t make it work.

What am I missing?

Thanks for the help!!

Steven

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [October 2, 2020, 6:41pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/2 "2020-10-02T18:41:29Z")

</div>

`if name1 == "":`

In Python, `is` tests if two variables refer to the same object

Also you can drop the semicolons, although they won’t hurt anything

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [October 2, 2020, 6:48pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/3 "2020-10-02T18:48:22Z")

</div>

For bonus points use a ternary operator. Delete from row 5 on and do

`name = name1 if name1 else username`

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 2, 2020, 6:59pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/4 "2020-10-02T18:59:08Z")

</div>

@chasondeshotel Thank you !! That was my original syntax, but chasing other issues made me change it and I guess I never went back to it when I corrected the other issue.

Thanks for the help!!!

Steven

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 2, 2020, 7:57pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/5 "2020-10-02T19:57:58Z")

</div>

@chasondeshotel,

following up here…how do I continue to run the code after the if else statement? Seems like a LONG time ago it used to be END IF:. Seems things have changed a lot since then.

so…

if this is true  
do this  
else  
do that  
then keep doing  
the  
consecutive  
lines to the end.

Thanks, Steven

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [October 2, 2020, 8:07pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/6 "2020-10-02T20:07:46Z")

</div>

Unless you specifically call a return or something of that nature the code will continue past each if.  
For multiple you should use the elif as well.

```auto
name1 = event.source.parent.getComponent('Text Field').text
if name1:
    if name1=='Bob':
        event.source.parent.getComponent('Text Field 1').text = 'Bob'
    elif name1=='Joe':
        event.source.parent.getComponent('Text Field 1').text = 'Joe'
    else:
        event.source.parent.getComponent('Text Field 1').text = name1
else:
    event.source.parent.getComponent('Text Field 1').text = 'Nothing Entered'

```

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [October 2, 2020, 8:18pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/7 "2020-10-02T20:18:30Z")

</div>

In Python, whitespace is used to denote scope… so levels of indenting matter. In this example, when your indention level recedes one, the interpreter knows you’ve exited the `if` block. This is also the reason you don’t normally use semicolons in Python

And for more bonus points, Python’s `for` loop also has an `else`. It’s executed only if there was not a `break` condition

```auto
for thing in iterable:
    if find_thing(thing):
        # found it!
        do_stuff(thing)
        break
else:
    # he ain't here ._.
    thing_not_found()

```

Which can be handy and almost makes up for their terrible ternary syntax

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 2, 2020, 8:50pm UTC](https://forum.inductiveautomation.com/t/scripting-if-else/39324/8 "2020-10-02T20:50:06Z")

</div>

@chasondeshotel so I don’t even need the else. That works just like I wanted.

Thanks for the examples. I am sure I will put all this to good use in the near future.

Thanks, Steven
