|
o really ??
i think it can be used like this :
switch(iData)
{
case 0:
case 1:
// do stuff for 0 and 1
break;
case 2:
// do stuff for 2
case 3:
// do stuff for 2 and 3
break;
}
if the break statement is missing, it go through next case ..
VirtualVoid.NET
|
|
|
|
|
It is NOT C/C++ , my friend, it is C#
Juraj Suchan wrote: case 0:
case 1:
// do stuff for 0 and 1
break;
Fine: It is allowed.
Juraj Suchan wrote: case 2:
// do stuff for 2
case 3:
// do stuff for 2 and 3
break;
That's not allowed in C# (well, in fact, it is allowed as it stands, with just comments ).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
ou .. am I blind? sorry
VirtualVoid.NET
|
|
|
|
|
in C++ drop throw switch-statement is allowed in C# not - if it was that, you didn't see.
|
|
|
|
|
|
I like this reason as well
void foobar()
{
// blah blah blah
for(...)
{
// blablabla
for(..)
{
// foo foo foo
if(bar)
goto end;
}
}
end:
// beep bop a loola
}
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
You may throw an exception for that.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
That's a worse offense than a goto IMHO - exceptions should be reserved for exceptional conditions (the clues in the name) not used as an alternative control flow mechanism.
|
|
|
|
|
Rob Grainger wrote: not used as an alternative control flow mechanism.
That's exactly what they are (IMHO).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: That's exactly what they are (IMHO).
Guess so, but the operative bit to my eyes is "alternative". Exceptions are much heavier weight than loop variables etc. in terms of the execution model. Enclosing code in try/catch involves manipulation of the stack frame, tracking exception-handling blocks and other devilry.
Personally, if a condition can be anticipated, I consider it better to manage using traditional control flow mechanisms.
|
|
|
|
|
You're right and personally I won't use exceptions for that piece of code (I just made a not about the possibility). BTW I won't even write such a piece of code.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I take exception to that remark!
*Bypasses his finally block due to a goto statement*
|
|
|
|
|
hmm, what do you think? Why is the name of the Exception object "Exception" - because you should (ab)use it for control program flow?
So I see it like this:
Use Exceptions for exceptions
Avoid goto, but if you must, why not use goto?
A single goto, for a purpose as shown above, for shure does not leed to spaghetti-code,
but don't try to replace all your for/whiles... with a goto.
|
|
|
|
|
A simple 'break' would do the job, no need for 'goto'...
Regards
Thomas
|
|
|
|
|
break will only break the inner most loop!
C# is not java where you could have labelled loop...
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
I know.
But in the pseudocode above it will definitely execute the 'beep bop a loola'...
Regards
Thomas
|
|
|
|
|
|
Thomas Weller wrote: A simple 'break' would do the job, no need for 'goto'...
Except that a break doesn't break out of two for loops, just the inner one - necessitating a flag to detect whether the outer loop should be exited too.
In C# this seems a legitimate usage to me. I just wish C# had JavaScript's labelled loops/breaks, yielding the following code style:
outer_loop:
while (true) {
inner_loop:
while (true) {
if (condition1)
break inner_loop;
if (condition2)
break outer_loop;
}
}
Given that and fall-through in switch..case I see no need for goto.
|
|
|
|
|
A 'labelled break' is a 'goto' alias.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
What I mean is: a 'break' would be enough to execute the 'beep bop a loola' ...
By the way: I'd consider a 'flag' much better than a 'goto'...
Regards
Thomas
|
|
|
|
|
Thomas Weller wrote: By the way: I'd consider a 'flag' much better than a 'goto'...
Personally, I don't like introducing variables to track these kinds of conditions, but I guess at these levels it really comes down to choice of programming style.
Granted, though, that often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring.
|
|
|
|
|
Rob Grainger wrote: often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring
I fully agree on that. The need to introduce variables (or a 'goto') for the mere sake of controlling execution flow to me is clearly what is called a 'code smell'.
Rob Grainger wrote: I guess at these levels it really comes down to choice of programming style
Yup. The one way isn't really much better than the other...
Regards
Thomas
|
|
|
|
|
Add && !foo to the for loop conditions and/or use while instead.
|
|
|
|
|
Even Java has the "goto" similarity where you can "continue" to a label.
|
|
|
|
|
Use a break statement instead!
|
|
|
|