if()else{} - Expressions Advanced

I've talked about if(){}else{} a lot of times. In "Interesting Expressions", Create a Circular Preloader, and there's the Expressions Basics ebook of course.

I'm going to discuss a couple of things that you can do with ifelse.

Please note that I won't use the full thisComp.layer(*) etc for the sake of ease of reading. I'll just use Null 1 or Red Solid and so on, but will format them differently.

Less than, greater than, and extra conditions



As part of ifelse, you can use something like "if this value is less than this other one". This is obviously done in the () section after the if. So how do you use them?
==    Equal to
<      Less than
>      Greater than
<=    Less than or equal to
>=    Greater than or equal to

(Adding an ! before the above symbols will do the opposite. So !== is Not Equal to)

You can also use pipes and ampersands to define more than one condition in the ( ) section.
Say I apply this expression to the position of a layer:


if(Null 1.position[1] <= Null 2.position[1] && Null 1.opacity > Null 2.opacity) {[10,10]} else {[250,250]};


Let's break it down first.
If Null 1's Y position is less than or equal to Null 2's Y position AND Null 1's opacity is greater than Null 2's opacity, set the position as [10,10]. If neither of these conditions are true, then position will be [250,250];

&& is used to say BOTH must be true.

|| is used to say EITHER must be true:


if(Null 1.position[1] <= Null 2.position[1] || Null 1.opacity > Null 2.opacity) {[10,10]} else {[250,250]};


If Null 1's Y position is less than or equal to Null 2's Y position OR Null 1's opacity is greater than Null 2's opacity, set position as [10,10]. If neither of these conditions are true, then position will be [250,250];

Makes sense? Cool!

Defining Variables


Did you know that you can have a variable's value change with an ifelse expression? However, there's some syntax to it.
I tried this:


checker=(if(Null 1 Checkbox == 1) {45} else {200});


What I was hoping to achieve was that checker would be 45 if it was on, and 200 if it was off. But this doesn't work. You get an error saying "Illegal use of reserved word".
The proper way to do it is:


if(Null 1 Checkbox == 1) {checker=45} else {checker=200};


Basically, everything concerning an if needs to happen INSIDE the {}, not outside. So if you face that error, there's your solution.

Yo dawg...


if inside an if? It's great for complex sets, such as those that power INTERFERE.


if(Null 1 checkbox == 0) {1} else {
if(Null 2 checkbox == 0) {1} else {
if(Null 3 checkbox == 0) {1} else {
0}}}


So what's going on? This expression basically returns a value of zero only if all the three check-boxes are ON (since the zero is in ELSE). The flow makes sense, but there's a much easier and less wordy way to achieve this:


if(Null 1 checkbox == 0 && Null 2 checkbox == 0 && Null 3 checkbox == 0) {1} else {0};


For one, that's much shorter and does the exact same thing! So it's good to think about this stuff when writing expressions. Keep in mind although the second expression seems harder to read, it only involves "1" once, where as the one above involves 1 three times. If you're going to make a complex expression for a preset, you should consider this.

Glad I got to clear that up.
If you have any questions, drop a comment below :-D

Comments

Popular Posts