MISRAC++2008-6-4-2
In this section:
Synopsis
(Required) All if ... else if constructs shall be terminated with an else clause.
Enabled by default
Yes
Severity/Certainty
Low/High

Full description
If ... else if constructs that are not terminated with an else clause were detected. This check is identical to MISRAC2004-14.10, MISRAC2012-Rule-15.7, MISRAC++2023-9.4.1.
Coding standards
- MISRA C:2004 14.10
(Required) All if ... else if constructs shall be terminated with an else clause.
- MISRA C:2012 Rule-15.7
(Required) All if ... else if constructs shall be terminated with an else statement
- MISRA C++ 2023 9.4.1
(Required) All if... elseif constructs shall be terminated with an else statement
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
#include <stdio.h>
void example(void) {
if (!rand()) {
printf("The first random number is 0");
} else if (!rand()) {
printf("The second random number is 0");
}
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
#include <stdio.h>
void example(void) {
if (!rand()) {
printf("The first random number is 0");
} else if (!rand()) {
printf("The second random number is 0");
} else {
printf("Neither random number was 0");
}
}