Skip to main content

IAR Embedded Workbench for RL78 5.20

MISRAC2004-14.6

In this section:
Synopsis

(Required) For any iteration statement, there shall be at most one break statement used for loop termination.

Enabled by default

Yes

Severity/Certainty

Low/Medium

lowmedium.png
Full description

Multiple termination points were found in a loop.

Coding standards
MISRA C:2004 14.6

(Required) For any iteration statement, there shall be at most one break statement used for loop termination.

Code examples

The following code example fails the check and will give a warning:

int test1(int);
int test2(int);

void example(void)
{
	int i = 0;
	for (i = 0; i < 10; i++) {
		if (test1(i)) {
			break;
		} else if (test2(i)) {
			break;
		}
	}
}
void func()
{
  int x = 1;
  for ( int i = 0; i < 10; i++ )
  {
    if ( x )
    {
      break;
    }
    else if ( i )
    {
      break;  // Non-compliant – second jump from loop
    }
    else
    {
      // Code
    }
  }
}

The following code example passes the check and will not give a warning about this issue:

void example(void)
{
	int i = 0;
	for (i = 0; i < 10 && i != 9; i++) {
		if (i == 9) {
			break;
		}
	}
}
void func()
{
  int x = 1;
  for ( int i = 0; i < 10; i++ )
  {
    if ( x )
    {
      break;
    }
    else if ( i )
    {
      while ( true )
      {
        if ( x )
        {
          break;
        }
        do
        {
          break;
        }
        while(true);
      }
    }
    else
    {
    }
  }
}