I'm trying to create a function, or add something to the body of my main() that, whenever I pass an input integer, returns me a corresponding string, example:
1 2 3 4 5 6 7
string fn(int i)
{
if (i == 1) return"one";
if (i == 3) return"three";
if (i == 9) return"nine";
return"none";
}
Now does this take linear time in executing? i.e. does the compilere pass by the cases case by case or there's something special about if clauses?
Is switch statement the same?
If in both cases, linear time is inevitable, is there something better?
The time is constant (it could be considered linear, but integer comparison is such a trivial task that you wouldn't observe any linearity at all).
A switch would be the same.
Another way to do the same thing could be