A for loop is a type of loop construct that allows a block of code to be executed repeatedly for a fixed number of times. The
for loop is typically used when the number of iterations is known in advance and consists of three parts:
true, the code inside the loop is executed.
for (initialization; termination; increment) { /*...*/ }
All three statements are optional. However, when the initialization and update statements are not used, it can be unclear to the reader what the loop counter is and how it is being updated. This can make the code harder to understand and maintain.
for (;condition;) { /*...*/ } // Noncompliant; only the condition is specified
When only the condition expression is defined in a for loop, a while loop should be used instead to increase readability.
A while loop consists of a single loop condition and allows a block of code to be executed repeatedly as long as the specified condition
is true.
while (condition) { /*...*/ }