Y U NO NEED BREAK?

Sorry for the disturbing title, but I got a problem. Why doesnt the IF statement need a break;? The SWITCH statement needs one... Please help
switch() needing break; is a design flaw.
if() doesn't need it because if you have multiple statements, you just enclose them into curly braces.
Also break; can be used in for(), while() and do-while() in order to prematurely end them.
What is the design flaw with switch ?
Last edited on
What is the design flaw with switch ?


I read somewhere that it should have been designed to break automatically at every next case label and requiring some fallthrough keyword in the case that's what a programmer wants. How often do you not break; your cases?
for example (pseudo code):
1
2
3
4
5
6
7
8
9
10
11
12
permission = GetNextFilePermission(filename);
switch(permission) {
    case 'X':
        cout << "executable file" << endl;
        break;
    case 'R':
    case 'W':
        cout << "normal file" << endl;
        break;
    default:
        cout << "unknown file" << endl;
}

READ and WRITE permissions identifies a normal file that you can't execute.
Last edited on
1
2
3
4
5
6
7
8
permission = GetNextFilePermission(filename);
switch(permission) {
    case 'X': cout << "executable file" << endl;
    case 'R':
        continue;
    case 'W': cout << "normal file" << endl;
    default: cout << "unknown file" << endl;
}


Cleaner, no?
@catfish - I would agree with that - but I suppose the current way
is easier to do in machine code
Last edited on
@ Catfish:

I don't fully agree, as it's quite common for me to have 6-10 cases that all share the same code:

1
2
case x: case y: case z: case a: case b: case c:
  // ...stuff 


vs.
1
2
3
4
5
6
7
case x: continue;
case y: continue;
case z: continue;
case a: continue; 
case b: continue; 
case c:
  // ...stuff 



That said... it might be better to require the 'continue' only if there's a sequence point between case statements:

1
2
3
4
5
6
case a:
  dosomething();  // semicolon here
  continue;  // <- so we need a continue
case b:
  // <- nothing here
case c:  // <- so no need to continue 


But then that makes the rules more inconsistent and error prone.


On the other hand I agree that switch automatically falling through is not ideal. I just don't know what the best solution is, because all of them are kind of junky.
closed account (1vRz3TCk)
You would have to use something other than continue, that would be far to confusing...on the one hand cause control to pass to the end of a loop but on the other you would just move on to the next statement.
This is all academic anyway, as there's no way to make this change in C++ without destroying tons of existing code.
1
2
3
4
5
6
7
case x: continue;
case y: continue;
case z: continue;
case a: continue; 
case b: continue; 
case c:
  // ...stuff  


That indeed looks unwieldy. But the solution could be:
1
2
3
4
5
6
7
8
case x, y, z, a, b, c:
    extra_stuff();
    continue;
case d:
    plain_stuff_d();
case e:
    plain_stuff_e();
// etc 
Topic archived. No new replies allowed.