[previous] MISRA C:2012  Rule-15.4:  (Advisory) [next] There should be no more than one break or goto statement used to terminate any iteration statement

Example Code:


#include "misra.h"
#include "m3cmex.h"


extern int16_t rule_1504( void )
{
   int16_t m;
   int16_t n;

   for ( n = 0; n < 5; n++ )
   {
      if ( n > s16a )
      {
         break;
      }

      switch (s16a)
      {
      case 1:
         ++s16b;
         break;
      case 2:
         if (s16b > s16a)
         {
            break;
         }
         ++s16b;
         break;
      default:
         break;
      }

      if ( n > (s16a - 2) )
      {
         break;                                                 /* 0771 */
      }

   }

   /* Both of the following nested loops are compliant
      as each has a single break used for early
      loop termination. */

   for ( m = 0; m < 10; ++m )
   {
      if ( get_bool() )
      {
         break;
      }

      for ( n = 0; n < m; ++n )
      {
         if ( get_bool() )
         {
            break;
         }
      }
   }

   /* The following loop is non-compliant as there are
      multiple break and goto statements used for
      early loop termination. */

   for ( m = 0; m < 10; ++m )
   {
      if ( get_bool() )
      {
         break;
      }
      else if ( get_bool() )
      {
         goto EXIT;                                             /* 2001 */      /* Non-compliant */
      }
      else
      {
         s16b = get_s16();
      }
   }


   /* In the following example, the inner while loop
      is compliant because there is a single goto
      statement that can cause its early termination.
      However, the outer while loop is non-compliant
      because it can be terminated early either by the
      break statement or by the goto statement in
      the inner while loop. */

   while (m != 0)
   {
      m = get_s16();

      if (m == 1)
      {
         break;
      }

      while (n != 0)
      {
         n = get_s16();
         if (n == 1)
         {
            goto EXIT;                                          /* 2001 */      /* Non-compliant */
         }
      }
   }

   EXIT:
   return s16b;
}


QAC messages that encompass this guideline:

0771 More than one 'break' statement has been used to terminate this iteration statement.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents