Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.IF.NO_ELSE

MISRA C 2012 Rule 15.7: All if ... else if constructs shall be terminated with an else statement

A chain of if/else-if statements is not terminated with else or is terminated with an empty else clause.

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

A final else statement shall always be provided whenever an if statement is followed by a sequence of one or more else if constructs. The else statement shall contain at least either one side effect or a comment.

Rationale

Terminating a sequence of if ... else if constructs with an else statement is defensive programming and complements the requirement for a default clause in a switch statement (see Rule 16.5).

The else statement is required to have a side effect or a comment to ensure that a positive indication is given of the desired behaviour, aiding the code review process.

Note: a final else statement is not required for a simple if statement.

Example

The following example is non-compliant as there is no explicit indication that no action is to be taken by the terminating else.

if ( flag_1 ) 
{
  action_1 ( ); 
} 
else if ( flag_2 )
{
  action_2 ( ); 
}

/* Non-compliant */

The following shows a compliant terminating else.

else 
{
  ;     /* No action required - ; is optional */ 
}

See also

Rule 16.5

MISRA-C 2004 Rule 14.10 (required): All if "¦ else if constructs shall be terminated with an else clause.

This rule applies whenever an if statement is followed by one or more else if statements; the final else if shall be followed by an else statement. In the case of a simple if statement then the else statement need not be included.

The requirement for a final else statement is defensive programming. The else statement shall either take appropriate action or contain a suitable comment as to why no action is taken. This is consistent with the requirement to have a final default clause in a switch statement (15.3).

For example this code is a simple if statement:

if ( x < 0 )
{
   log_error(3);
   x = 0;
}                /* else not needed */

whereas the following code demonstrates an if, else if construct

if ( x < 0 )
{
   log_error(3);
   x = 0;
}
else if ( y < 0 )
{
   x = 3;
}
else                 /* this else clause is required, even if the     */
{                    /* programmer expects this will never be reached */
   /* no change in value of x */
}

MISRA-C++ 2008 Rule 6-4-2 (required): All if "¦ else if constructs shall be terminated with an else clause.

Rationale

When an if statement is followed by one or more else if statements then the final else if shall be followed by an else statement. In the case of a simple if statement the else statement need not be included.

The final else statement, which should either take appropriate action or contain a suitable comment as to why no action is taken, is defensive programming.

Example

For example this code is a simple if statement:

if ( x < 0 )
{
   log_error( 3 );
   x = 0;
}
// else not needed

Whereas the following code demonstrates an if, else if construct

if ( x < 0 )
{
   log_error ( 3 );
   x = 0;
}
else if ( y < 0 )
{
   x = 3;
}
else   // this else clause is required, even if the
{      // developer expects this will never be reached
   // No change in value of x
}