Continuum of a Program
If you look a program, you will see data and code -- logical. Have you ever thought about their interelationships?
The choice of data structure predestines some of the code. The choice of some code predestines some data structures. In fact, the two could be perceived as opposite ends of a continuum. When you design/implement a program, you choose what to do. Consider that choice a slider on a scale. It can be heavy data structure and lighter code or the other way around.
Programs can be optimized for speed or for data usage (usually these goals are diametrically opposed).
Here are 3 different implementations that produce the same result:
Nested If Statements
if (1 == i) { total = total + 17; } elseif (2 == i) { total = total + 29; } elseif (3 == i) { total = total + 99; }
Case Stratement
switch (i) { // switch Case 1: total = total + 17; break; Case 2: total = total + 29; break; Case 3: total = total + 99; break; } // switch
Note that if you look at the assmbly code generated by the nested IF and the CASE structure, you will see that the code is almost identical. In other words, neither method has a significant runtime advantage. Of course style can be argued.
Array Implementation
int ary[4] {0, 17, 29, 99}; total = total + ary[i];