• A quick question, if and else if
    7 replies, posted
I have an 'if' statement, I then want to add multiple conditions to it. So which is the better method? [code] If 'argument1' end If 'argument2' end [/code] OR [code] If 'argument1' else if 'argument2' end [/code] Thanks for any help, I just want to know which is a better method and why. Thanks -Duby
You would use 'else' because then it wouldn't be reached if the first statement was true, saving a few cycles.
It depends. Are they related, or are they completely different? What is the purpose? For example: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/loading_files_across_realms.lua[/url] Notice the if/else for SERVER / CLIENT. Those are related and fit together. the ELSEIF isn't needed for those examples ( as an else would be the inverse of SERVER or CLIENT ) but they're included to help answer questions like this.
In this instance it wouldn't matter if it was a server or client side. But rather for if I was using Lua for non gmod purposes. So imagine a program requiring 3 conditions which could be outputted. Which of the two methods above would work best>? Or would be more efficient.
[QUOTE=DubyxD;44428071]In this instance it wouldn't matter if it was a server or client side. But rather for if I was using Lua for non gmod purposes. So imagine a program requiring 3 conditions which could be outputted. Which of the two methods above would work best>? Or would be more efficient.[/QUOTE] A better way to look at it is: [lua] if A then -- CodeA elseif B then -- CodeB else -- CodeC end [/lua] Execute CodeA if A statement is true otherwise if A statement is false, but B statement is true, execute CodeB otherwise execute CodeC
If it's one option out of many, I'd use an elseif. For example: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/database/_connecting_with_fallbacks_in_place.lua[/url] When setting up a fall-back system you load your preferred object first. If it fails, you load your secondary. If it fails you can load a third and continue on, if you will. If you were to use if then end, if then end; you'd have to add additional overhead to test for the value. Example: [lua]require( "tmysql" ); if ( tmysql ) then // We know tmysql module loaded if we're here else // We know tmysql module failed to load if we're here end[/lua] vs [lua]require( "tmysql" ); if ( tmysql ) then // We know tmysql module loaded if we're here end // Once we get here, if we didn't use a return, we now need to test for the inverse of the above.. If you were to require mysqloo right above the if, then you'd need to do a check for !tmysql && mysqloo ... require( "mysqloo" ); if ( !tmysql && mysql ) then end // Whereas instead of that previous if, we can simplify like: if ( !tmysql ) then // We know tmysql module failed to load if we're here require( "mysqloo" ) if ( mysqloo ) then // We know mysqloo loaded if we're here end if ( !mysqloo ) then // We know mysqloo failed to load if we're here end end // Whereas instead of using a mysqloo and !mysqloo separately, it could be an if/else/end. [/lua] It comes down to personal preference. I know of people that will do redundant things like: [lua]local _rand = math.random( 1, 100 ); if ( _rand > 50 ) then if ( _rand > 50 && _rand > 75 ) then end end[/lua] "Just because it makes them feel more 'sure' that the logic won't mess up......" Take a look at this: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/tracking_players/player_connect_and_disconnect_with_list_example.lua[/url] It is good to practice things like this: [lua] if ( !PLAYERS_TRACKING_LIST ) then PLAYERS_TRACKING_LIST = { }; end if ( !PLAYERS_TRACKING_LIST[ _steamid ] ) then // Define the structure and initialize data. PLAYERS_TRACKING_LIST[ _steamid ] = { names = { _name }; steamid = _steamid; kicks = 0; bans = 0; disconnect_reasons = { }; }; else // If they changed their name; add it to the list. if ( !table.HasValue( PLAYERS_TRACKING_LIST[ _steamid ].names, _name ) ) then table.insert( PLAYERS_TRACKING_LIST[ _steamid ].names, _name ); end end[/lua] instead of something like if !List then declare it, and do the other logic in there too; because with the latter, you'd end up needing to do logic or repeat code for the same result In the end, it comes down to personal preference. Logic is universal, if you can simplify it one way, then I'd say go for it. If you add redundant statements, as long as they aren't redundant loops, it won't affect performance too drastically unless you're coding something whereby you are required to squeeze every ounce of performance out of something.
Hmmm very interesting thanks! All the answers above have helped me understand a bit more about when it is appropriate to use either of the two. This was sprung from me and a friend having a debate on which was a better method to use. Of course we have learnt it literally depends on the situation. :)
There are almost infinitely many possible ways to create functional code which all does the same thing but is written completely differently with varying final file-size. Just because it's the smallest doesn't mean it's the fastest, just because it's the largest doesn't mean it's the slowest, just because 10 ifs are used where another one uses 1 if and 1 else or ternary operations; it doesn't matter. If you're ever confused, or what to check the speed on something, use this: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_hud_stuff.lua[/url] Also, please mark solved if it's "solved". It's pretty much an open-ended question so you could leave it open and maybe others will contribute too.
Sorry, you need to Log In to post a reply to this thread.