Multiple form combinations to many for if/else

I have a form with two inputs. The input is a select from 1-10. I need to take all the combinations from the fields and return a single result.

If inputA is 1 and inputB is 3 then 5
If inputA is 5 and inputB is 7 then 9

There are too many combinations for if/else or is that the best way?

Thanks for the help!

SWITCH / CASE would be the way I do it.

HTH,

:slight_smile:

Yes, there is still likely to be a lot of ā€œcaseā€ but it will be less code and easier to read and maintain.

1 Like

If i use if statements there will be at least 100 of them. If i use switch/case it doesnā€™t seem as straight forward. How would i check to see if this number or that number?

Pseudo code:

switch(inputA){
case 1:
    switch(inputB){
    case 1:
        //code to process
    break;
    case 2:
        //code to process
    break;
    ...
case 2:
    switch(inputB){
    case 1:
        //code to process
    break;
    case 2:
        //code to process
    break;
    ...
case 3:
    switch(inputB){
    case 1:
        //code to process
    break;
    case 2:
        //code to process
    break;
    ...
...
}

HTH,

:slight_smile:

PSā€¦ or, if the inputs are both 1-10, you could make an array, and use the inputs of each as the indexes. (ie, thisArray[inputA][inputB] - if A=3 and B=5, then the value of thisArray[3][5].

In the ā€œcode to processā€ will there be a if type of statement?

No. The ā€˜code to processā€™ is what you indicated in your original post. ā€œIf inputA is 1 and inputB is 3 then 5ā€ The ā€œthen 5ā€ would be the code that goes in //code to process.

:slight_smile: (this feels like homework)

I understand, hate to put homework on ya. I would have about the same number of lines as i would if i did 100 if/then.

What rules govern the return?

I doubt whoever gave you this problem listed out all 100 possible combinations in a chart, so there must be some programmatic/mathematical principle behind it.

Given the two examples you gave, it would be Result = B+(B-A). But thatā€™s only 2 examples out of 100.

This is what I was wondering too.

Scott

This is a little program that will assess the input of a form.

If the water is 10 degrees and the air is 30 degrees then it will pull the result out of MySQL by ID.

There are ten for inputA and ten for inputB, so 100 possible combinations. I could write out 100 if/then statements but i know there has to be a better way. I just canā€™t get my head around the solution.

And what is the result out of Mysql going to be?

Scott

I am not sure why that matters, but it will be a specific product recommendation. Thanks for the help so far everyone.

That helps to understand the overall problem.

How about just two input fields?

Water Temperature:
Air Temperature:

Then calculate the value you need to find your product recommendation. What would you query for in the product database to find the recommended products?

Scott

What weā€™re trying to get at is: consolidate the number of cases you have to deal with.

If there is absolutely no pattern to the value of the result and all of the values are different, then yes, youā€™ll either have to do 100 if/else clauses, 100 case switches, or a 10x10 multidimensional array.

Ok, I got it. There is no pattern to the value so I will have to create 100 different outcomes. Thanks for the help!

I personally would do the multidimensional array, simply for readability/space consumption of code, then. But thatā€™s me.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.