Loops with at most one iteration are equivalent to an if statement. Using loops in
this case makes the code less readable.
If the intention was to execute the loop once, an if statement may be used or the
loop removed.
Otherwise, the jumping statement should be made conditional so the loop can execute more than
once.
Loops with at most one iteration can happen with a statement that unconditionally transfers control is misplaced inside the body of the loop.
These statements are:
ExitBreakHaltraisegotoMake the statement that affects execution of the loop conditional, or remove it all together.
var I := 0; while I < 10 do begin Inc(I); Break; // Noncompliant end;
for var I := 0 to 10 do begin
if I = 2 then
Break // Noncompliant
else begin
Writeln(I);
Exit; // Noncompliant
end;
end;
var I := 0; while I < 10 do begin Inc(I); end;
for var I := 0 to 10 do begin
if I = 2 then
Break
else begin
Writeln(I);
end;
end;