Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3218. Cycle condition may be incorrect…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V3218. Cycle condition may be incorrect due to an off-by-one error.

Apr 01 2025

The analyzer has detected that an incorrect maximum or minimum value of the collection index is used in the loop. The index is greater or lesser than the intended value by one. This is usually due to an error in the loop condition.

Take a look at an example with the for loop:

void PrintArray(int[] numbers)
{
  for (int i = 0; i <= numbers.Length; i++) 
  {
    Console.WriteLine(numbers[i]);
  }
}

The i <= numbers.Length loop condition includes the index range from 0 to numbers.Length. The first index is 0, and the index of the last element is numbers.Length - 1. As a result, numbers.Length exceeds the last valid index by one, leading to an off-by-one error.

The fixed code looks like this:

void PrintArray(int[] numbers)
{
  for (int i = 0; i < numbers.Length; i++) 
  {
    Console.WriteLine(numbers[i]);
  }
}

Let's look at an example of iterating over the array in reverse order:

void PrintArray(int[] numbers)
{
  for (int i = numbers.Length - 1; i > 0; i--) 
  {
    Console.WriteLine(numbers[i]);
  }
}

Since the i > 0 condition restricts the index to 1, the first array element is not printed to the console.

In this case, the fixed code looks like this:

void PrintArray(int[] numbers)
{
  for (int i = numbers.Length - 1; i >= 0; i--) 
  {
    Console.WriteLine(numbers[i]);
  }
}

Now the loop condition is correct, and all array elements will be printed to the console.

The diagnostic rule also covers the while and do while loops.

Take a look at an example with the while loop:

void PrintArray(List<int> numbersList)
{
  int i = 0;
  while (i <= numbersList.Count)
  {
    Console.WriteLine(numbersList[i]);
    i++;
  }
}

The loop condition includes an index value equal to the collection size, which results in a collection overrun.

The fixed code:

void PrintArray(List<int> numbersList)
{
  int i = 0;
  while (i < numbersList.Count)
  {
    Console.WriteLine(numbersList[i]);
    i++;
  }
}

After the fix, the collection will be fully iterated, and no exception will occur.

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam