Breaks
-
But we're talking about C++ here...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Yes, and I'm saying that I've done it in C++, and I hate that I can't in C#.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
-
VuNic wrote:
I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.
I have, many times. I *hate* that .NET doesn't allow this, and forces me to put a break.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Well, you can still do a fall-through with empty cases:
switch(value) { case 0: case 1: case 2: System.Console.Writeln("CodeProject.com"); break; }
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn Rand -
Well, you can still do a fall-through with empty cases:
switch(value) { case 0: case 1: case 2: System.Console.Writeln("CodeProject.com"); break; }
"When you have made evil the means of survival, do not expect men to remain good. Do not expect them to stay moral and lose their lives for the purpose of becoming the fodder of the immoral. Do not expect them to produce, when production is punished and looting rewarded. Do not ask, `Who is destroying the world?' You are."
-Atlas Shrugged, Ayn RandI know, but that's not enough.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
VuNic wrote:
Do you think the "break;" [C++] command is really needed at the end of every case statement?
No. I think the default behavior is wrong here.
VuNic wrote:
I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.
I have. [edit]I make sure it's commented prominently.[/edit]
Cheers, Vikram.
"...we are disempowered to cultivate in their communities an inclination to assimilate to our culture." - Stan Shannon.
-
I know, but that's not enough.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
You can also "goto" an specific case, which is probably what the compiler does after rearranging "case"s for performance.
-
VuNic wrote:
Do you think the "break;" [C++] command is really needed at the end of every case statement?
No, it's not needed at the end of every one. There are rare instances where you might want to process code in one case but fall through to the next.
VuNic wrote:
I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.
Then in your case, you need a
break
between cases.VuNic wrote:
You know I missed to put a "break;" and it resulted in a big chaos
Silver lining - you've become a better programmer as a result of the experience.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
you've become a better programmer as a result of the experience.
:-O:-O Thanks. It's encourging. Now I'm raring to make big mistakes :-O. [My boss::wtf:]
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
-
You can also "goto" an specific case, which is probably what the compiler does after rearranging "case"s for performance.
Yep, they enforced break because otherwise the code is hard to read, and then they encouraged goto. Brilliant.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
VuNic wrote:
where I'd want the control to pass on to the next case after satisfying a particular case
In my area (micrcontroller) where most of the logic is done using state machine, switch-case are very popular, and we have very often the case that we fall through a case statement to another.
Company policy : no access to the internet but CP ~RaGE()
-
John Simmons / outlaw programmer wrote:
you've become a better programmer as a result of the experience.
:-O:-O Thanks. It's encourging. Now I'm raring to make big mistakes :-O. [My boss::wtf:]
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
Waitaminit - I'm not trying to be encouraging. You'll eventually fail at everything you try to do.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
VuNic wrote:
I've never been in a situation where I'd want the control to pass on to the next case after satisfying a particular case.
I have. It's one of the very few real uses i have for
switch
blocks...---- Scripts i’ve known... CPhog 1.8.2 - make CP better. Forum Bookmark 0.2.5 - bookmark forum posts on Pensieve Print forum 0.1.2 - printer-friendly forums Expand all 1.0 - Expand all messages In-place Delete 1.0 - AJAX-style post delete Syntax 0.1 - Syntax highlighting for code blocks in the forums
-
Waitaminit - I'm not trying to be encouraging. You'll eventually fail at everything you try to do.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
You'll eventually fail at everything you try to do
Now I'm trying to make mistakes. So thanks for your wishes john. ;P
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
That's one of the shortcomings of C that will cause trouble for any new C-based language that doesn't want to break code that gets ported to the new language. (Personally, I'd rather have the ideal language.) My real complaint is that if you have a
switch
in a loop, there are times when acase
wants tobreak
the loop, but can't (even though it cancontinue
the loop!). There should be some other keyword forswitch
es;break
(andcontinue
) should be reserved for loops. -
Yep, they enforced break because otherwise the code is hard to read, and then they encouraged goto. Brilliant.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Must be the left over VB influence...
¡El diablo está en mis pantalones! ¡Mire, mire! Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)! SELECT * FROM User WHERE Clue > 0 0 rows returned Save an Orange - Use the VCF! Techno Silliness
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
what about:
switch (something) { case A: case B: case C: DoSomething(); break; case D: case E: case F: DoSomethingElse(); break; .... }
Todd Smith
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
I have used fall throughs, though I've long felt the proper implementation would be that a new case statement implies break and to fall through you write
fallthrough;
. (I've also wanted the equivalent of "reswitch"--that is; go back and reevaluate the expression again.)Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
Do you think the "
break;
" [C++] command is really needed at the end of everycase
statement? I've never been in a situation where I'd want the control to pass on to the nextcase
after satisfying a particular case. You know I missed to put a "break;" and it resulted in a big chaos :doh:.
Dario: How is "directory" in French? (I mean a file system directory). John Simmons: "zee file holdaire thingie"
PC-Lint will catch missing break statements, and a lot of other common coding oversights.
-
I've probably coded a fall-through like that maybe twice in my life. Not counting those times where I goofed and forgot the
break
. ;) (That shows what I think is a design mistake in C - the default behavior should be no fall-through betweencase
s, since that's what programmers will want the vast majority of the time. Oh well.) I was just looking up the C# spec and C# requiresbreak
statements even though the behavior is no fall-through. Bwa? :confused:--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?
Not only that but a fall-though is still supported but requires the use of the
goto
statement. I would have preferred an explicit “fall-through” keyword.Steve
-
I have used fall throughs, though I've long felt the proper implementation would be that a new case statement implies break and to fall through you write
fallthrough;
. (I've also wanted the equivalent of "reswitch"--that is; go back and reevaluate the expression again.)Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke