"If statement" continuously checking for multiples of for loop's index count

console, if-statement, java

Solution

Use `modulus(%)` operator: -

if (i % 5 == 0) {

}

- `5 % 5 == 0`, `10 % 5 == 0`, ...

Since you are using a `for loop`, you can simply change your increment from `i++` to `i += 5`, and leave the if condition.

for (int i = 0; i < someNum; i += 5) {
    // No need to check for `i` against `modulus 5`.
}

Problem

I have a for loop that checks for every 5th position. And at every 5th position, I'm performing an action like so (which works): ``` for(int i = 0; i < foo().length; i++) { System.out.print(i); if(i == 5 || i == 10 || i == 15) System.out.println(); } ``` Is there a way to write if statement so no matter how long `foo().length` is, I don't have to keep coming back to adjust it?

Original source