Y U NO NEED BREAK?

May 16, 2012 at 12:17pm
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
May 16, 2012 at 12:25pm
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.
May 16, 2012 at 12:47pm
What is the design flaw with switch ?
Last edited on May 16, 2012 at 12:50pm
May 16, 2012 at 1:02pm
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?
May 16, 2012 at 1:10pm
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 May 16, 2012 at 1:12pm
May 16, 2012 at 1:18pm
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?
May 16, 2012 at 2:36pm
@catfish - I would agree with that - but I suppose the current way
is easier to do in machine code
Last edited on May 16, 2012 at 2:42pm
May 16, 2012 at 2:54pm
@ 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.
May 16, 2012 at 3:03pm
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.
May 16, 2012 at 4:43pm
This is all academic anyway, as there's no way to make this change in C++ without destroying tons of existing code.
May 16, 2012 at 5:16pm
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.