If the body of a loop can only execute at most once, then the whole loop is equivalent to an if conditional. This is confusing for readers, and may even indicate a logic error.

Replace the loop with a conditional.

In the following example, the function nextToken reads a value c from a reader, and then enters a while loop if c is not falsy. Inside the loop body, it performs a switch on c, taking different actions depending on its value. After the switch, it then unconditionally breaks out of the loop.

In other words, the loop body either does not execute at all (if c is falsy), or exactly once (if it is truthy), but never more than once.

Assuming that this is the desired behavior, the while loop should be replaced with an if statement as follows:

  • Mozilla Developer Network: Loops and iteration.