Skip to content

Files

Latest commit

 

History

History
35 lines (24 loc) · 633 Bytes

ForLoopShouldBeWhileLoop.md

File metadata and controls

35 lines (24 loc) · 633 Bytes

Pattern: Use of for instead of while loop

Issue: -

Description

A for loop without an init and update statement can be simplified to a while loop.

Example of violations:

int i = 0;
for(; i < 5;) {     // Violation
    println i++
}

// These are OK
for(i in [1,2])         // OK
   println i

for(int i = 0; i<5;)    // OK
    println i++

int i = 0;
for(; i < 5; i++)       // OK
    println i

for (Plan p : plans) {  // OK
    println "Plan=$p"
}

Further Reading