14.10 Hiding an else if construct

6.14 Control Flow

Moderators: misra-c, david ward

Locked
brian_bassil
Posts: 1
Joined: Thu Aug 07, 2008 3:08 pm
Company: Cobham

14.10 Hiding an else if construct

Post by brian_bassil » Wed Sep 10, 2008 1:13 pm

The following

Code: Select all

if ( x < 0 )
{
    log_error(3);
    x = 0;
}
else if ( y < 0 )
{
   x = 3;
}
else
{
/* no change */
}
Can be coded as

Code: Select all

if ( x < 0 )
{
    log_error(3);
    x = 0;
}
else 
{
   if ( y < 0 )
   {
      x = 3;
   }
}
to hide the "else if". Is this OK?

I think the code blocks are semantically identical but the last one does not list the else.

Brian

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Re: 14.10 Hiding an else if construct

Post by misra-c » Fri Sep 12, 2008 12:41 pm

Your implementation makes it clear to a reviewer that there are only two paths associated with the test of x and is acceptable. The intent of Rule 14.10 is to catch instances where the final else of an if ... else if ... chain has been accidently omitted.
---
Posted by and on behalf of
the MISRA C Working Group

Locked

Return to “6.14 Control Flow”