Break an if statement

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
KayleMaster
Party member
Posts: 212
Joined: Mon Aug 29, 2016 8:51 am

Break an if statement

Post by KayleMaster » Wed Sep 07, 2016 8:32 am

Code: Select all

    if button==2 then 
        -----------------------------------------------------------------------------------northwest
    	if LocalX < 1 and LocalY < 1 and LocalX > -64 and LocalY > -64 then                                    
    	object_chunk_northwest[LocalX+64][LocalY+64]=1; update_objects("northwest"); end
        -----------------------------------------------------------------------------------west
    	if LocalX < 1 and LocalY < 65 and LocalY > 0 and LocalX > -64 then                           
    	object_chunk_west[LocalX+64][LocalY]=1; update_objects("west"); end
        -----------------------------------------------------------------------------------center
    	if LocalX > 0 and LocalY > 0 and LocalX <65 and LocalY<65 then               
    	object_chunk[LocalX][LocalY]=1; update_objects("center"); end
        -----------------------------------------------------------------------------------north
    	if LocalX > 0 and LocalY < 1 and LocalX<65 and LocalY>-64  then              
    	object_chunk_north[LocalX][LocalY+64]=1; update_objects("north"); end
        -----------------------------------------------------------------------------------southwest
    	if LocalX > -64 and LocalY > 64 and LocalX <1 and LocalY<129 then              
    	object_chunk_southwest[LocalX+64][LocalY-64]=1; update_objects("southwest"); end
        -----------------------------------------------------------------------------------northeast
    	if LocalX > 64 and LocalY > -64 and LocalX <129 and LocalY<1 then            
    	object_chunk_northeast[LocalX-64][LocalY+64]=1; update_objects("northeast"); end
        -----------------------------------------------------------------------------------east
    	if LocalX > 64 and LocalY < 65 and LocalX <129 and LocalY>0 then               
    	object_chunk_east[LocalX-64][LocalY]=1; update_objects("east"); end
        -----------------------------------------------------------------------------------south
    	if LocalX > 0 and LocalY < 129 and LocalX <65 and LocalY>64 then             
    	object_chunk_south[LocalX][LocalY-64]=1; update_objects("south"); end
        -----------------------------------------------------------------------------------southeast
    	if LocalX > 64 and LocalY < 129 and LocalX <129 and LocalY>64 then              
    	object_chunk_southeast[LocalX-64][LocalY-64]=1; update_objects("southeast"); end

So let's guess that local x and local y belong to northwest, the code is executed but it still checks whether it's in west, center, north etc (which is not possible). After the execution of northwest for example, I want to end the block and NOT continue to execute the other if's.
I know it's possible, just don't know the syntax.

User avatar
raidho36
Party member
Posts: 1943
Joined: Mon Jun 17, 2013 12:00 pm

Re: Break an if statement

Post by raidho36 » Wed Sep 07, 2016 8:47 am

You can't place "break" anywhere in Lua 5.1, this operation is reserved for breaking the loops specifically. So in order to break the execution, you must enclose it in a loop. Alternatively, you can use "goto" statement.

Note that you probably wouldn't gain anything by doing this since in this particular scenario the CPU will know that any of those branches are "highly unlikely" and will try to skip them altogether by pre-choosing the "false" branch, only reverting to executing one if it finds out it was necessary after all.

Also, if this is perofrmance-critical section, this approach wouldn't have been helpful. Instead of eliminating code execution through branching, you should eliminate the branching altogether, and make it utilize mathematical operations in order to process your data properly instead of relying on multitude of hard-coded routines.

As a more general advice on optimization, learn the quirks of software you're working with (LuaJIT, OpenGL, etc.), and learn the quirks of hardware you're working with (x86/ARM-based CPUs, DDR memory, modern GPUs, etc). Without knowing those two, any attempt at optimization is just a shot in the dark.
Last edited by raidho36 on Wed Sep 07, 2016 9:01 am, edited 1 time in total.

User avatar
0x72
Citizen
Posts: 51
Joined: Thu Jun 18, 2015 9:02 am

Re: Break an if statement

Post by 0x72 » Wed Sep 07, 2016 8:58 am

what you need is elseif

User avatar
zorg
Party member
Posts: 2731
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Break an if statement

Post by zorg » Wed Sep 07, 2016 9:23 am

As /H/ said, using elseif will ensure that at most one branch will execute. That said, that might also come with its own drawbacks.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
raidho36
Party member
Posts: 1943
Joined: Mon Jun 17, 2013 12:00 pm

Re: Break an if statement

Post by raidho36 » Wed Sep 07, 2016 9:27 am

Huh, I didn't paid attention to the code snippet, it doesn't apparently uses elseif.

Then again, the only difference in assembly for either scenario is that with elseif it'll jump to after the last statement after executing any of them. Not that it matters because the CPU will try to skip through all of them as if they evaluated to "false", resulting in sequential jumping from last condition to the next without actually stopping by, and then reverting to executing the single one that did evaluated to true.

Zeliarden
Party member
Posts: 139
Joined: Tue Feb 28, 2012 4:40 pm

Re: Break an if statement

Post by Zeliarden » Wed Sep 07, 2016 9:29 am

return?

KayleMaster
Party member
Posts: 212
Joined: Mon Aug 29, 2016 8:51 am

Re: Break an if statement

Post by KayleMaster » Wed Sep 07, 2016 9:39 am

It's not performance critical, like at all. Just good practice I guess.
Elseif isn't necessary because the values are so, that only 1 branch will execute. I guess I'm splitting hairs too much here.

User avatar
zorg
Party member
Posts: 2731
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Break an if statement

Post by zorg » Wed Sep 07, 2016 7:07 pm

In hindsight, i should have said that (depending on order and probability), elseifs will minimize comparisons. But as you said, unless it's in a critical part of a program, it shouldn't matter that much.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests