MISRA C++:2023 Rule 9.4.1
All if ... else if constructs shall be terminated with an else statement
Since R2024b
Description
Rule Definition
All if ... else if constructs shall be terminated with an else statement. 1
Rationale
Ending an if... else if statement with an else
statement is defensive programming. This final else statement acts as a
fail-safe in case a unique situation occurs where the code progresses past the
if and else if statements.
When an if statement is followed by one or more else
if statements, follow the final else if statement with an
else statement. Within the else statement provide an
action. If no action is needed, provide a comment as to why no action is taken.
Polyspace Implementation
Polyspace® raises this defect whenever an if … else if construct does
not end with an else statement.
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 R2024b
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.