How to make for loops in Java increase by increments other than 1

TomLisankie picture TomLisankie · Jan 3, 2011 · Viewed 257.8k times · Source

If you have a for loop like this:

for(j = 0; j<=90; j++){}

It works fine. But when you have a for loop like this:

for(j = 0; j<=90; j+3){}

it doesn't work. Could someone please explain this to me?

Answer

user557219 picture user557219 · Jan 3, 2011

That’s because j+3 doesn’t change the value of j. You need to replace that with j = j + 3 or j += 3 so that the value of j is increased by 3:

for (j = 0; j <= 90; j += 3) { }