Talk:IfElse: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
Line 51: Line 51:
Finally I think the and-or-statement is still more effective that IfElse.
Finally I think the and-or-statement is still more effective that IfElse.
[[User:NeonBlack|NeonBlack]] 12:19, 4 June 2009 (CEST)
[[User:NeonBlack|NeonBlack]] 12:19, 4 June 2009 (CEST)
----
Swapping the arguments is fine if you're sure what the values are when you write the code.
For example, which one of these will not be false?
<syntaxhighlight lang="lua">name = (whichPlayer) and getPlayerName(player1) or getPlayerName(player2)</syntaxhighlight>
Also, as a crazy example, choosing between false and nil is impossible with and-or:
<syntaxhighlight lang="lua">moo = (variable) and false or nil
moo == always nil</syntaxhighlight>
<syntaxhighlight lang="lua">moo = (variable) and nil or false
moo == always false
</syntaxhighlight>
IfElse <3
[[User:Ccw|MrSmartArse]] 17:06, 4 June 2009 (CEST)
----

Latest revision as of 16:22, 4 June 2009

There's not really any use for this, as far as i can tell. Lua supports this by its syntax sugars:

moo = (variable) and (ifTrue) or (ifFalse)

works the same as

moo = (varible) ? ifTrue : ifFalse

in C++.


oO You're right. Never thought of that. What I was thinking about, too, was something like

moo = ({ [true] = ifTrue, [false] = ifFalse })[condition]

But your code is much better. Thanks for posting this. ;D NeonBlack 22:00, 18 May 2009 (CEST)


The problem with the plain Lua version is that 'ifTrue' is also part of the test condition. Consider this:

variable = true
ifTrue = false
ifFalse = true

moo = (variable) ? ifTrue : ifFalse
== false

ElseIf works fine here:

moo = IfElse( variable, ifTrue , ifFalse )
== false

Lua's and's 'n' or's however may give unexpected results:

moo = (variable) and (ifTrue) or (ifFalse)
moo == true

MrSmartArse 08:46, 4 June 2009 (CEST)


it returns true because one of the two first arguments are false.

this is lua's fault.

Flobu 11:43, 4 June 2009 (CEST)


That's no real problem since the only values that make a condition fail are nil and false. You won't want to use any of them as a result if the condition is true. In this case you should just swap the ifTrue and ifFalse and negate the condition.

local moo = not true and true or false
moo == false

You would just have to remember that ifTrue must not be false or nil and that instead of that ifFalse can be false or nil. Finally I think the and-or-statement is still more effective that IfElse. NeonBlack 12:19, 4 June 2009 (CEST)


Swapping the arguments is fine if you're sure what the values are when you write the code. For example, which one of these will not be false?

name = (whichPlayer) and getPlayerName(player1) or getPlayerName(player2)

Also, as a crazy example, choosing between false and nil is impossible with and-or:

moo = (variable) and false or nil
moo == always nil
moo = (variable) and nil or false
moo == always false

IfElse <3

MrSmartArse 17:06, 4 June 2009 (CEST)