C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Improving break & continue

From: Oliver Hunt <oliver_at_[hidden]>
Date: Mon, 25 Nov 2024 16:29:35 -0800
This seems ambiguous/confusing, take:

```cpp
while (..) {
   …
   if (x) {
      break while;
   } else {
     while (…) {
      …
      break while;
      ...
     }
}
```

The specification semantics are unambiguous (you go to the nearest appropriate control structure) but for a developer you can restructure code and your code behavior can change completely, and silently. For example let’s refactor the above:
```cpp
while (..) {
   …
   if (x) {
      break while;
   } else {
- while (…) {
+ for (…) {
      …
      break while;
      ...
     }
}
```

The target of the break silently changes with no real ability to produce even warning diagnostic.

I agree that improving break/continue is something that we should do, and seems like it should be very low cost (so I assume it has been proposed previously, and if so I don’t know the rationale for not doing so).

Most other modern languages just allow continue and break to take a label, such that

```cpp
foo: for (…) {
  bar: switch (…} {
    case X:
       wibble: while (…) {
          …
          If (a) break bar;
          If (b) continue wibble;
          If (c) break foo;

```

Which allows what you want, doesn’t have the hazards of targeting implicitly based on what syntactic construct is used, and makes it absolutely explicit what the intended target of the control statement is.

Again, given this is a feature that is present in numerous languages, I assume that this has been proposed for C++ in the past, and there were problems that made it non viable (though I am curious what those problems are).

—Oliver

> On Nov 25, 2024, at 3:23 PM, Filip via Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Hi everyone,
>
> This is my first time posting like this.
> I have an idea to improve the use of break and continue keywords in loops and switch statements.
>
> Examples:
> #1
> ``` c++
> while ( … ){
> for ( … ) {
> if ( … ){
> break while; // break the while loop, not the for loop
> // break for while; // identical in functioning to the above version
> }
> }
> }
> ```
>
> #2
> ``` c++
> for ( … ){
> for ( … ) {
> if ( … ){
> break for for; // break out of outer loop
> }
> }
> }
> ```
>
> #3
> ``` c++
> while ( … ){
> for ( … ) {
> switch ( … ){
> case 0 : break while;
> case 1 : continue while;
> case 2 : break switch; // explicit break of switch statement identical to break in this case
> case 3 : continue switch; // fall through without using comment or [[fallthrough]]
> default: return …;
> }
> }
> }
> ```
>
> You could replace break with continue in any place above, it would avoid making additional boolean variables,
> extracting some simple loops into functions and not use labels all goto like.
>
> You could be more specific with the amount of different for, while, do or switch statements, but they should match the closest breakable statement with the least amount of keywords.
>
> Empty or traditional break / continue should match the closest breakable statement.
>
> Thanks for reading.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals


Received on 2024-11-26 00:29:50