4

I am running into a situation where my program can have different outcomes depending on the state of some variables. 4 variables are involved and they can all have different ( 3 to 4 different states). All the possible combinations are leading me to about 48 different cases which would be resolved using a 4 level deep nested if/else structure. So, I have a couple of questions,

  1. Is there a better technique to draw logic or decision tables or some other structure so that you can model a decision tree for this many outcomes? When a decision is dependent on only 2 variables you can easily model this on a spreadsheet which is two dimensional by its nature but how do you deal with a case like the one I mentioned?

  2. Other than a 4 level deep nested if/else structure is there a better programming technique to deal with this?

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
mbashir
  • 143
  • 3

1 Answers1

4

I think what you're looking for is a 'decision table'

decision table example

it's tedious to fill it out, but it forces you to consider all possibilities - and you may see patterns in the variable values and/or outputs that will let you write more compact if-logic

Steven A. Lowe
  • 33,808
  • 2
  • 84
  • 151
  • Just to extend, [Karnaugh maps](https://en.wikipedia.org/wiki/Karnaugh_map) are a more complex variation on decision tables but can help you define the minimal logical evaluations necessary to achieve the branching logic. – Flater May 13 '20 at 08:59
  • @Flater thanks for the reference though Karnaugh maps only work with boolean variables – Steven A. Lowe Jun 18 '20 at 19:58
  • Any value can be broken down into binary values (OP has 3-4 values per variable, which can be expressed using 2 bits), but I do agree that for a large range of values it becomes increasingly complex. – Flater Jun 19 '20 at 08:56