Skip to main content
2 of 5
added 105 characters in body; edited title
Joop
  • 227
  • 2
  • 6

While loop with header changing the variables with empty body or are other ways preferred over this?

The reason I am asking this is because I am doing some analyses on my code by the build in code analysis of my IDE and I get the remark

while statement has empty body at line

Which seems to indicate that having a loop with no lines in them is not a preferred way to do something. I got this code, in which what I do a conditional check which also changes my code, and it works:

    while (lastSucceedingIndex < tailIndex && (array[++lastSucceedingIndex] == key)) {
    }
    lastSucceedingIndex--;

Information about what the loop does and why I decided to use it like this: I am using shortcircuit to quit the loop if the first part of the conditional check does not evaluate to true. The second part increases first the lastSucceedingIndex and then checks if it matches some key, more often than not this is the case. Since I increase lastSucceedingIndexwithin the header I leave the while loop empty. To later correct for the off by one bug I add the last line after the while loop.

Now my question is, is having an empty loop good practice? Or should I move the lastSucceedingIndex to the body and increment it there? Or is there even a third option I am missing?

Joop
  • 227
  • 2
  • 6