== Why is this an issue?

include::../description.adoc[]

== How to fix it

If your https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement:~:text=The%20condition%20section%20that%20determines%20if%20the%20next%20iteration%20in%20the%20loop%20should%20be%20executed[stop condition] indicates a **maximum** value, the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement:~:text=The%20iterator%20section%20that%20defines%20what%20happens%20after%20each%20execution%20of%20the%20body%20of%20the%20loop[iterator] should **increase** towards it. Conversely, if your stop condition indicates a **minimum** value, the iterator should **decrease** towards it.

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
for (int i = 0; i < maximum; i--)  // Noncompliant: runs until it underflows to int.MaxValue
{
    // ...
}

for (int i = maximum; i >= maximum; i++)  // Noncompliant: runs until it overflows to int.MinValue
{
    // ...
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
for (int i = 0; i < maximum; i++) // Compliant: Increment towards the maximum value
{
}

for (int i = maximum; i >= 0; i--) // Compliant: Decrement towards the minimum value
{
    // ...
}
----

== Resources

=== Documentation

* https://en.wikipedia.org/wiki/Integer_overflow[Integer overflow]
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements#the-for-statement[The `for` statement]

include::../rspecator.adoc[]
