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 Statement
switch (i) { // switch Case 1: total = total + 17; break; Case 2: total = total + 29; break; Case 3: total = total + 99; break; } // switch
Array Implementation
int ary[4] {0, 17, 29, 99}; total = total + ary[i];