I can’t decide how I should loop over ranges. This way:
for (int i = 0; i < max_i; i++) {
for (int j = 0; j < max_j; j++) {
// first way - two loops
}
}
Or this way:
for (int k = 0; k < max_i*max_j; k++) {
// second way - one loop
}
Thanks, Boda Cydo.
It depends on what you will be doing with the indices. If you’re using two values separately (for example you’re iterating over all permutations of the values in the two loops) then the two loop solution is more clear. The one-loop strategy is better if you don’t care about the individual values but just their product instead.
Either way, choose the strategy which expresses your intent more clearly. The performance implications are trivial in comparison to the importance of the simplicity of maintaining the code.