45 lines
797 B
Plaintext
45 lines
797 B
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
== How to fix it
|
|
|
|
It's generally recommended to only update the loop counter in the loop declaration. If skipping elements or iterating at a different pace based on a condition is needed, consider using a while loop or a different structure that better fits the needs.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
for (int i = 1; i <= 5; i++)
|
|
{
|
|
Console.WriteLine(i);
|
|
if (condition)
|
|
{
|
|
i = 20;
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
int i = 1;
|
|
while (i <= 5)
|
|
{
|
|
Console.WriteLine(i);
|
|
if (condition)
|
|
{
|
|
i = 20;
|
|
}
|
|
else
|
|
{
|
|
i++;
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../rspecator.adoc[]
|