[Solved] “for” loop added when I use minus and subtracted when I use plus


So the situation is a loop like this:

for (rows = columns + 1; rows < height; rows++) {
     // do something
}

I think you’re asking why the loop executes fewer times when the loop says for (rows = columns + 1;... than when it says for (rows = columns - 1;.... The reason is that the first part of a for loop is the initial condition; it’s where you give rows it’s starting value. If you add 1 to the starting value, and then count up to some final value (height in this case), then you’ve decreased the difference between the starting value and the final value.

Consider this:

height = 3;
for (rows = 1; rows < height; rows++) {...}

versus this:

height = 3;
for (rows = -1; rows < height; rows++) {...}

Which one of those loops do you suppose will execute more times?

1

solved “for” loop added when I use minus and subtracted when I use plus