Funny C++

Well not really sure if this is funny or not but I was very surprised to learn something in school today. We were talking about switch statements and working on some program when the teacher told us that when you don't write any statements for a case, the code from the next case is inherited.

This is the piece of code we were working with in school:
1
2
3
4
5
6
7
8
9
10
switch (reply)
{
  case 'n': ;
  case 'N':
           fin = true;
           break;
  case 'y': ;
  case 'Y':
           fin = false;
}


So for example if reply was to equal "n", the case statements for N would be used and fin would equal true.

I don't know, maybe its just me, but this kind of logic and construction doesn't really seem right to me. I like how C++ is strict and this prevents errors but I don't like how C++ works here.

I just thought I'd share this with you guys and get your opinion on this. Maybe this type of usage is practical somewhere. For me, if reply was "n" or "y", then nothing should happen regardless of whether I have explicitly written break; in which case fin would equal whatever it originally equaled.

Quick program I wrote that you can use to test this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

int main()
{

    char reply('n'), fin('a');

    switch (reply)
    {
      case 'n': ;
      case 'N':
               fin = 'b';
               break;
      case 'y': ;
      case 'Y':
               fin = 'c';
    }

    cout << fin;

    cin.get();
    cin.get();

    return 0;
}
when you don't write any statements for a case, the code from the next case is inherited.
This is inaccurate for two reasons.
1. "Inherited" is completely out of place, in this context.
2. Even if the case does contain some statements (such as in your examples, since a lone semicolon is considered a statement), the control flow can still "fall through" (that's the proper term) to the next case if no break statement is encountered.

There's a good reason for this behavior. A switch block is a shorthand way of doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do{
    if (reply=='n')
        goto _1;
    if (reply=='N')
        goto _2;
    if (reply=='y')
        goto _3;
    if (reply=='Y')
        goto _4;
_1:
_2:
    //...
    break;
_3:
_4:
    //...
}while (0);
It's so similar, in fact, that if you try declaring a variable in one of the middle cases, some compilers will produce the same warning than when you try to declare a variable between a goto statement and its label:
1
2
3
4
5
6
7
8
9
10
11
switch (x){
    case 1:
        //...
    case 2:
        int a=0;
        break;
}

goto label;
int a=0;
label:
I tried a case where I didn't include break at all and I guess that's where the dandy default statement comes into play. Interesting. Thank you.

One more question, I noticed you used a do-while loop for example purposes. In such case, can't we write code without the do-while because it's only going to run once?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do{
    if (reply=='n')
        goto _1;
    if (reply=='N')
        goto _2;
    if (reply=='y')
        goto _3;
    if (reply=='Y')
        goto _4;
_1:
_2:
    //...
    break;
_3:
_4:
    //...
}while (0);


EQUALS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    if (reply=='n')
        goto _1;
    if (reply=='N')
        goto _2;
    if (reply=='y')
        goto _3;
    if (reply=='Y')
        goto _4;
_1:
_2:
    //...
    break;
_3:
_4:
    //... 


Correct?
can't we write code without the do-while because it's only going to run once?
What would you be breaking out of, then?
Ahh that makes sense.

But wait, can't you just use the continue statement and that would stop progression but I'm not sure if any other cases will apply?
No I get how to use them--I just found this particular scenario odd.
But wait, can't you just use the continue statement and that would stop progression
Could you give an example of what you mean?
closed account (EzwRko23)

In such case, can't we write code without the do-while because it's only going to run once?


C and C++ support breaking only out of for, while, do while and switch, not from any block of code, so this while was just for turning "break" into a "goto end of the block".

The switch statement has been inherited directly from C, which had it as a simple "goto" replacement. Note that this was created in times, when goto and assembly programming was very popular. Nowadays that design choice may seem odd or funny. In some modern languages the switch statement purposely doesn't allow fall-through - if several cases need common handling, then you make it simply a single case and fall-through is not needed.
Last edited on
@helios

I was referring to the fact that you can use continue to skip an if, for, while, and do-while statements.

I'm not sure how heavily this is used in C++ but in other languages, you can do something like this:
1
2
3
4
if ( CONDITION )
   continue;
else
   // something else 


Although the continue sometimes isn't needed (i.e. if i were to place curly brackets), I think it still works in a similar fashion. I think its just the language difference. I did a Google search and it also seems to be the case.

But it does make sense to have a break statement to "break" out of inside a loop. Thanks!

@xorebxebx
Ahhhhh. That makes sense. Thank you.
continue can't be used on if statements. You don't go into the else if the if's condition was true, whether you use curly braces or not.
closed account (EzwRko23)

I'm not sure how heavily this is used in C++ but in other languages, you can do something like this:


Not in C++. And I haven't heard of any language that allowed to use continue inside if. However, if you wish, you can implement such construct in Scala or some LISP, as a library. These languages allow for user-defined flow control structures.
I've used the continue statement in PHP before and its quite useful for scenarios where the only bool value is false and if true, then some data might be returned.

Anyways thank you guys for taking the time to explain this stuff to me. Thanks! :)
Topic archived. No new replies allowed.