# Long if statements

**URL:** <https://forum.kirupa.com/t/long-if-statements/186774>\
**Category:** Uncategorized\
**Created:** [April 24, 2006, 3:22pm UTC](https://forum.kirupa.com/t/long-if-statements/186774 "2006-04-24T15:22:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![g5604](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@g5604](https://forum.kirupa.com/u/g5604)\
**Post date:** [April 24, 2006, 3:22pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/1 "2006-04-24T15:22:32Z")

</div>

hi,

i have fairly long if statements like this:

```php
if (_parent._parent.curr_chan == _parent._parent.chan_one) {
        _parent._parent.chan_one.filters = _parent._parent.filter1Array;
    }
    if (_parent._parent.curr_chan == _parent._parent.chan_two) {
        _parent._parent.chan_two.filters = _parent._parent.filter2Array;
    }
    if (_parent._parent.curr_chan == _parent._parent.chan_three) {
        _parent._parent.chan_three.filters = _parent._parent.filter3Array;
    }

```

is it better practice (and easy for flash to handle ) to use a switch statement and/or should i alway provide an else outcome?

cheers.  
G

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 3:29pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/2 "2006-04-24T15:29:19Z")

</div>

I usually use switch statements once it goes beyond three options. It’s really for readability.

At the end of the day, once compiled into an SWF, it is coded the same way for the Flash Player, so there really isn’t much of a difference:)

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 3:34pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/3 "2006-04-24T15:34:53Z")

</div>

you can do two things to make this both more reusable, and increase performance.

```auto

var path:Object = _parent._parent;
var filters:Array;
switch (path.curr_chan)
{
case path.chan_one :
    filters = path.filter1Array;
    break;
case path.chan_two :
    filters = path.filter2Array;
    break;
case path.chan_three :
    filters = path.filter3Array;
    break;
}
path.churr_chan.filters = filters;

```

Here we are setting a path so that you don’t have to type it on every line… it’s pretty intuitive.

**If/Else VS Switch**

The one thing I wanted to remark on was the use of switch over if/else. Now if you look at the bytecode produced by an if/else and a switch that are doing the same thing, you’ll see that it is the exact same. With that said, switch will perform better than an if/else statement because if/else allows for values in it’s expression to change dynamically, which means that when the expression is evaluated the value _may_ have to be retrieved from a location in memory, this process takes time. Now with switch statements the case is a predetermined value that is set at compile time… which means that the your expression is compared to a set value which does not need to be pulled out of memory.

I hope this made sense, if not please feel free to ask questions.

Take Care

\_Michael

---

<div class="post-metadata">

**Author:** ![mpelland](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mpelland](https://forum.kirupa.com/u/mpelland)\
**Post date:** [April 24, 2006, 3:35pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/4 "2006-04-24T15:35:35Z")

</div>

there are a couple ways to make this prettier…

one think, if you should look at is creating a variable for the curr\_chan… or at very least the parent.parent. ie

```auto

var secondParent = _parent._parent;
var curChan = secondParent .curr_chan;

if (curChan == secondParent.chan_one) {
   secondParent.chan_one.filters = secondParent.filter1Array;
} 

```

to shoten it a bit more you have the option of just cleaning up your if statement(s) or making a swf

```auto
if (curChan == secondParent.chan_one) {
        secondParent.chan_one.filters = secondParent.filter1Array;
    } else if (curChan == secondParent.chan_two) {
        secondParent.chan_two.filters = secondParent.filter2Array;
    } else if (curChan == secondParent.chan_three) {
        secondParent.chan_three.filters = secondParent.filter3Array;
    } 

```

or

```auto

switch(curChan) {
case secondParent.chan_one:
   secondParent.chan_one.filters = secondParent.filter1Array;
   break;
case secondParent.chan_two:
   secondParent.chan_two.filters = secondParent.filter2Array;
   break;
case secondParent.chan_three:
   secondParent.chan_three.filters = secondParent.filter3Array;
   break;
default: 
  break;
}

```

i think that it might work to your benefit to just work with a modified if statement if you are only using 3 conditions

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 3:41pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/5 "2006-04-24T15:41:00Z")

</div>

Here is a look at the actionscript vs bytecode comparison of if/else and case…

I will list both the if / else and the case, but I will only have one listing of bytecode, that is because when compiled they both share the same thing ;).

```auto

if (myVar == 5)
{
	myVar = 5;
}
else if (myVar == 4)
{
	myVar = 5 - 5;
}
else
{
	myVar = 1;
}

switch (myVar)
{
	case 5:
		myVar = 5;
		break;
	case 4:
		myVar = 5 - 5;
		break;
	default:
		myVar = 1;
		break;
}

```

and the bytecode

```auto

movie 'IFCase.swf' compressed // flash 8, total frames: 1, frame rate: 12 fps, 550x400 px

  frame 0
    constants 'myVar'  
    push 'myVar'
    getVariable
    push 5
    equals
    not
    branchIfTrue label1
    push 'myVar', 5
    setVariable
    branch label3
   label1:
    push 'myVar'
    getVariable
    push 4
    equals
    not
    branchIfTrue label2
    push 'myVar', 0.0
    setVariable

```

And please note that there is a difference between the two, and they are not always interchangable, and if used correctly one can have a performance benefit over the other.

Take Care.

\_Michael

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 24, 2006, 3:45pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/6 "2006-04-24T15:45:05Z")

</div>

Thanks Mike that was informative.:thumb:

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 3:53pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/7 "2006-04-24T15:53:08Z")

</div>

😉 No problem.

On a side note, I am currently putting together a book proposal to send to O’reilly and FriendsOfEd…

Assuming that either are interested, this book’s focus is on efficiency and how to program Actionscript in a way that best fits the application you are working on. I go over a few topics that are very important for any programmer to know (especially us Actionscripters, due to Actionscripts complexes 😛 )…

Hopefully this all will work out 🙂 I promise it will be more informative than I can be on the forums. Take Care.

\_Michael

---

<div class="post-metadata">

**Author:** ![g5604](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@g5604](https://forum.kirupa.com/u/g5604)\
**Post date:** [April 24, 2006, 3:53pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/8 "2006-04-24T15:53:20Z")

</div>

wow! great replies thanks guys! always good to learn better ways of doing things, i like clean code 🙂

---

<div class="post-metadata">

**Author:** ![g5604](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@g5604](https://forum.kirupa.com/u/g5604)\
**Post date:** [April 24, 2006, 4:03pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/9 "2006-04-24T16:03:42Z")

</div>

Michael, i would be very intrested in that book :pleased:

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 24, 2006, 4:09pm UTC](https://forum.kirupa.com/t/long-if-statements/186774/10 "2006-04-24T16:09:01Z")

</div>

Well I will definitly be keeping everyone up to date with how it’s looking…

Thanks for your interest, and feedback… truly means the world.

Take Care  
\_Michael
