Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI guys,

I'm doing an application that users can use certain information from combo boxes, checkboxes etc

The problem is that I need to give them some client details depends on the data they chose.


I have an if statement with 5 conditions example:

if(a > 0, B >0, c>0 ,d>0, e>0)

now these can be combined so I need to do a lot of if statement ( factorial of 5 (120 if statements))


, someone know a way how can i reduce the complexity and repetition of this if statement please.



CODE EXAMPLE:


/if (this.Mcc.Length > 1 && this.CheckboxIsTicked == false && this.TestCases > 0 && THIS.TESTCASE1 > 0 && THIS.TESTCASE2 > 0)
                    {
                        
                            sql = "SELECT * FROM  TABLE1;
                       

                    }ELSE IF(<pre>this.Mcc.Length > 1 && this.CheckboxIsTicked == false && this.TestCases > 0 && THIS.TESTCASE1 > 0 && THIS.TESTCASE2 < 0
)
{
sql = "SELECT * FROM TABLE2;

}


AND MANY MANY MORE

What I have tried:

i tried to make it simpler but i cannot. Some suggestions pleasE?
Posted
Updated 9-Feb-18 9:49am
Comments
Patrice T 9-Feb-18 7:17am    
Not clear what you try to do, this code is not C.
Joe Doe234 9-Feb-18 8:05am    
c# sorry but i cannot find where i can edit my question :/... originalgriff understand it ...
Patrice T 9-Feb-18 8:30am    
Use Improve question to update your question.

Have a look at Decision table - Wikipedia[^].
 
Share this answer
 
Comments
Ziee-M 9-Feb-18 8:41am    
Learned somthin new today, thx
CPallini 9-Feb-18 9:01am    
You are welcome.
Maciej Los 9-Feb-18 14:50pm    
5ed!
CPallini 9-Feb-18 15:38pm    
Too good, Sir!
Thank you.
Maciej Los 9-Feb-18 15:39pm    
;)
if you want just strings, then one way to do it is to set up and array, and reduce each if condition to a single bit in an integer.
C#
int index = 0;
if (a > 0) index |= 1;
if (b > 0) index |= 2;
if (c > 0) index |= 4;

You can then set up an array of strings that relates to the index value:
C#
string[] selects = new string[] { "Select * FROM none",
                                  "Select * FROM a only",
                                  "Select * FROM b only",
                                  "Select * FROM a and b",
                                  "Select * FROM c only",
                                  "Select * FROM a and c",
                                  "Select * FROM b and c",
                                  "Select * FROM a, b, and c"};
And select your string directly.
 
Share this answer
 
v2
Comments
Joe Doe234 9-Feb-18 7:44am    
then I can call select[index] , right ?
OriginalGriff 9-Feb-18 7:54am    
Exactly that!
Joe Doe234 9-Feb-18 8:04am    
but the problem is that i am going to have the same amount of if statements, there is a way that i can reduce them please ?
OriginalGriff 9-Feb-18 8:21am    
No, the whole idea is you don't. In the example I gave, you have three if statements, and that chooses from eight strings (basic binary going on here). You said you have five condition which are combined: that's what the binary OR into the index value does: if a and b are true, index holds three (1 | 2 == 3)
You have five conditions which gives you 32 options, but only five if statements.
Joe Doe234 9-Feb-18 10:12am    
wow, that seems interesting. I will go into more detail about this. thank you :)
What you are looking at is building a decision table.
 
Share this answer
 
Quote:
now these can be combined so I need to do a lot of if statement ( factorial of 5 (120 if statements))

No you have 5 binary conditions, it makes 2^5= 32 possibilities.

You need to learn that C is case sensitive, this mean that:
'this' is correct, 'THIS' is not.
'if' is correct, 'IF' is not.
'else' is correct, 'ELSE' is not.
C++
sql = "SELECT * FROM  TABLE1"; // is correct
sql = "SELECT * FROM  TABLE1; // is not


C++
if (THIS.TESTCASE2 > 0) {...}
else if (THIS.TESTCASE2 < 0) {...}

This code misses the case (THIS.TESTCASE2 == 0) which is not handled.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900