TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 17 Mar 2017 05:58 PM |
~Many programming languages including Lua have a useful feature to handle boolean expressions. Instead of writing out long multi-line if statements, we can combine them into a one line expression! Another way to handle this is with switch statements, but Lua does not have these! Let me show you an example.
local k = 0 local c = 0
if k == 0 then c = 1 elseif k == 1 then c = 2 elseif k == 2 then c = 3 else c = false end
print(c) -- 1
--Here, we compare k multiple times and then return c. The result is 1.
local k = 3 local c = 0
c = k == 0 and 1 or k == 1 and 2 or k == 2 and 3 or false
print(c) -- false
The same can be done in one line! Isn't that cool and useful!? Say goodbye to if statements! Although a downside might be readability, but this can shorten your code alot!
Let me know what you guy think. You can expand on this to offer better clarification if need be!
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
VoidFrost
|
  |
| Joined: 14 Oct 2011 |
| Total Posts: 1188 |
|
|
| 17 Mar 2017 06:01 PM |
| Is this like a, tutorial series you're doing? |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
| |
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 17 Mar 2017 06:02 PM |
@Void no, just a heads up to novices
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 17 Mar 2017 06:03 PM |
It's less efficient and more misleading and confusing to do that than just some if statements.
Only do it if it's obvious. If it looks like "c = k == 0 and 1 or k == 1 and 2 or k == 2 and 3 or false" then clearly you shouldn't be doing it. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
| |
|
VoidFrost
|
  |
| Joined: 14 Oct 2011 |
| Total Posts: 1188 |
|
|
| 17 Mar 2017 06:04 PM |
| I knew about this, but I don't know if it's faster than if statements. Is it? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 17 Mar 2017 06:05 PM |
| I just mentioned that. It's almost always slower if you have more than 2 expressions but in the case of you just doing "local x = y or z" it's (negligibly) faster. |
|
|
| Report Abuse |
|
|
VoidFrost
|
  |
| Joined: 14 Oct 2011 |
| Total Posts: 1188 |
|
|
| 17 Mar 2017 06:06 PM |
Yeah the word efficient doesen't always equal to the definition of faster.
Alright then. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 17 Mar 2017 06:07 PM |
The speed difference for small things is negligible. But like cntkillme said, technically it is less "efficient".
Let's also be clear that efficiency is relative.
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 17 Mar 2017 06:09 PM |
(and usually negligible) Just use whatever looks nice.
local msg = banned and "You're banned" or "Welcome to the game" looks nicer than if banned then msg = "Youre banned" else msg = "Welcome to the game" end
IMO |
|
|
| Report Abuse |
|
|
VoidFrost
|
  |
| Joined: 14 Oct 2011 |
| Total Posts: 1188 |
|
|
| 17 Mar 2017 06:09 PM |
I think it's obvious that doing this
c = k == 0 and 1 or k == 1 and 2 or k == 2 and 3 or false
Looks terrible compared to the if statement way. Terrible to read and as far as I understand it, slower. In that case, it takes the away the point of doing such a method. Of course it's different for simpler examples. |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 17 Mar 2017 06:13 PM |
Void are you even bothering to read what Cntkillme just said Let me paraphrase:
Compact albeit complex conditionals using ternary operators are faster than multiple if-thens, however, they can be more confusing, and should be avoided if comprehension is sacrificed because of the very negligible different.
|
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 17 Mar 2017 06:13 PM |
negligible efficiency difference*
|
|
|
| Report Abuse |
|
|
VoidFrost
|
  |
| Joined: 14 Oct 2011 |
| Total Posts: 1188 |
|
|
| 17 Mar 2017 06:14 PM |
| " It's almost always slower if you have more than 2 expressions " |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 17 Mar 2017 06:14 PM |
| No I said they were slower |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 17 Mar 2017 06:15 PM |
I flipped it in my mind while I was writing it
I think we all get the point now
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 17 Mar 2017 06:17 PM |
in my engine i have the following line
if PiecePawn[p] and PieceCol[p] == PieceCol[pce] then score = score - Bishop InFrontOfPawn end
We could instead write that as
score = (PiecePawn[p] and PieceCol[p] == PieceCol[pce]) and score - Bishop InFrontOfPawn or score
But yes, like you said, cases like that, its probaby preferrable to use if statements instead
|
|
|
| Report Abuse |
|
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
|
| 17 Mar 2017 07:15 PM |
this is how java does it
int num = 1;
switch(num) { case 1: System.out.println("num is one!"); break; case 2: System.out.println("num is two!"); break; } |
|
|
| Report Abuse |
|
|
|
| 17 Mar 2017 07:20 PM |
No... the ternary in Java works like this:
int var2 = var1 == 5 ? 1 : 2
|
|
|
| Report Abuse |
|
|
XCVlll
|
  |
| Joined: 23 Oct 2008 |
| Total Posts: 963 |
|
|
| 17 Mar 2017 08:07 PM |
| @Un, I thought he was talking about the switch loop mb |
|
|
| Report Abuse |
|
|
Wrathsong
|
  |
| Joined: 05 Jul 2012 |
| Total Posts: 22393 |
|
|
| 17 Mar 2017 08:10 PM |
parenthesis = better readability for longer ones
Ever wanted to learn how to script? Check out my YouTube channel: youtube.com/austintheslayer Want to see me code in action? Follow me on twitch: twitch.tv/austinrblx |
|
|
| Report Abuse |
|
|