I'm trying to iterate through a loop (any programming language, really, but for this exercise I happen to be using c#) and I was just wondering... what happens when you try to use a loop that doesn't iterate at all (i.e. ...."null"?)
For example:
int x = choose(0,1,2);
for(int i=0;i<x;i++) {
//some stuff
}
Like, what would happen if x gets chosen to be 0
? Does it just become a useless for loop on that case? Will my program crash? Is that bad programming practice? etc. etc.
I'm mainly asking because I'm trying to format a concatenated string but only if some array has enough elements. thanks
Well simply put, nothing will happen. A for
loop is like an if
statement where it checks the condition and repeats while that condition is true.
for(int i=0;i<x;i++)
This is saying:
i
to 0i
is less than the value of x
i
at the end of the loopIf x
is 0, then the loop will simply not run; it becomes useless.