MISRA C++:2008 Rule 6-5-4
The loop-counter shall be modified by one of: --, ++, -=n, or +=n ; where n remains constant for the duration of the loop
Description
Rule Definition
The loop-counter shall be modified by one of: --, ++, -=n, or +=n ; where n remains constant for the duration of the loop.1
Rationale
If the loop-counter modifier of the loop is not constant, the loop might end unpredictably, causing results contrary to your expectations. Constant increments create predictable loop termination.
Polyspace Implementation
Polyspace® reports this defect whenever the modifier of a loop-counter is not constant. Polyspace also reports this defect when you modify a loop by -=n or +=n and modify n within the loop.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Statements |
Category: Required |
Version History
Introduced in R2013b
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.