Rationale for no goto, continue but allow break

6.14 Control Flow

Moderators: misra-c, david ward

Locked
niall
Posts: 1
Joined: Tue Sep 13, 2005 2:44 pm
Location: Wiltshire

Rationale for no goto, continue but allow break

Post by niall » Wed Sep 14, 2005 3:55 pm

I know this should have been brought up before, but in reviewing MISRA-2 this passed me by.
Being pedantic, why has the rule on break (14.6) been relaxed and not 14.5 (or 14.4)?
A break in a loop is just a goto!
I have no problem with it, it just smacks of inconsistency.
Niall Cooling CEng
Feabhas Limited
http://www.feabhas.com

Gavin McCall
Posts: 72
Joined: Mon Sep 13, 2004 9:24 am

Post by Gavin McCall » Thu Nov 03, 2005 2:26 pm

MISRA C Steering Team - 2/11/2005

In writing MISRA-C:2004, the use of a loop with two exits was considered acceptable.

One is available for normal exit, and the second for exceptional exits, using the \"break\" statement.

This jump is well constrained unlike \"goto\" which can allow jumps anywhere.
Gavin McCall
Personal view only.

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

Post by misra-c » Tue Aug 29, 2006 4:48 pm

MISRA C Steering Team - 2/11/2005

In writing MISRA-C:2004, the use of a loop with two exits was considered acceptable.

One is available for normal exit, and the second for exceptional exits, using the \"break\" statement.

This jump is well constrained unlike \"goto\" which can allow jumps anywhere.
Re-posted under offical user name only.
---
Posted by and on behalf of
the MISRA C Working Group

sparker
Posts: 13
Joined: Thu Apr 28, 2005 2:05 pm
Location: Cambrige, UK

Re: Rationale for no goto, continue but allow break

Post by sparker » Wed Aug 30, 2006 9:34 am

niall wrote:I know this should have been brought up before, but in reviewing MISRA-2 this passed me by.
Being pedantic, why has the rule on break (14.6) been relaxed and not 14.5 (or 14.4)?
A break in a loop is just a goto!
I have no problem with it, it just smacks of inconsistency.
I seem to recall griping about this at the first MISRA-C forum thing (many years ago). I thought that disallowing break within a loop was extreme and that the alternatives (largely loop control variables) are ugly and error prone; others (Peter Amey I suspect) pointed out that it was a stronger restriction than that in SPARK Ada (which allows, if I remember correctly, a break from a top level conditional within a loop). This may have influenced the decision.

As an aside, we could, of course, rewrite all control flow in terms of if/goto (compilers do it all the time) -- there isn't anything special about break. (See http://david.tribble.com/text/goto.html for interesting discussion on modern goto thinking.)

Locked

Return to “6.14 Control Flow”