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;
}
|