What does ‘else’ actually do?
-
If something then
Do a thing
EndifVery simple and straightforward. You can see if things are more than, less than, equal to, not equal to, the same as (like for strings), see if things are not the same or not more or not less etc. I do loads of stuff using those. Sometimes I do long chains of ‘if something and if not something and...’ sometimes I put more ifs inside other ifs and it all works out. I’ve never had to use an ‘else’. So what does it do and how would I use it? :S
-
@toxibunny
The expression in an if statement (something in your example) can only have one of two outcomes:
- it's true, the block within the if statement is executed
- it's false, the block within the if statement is not executed
Sometimes you also want to do something if the expression results into false (scenario 2), but it's something different from if the expression is true (scenario 1).
You could write this as follows:
var c = controls(0) if c.a and c.b then // do scenario 1 endif if !(c.a and c.b) then // do scenario 2 endif
This would work, but it's more laborious to maintain and to understand. You might even forget why it's written this way later, and update one if statement, but forget to update the other.
By using the
else
keyword, you make this more compact, and you only need to maintain the if condition in one place:var c = controls(0) if c.a and c.b then // do scenario 1 else // do scenario 2 endif
Edit:
Besides this point, if you have a complex expression that you use in many places, it could also be a good idea to move that to a separate function, so in this example that would lead to:function doABarrelRoll() var c = controls(0) return c.a and c.b if doABarrelRoll() then // do scenario 1 else // do scenario 2 endif
By doing this, the function-name reminds you what the combination of checks actually stand for, the next time you look at it. And on top of it, if you want to change the controls for
doABarrelRoll
in this example, you only need to change one line of code. -
Thanks, that’s much clearer now. I like what you did here:
function doABarrelRoll()
var c = controls(0)
return c.a and c.bThe last line returns a 1 if they’re both 1, but 0 otherwise, is that right? I never thought of doing it that way. It’s nice and neat.
-
This post is deleted! -
@toxibunny said in What does ‘else’ actually do?:
The last line returns a 1 if they’re both 1, but 0 otherwise, is that right?
Yes, you are correct. For accuracy, I feel like I need to say more, but you can ignore this:
The more correct phrasing would be:
The last line returns a
true
if they’re both 1, butfalse
otherwiseIn some other languages, the expression
2 and 3
would result in3
, but in Fuze2 and 3
results in1
. This means that1
in this context is treated as a boolean, not an int. So even though1
andtrue
are technically the same data in Fuze, by calling ittrue
it's more clear that you don't need to expect the function to return a different numerical value. -
@PB____ , nice refactor and explanation of if-else : )