Raised This Month: $ Target: $400
 0% 

How to transfer a lot of options in "for" operator


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 05-30-2015 , 11:23   Re: How to transfer a lot of options in "for" operator
Reply With Quote #10

Quote:
Originally Posted by groofshark View Post
That's for all menu. I want instead adding 20 if( g_options[id][iItem] ) just to make a loop and that loop to give on every "IF" - option and his number(iItem)
You don't need to loop unless you have a more complex situation.

Code:
/**  * Given this enum the following things are true:  *    opt1 == 0 && opt2 == 1 && opt3 == 2  */ enum _:options {     opt1,     opt2,     opt3, } /**  * That means that this if can easily be reduced.  * Observe that when |iItem == 0| then |opt1 == iItem| is also true.  */ if(g_options[id][opt1] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * So instead we can write it as following, note that |opt1| has  * been replaced, and instead |iItem| is retrieved from the array.  * This does not change code behavior.  */ if(g_options[id][iItem] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * Since the code inside of your if statements does not  * differ (it always returns the same value), the |iItem == 0|  * check is actually redundant. This means that we can remove it  * and get rid of all the other if statements at the same time:  */ if(g_options[id][iItem]) {     return ITEM_DISABLED; }

A loop would unnecessarily complicate things. You need to do something based on a single value, and you have the means to directly derive the necessary information from that single value. You don't need to search for anything, you don't need to iterate. A lookup-table is perfect for this, and you already have it in place. You just need to use it properly.
__________________
In Flames we trust!
Nextra is offline
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:42.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode