C++ Logo

std-proposals

Advanced search

[std-proposals] Improving break & continue

From: Filip <fph2137_at_[hidden]>
Date: Tue, 26 Nov 2024 00:23:47 +0100
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.

Received on 2024-11-25 23:24:00