== Why is this an issue?

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:

* The initialization statement is executed once at the beginning of the loop. It is used to initialize the loop counter or any other variables that may be used in the loop.
* The loop condition is evaluated at the beginning of each iteration, and if it is `true`, the code inside the loop is executed.
* The update statement is executed at the end of each iteration and is used to update the loop counter or any other variables that may be used in the loop.

[source,java]
----
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.

== How to fix it

=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
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.

==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
while (condition) { /*...*/ }
----

== Resources

* https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html[Java SE - The for Statement]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
