Structure While Statement -- Form I
You have a do-while statement preceded by an unconditional jump to a label immediately before the statement condition. Make the do-while statement a while statement.
goto test; do { /* loop */ ... test: } while(cond);
⇓
while(cond) { /* loop */ ... }
Motivation
While statements are translated by compilers into one of the three following possible forms: the condition succeeds the loop body, precedes it, or both. This refactoring recovers the original control structures when the former form is used. It requires the Structure Do While Statement refactoring to be applied first.
Mechanics
- Remove the jump.
Change the do-while statement into a while statement.
- Remove the label, if no longer used.
Structure While Statement -- Form II
- The loop block of an infinite loop starts with a conditional break.
Make the infinite loop a regular while statement.
while(1) { if(cond) break; /* rest */ ... }
⇓
while(!cond) { /* rest */ ... }
Motivation
As previously mentioned, While statements are translated by compilers into one of several possible forms. This refactoring recovers the original control structures where the condition precedes the loop body. It requires that the Structure Infinite Loop and Structure Break Statement refactorings are applied first.
Mechanics
Move the condition of the conditional jump into the while statement.
Structure While Statement -- Form III
A do-while loop is nested inside an if statement with the same condition. Make the loop a regular while statement.
if(cond) { do { /* loop */ ... } while(cond); }
⇓
while(cond) { /* loop */ ... }
Motivation
As previously mentioned, While statements are translated by compilers into one of several possible forms. This refactoring recovers the original control structures where the condition both precedes and succeeds the loop body. It requires that the Structure If Statement and Structure Do-While Statement refactorings are applied first.
Mechanics
Remove the if statement.
Change the do-while statement into a while statement.