The use of break
and continue
statements increases the complexity of the control flow and makes it harder to understand
the program logic. In order to keep a good program structure, they should not be applied more than once per loop.
This rule reports an issue when there is more than one break
or continue
statement in a loop. The code should be
refactored to increase readability if there is more than one.
Noncompliant code example
for (int i = 1; i <= 10; i++) { // Noncompliant; two "continue" statements
if (i % 2 == 0) {
continue;
}
if (i % 3 == 0) {
continue;
}
// ...
}
Compliant solution
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0 || i % 3 == 0) {
continue;
}
// ...
}