473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Can someone explain why switch syntax is the way it is?

And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D

Nov 17 '05 #1
12 1442
Andrew,

It's actually not a bad idea. If you put a suggestion on the product
feedback site, I would be more than happy to vote for it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew Ducker" <an****@ducker.org.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D

Nov 17 '05 #2
I think the reason is that it might make it look a little cleaner for
something like this:

switch(myVariable)
{
case 1:
case 2:
//do something if myVariable is 1 or 2;
break;
case 3:
case 4:
//do something if myVariable is 3 or 4;
break;
default:
}

Although, I think it would even look nicer if you could do "case 1, 2: ".
It probably goes back to the roots of the C language.

"Andrew Ducker" wrote:
And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D

Nov 17 '05 #3
Hi,

Honestly I prefer the break , it not that much longer and is VERY clear
the use,

If a C person see it, he knows that to expect.

If a C person see your proposal , well it's not explicit :)

also maybe it's easier for the compiler writers, in your case depending of
where the } is found it means more than a closing context.
I prefer the way it's now :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Andrew Ducker" <an****@ducker.org.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D

Nov 17 '05 #4

"Andrew Ducker" <an****@ducker.org.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D


Well, it would certainly be more "C# like" that way, but it would prevent
intentional "flow through" from one case to another. While I think such
"flow through" is a horrible, horrible idea, I'm sure someone out there
thinks it's absolutely necessary.
Nov 17 '05 #5
"Andrew Ducker" <an****@ducker.org.uk> schrieb im Newsbeitrag
news:11*********************@f14g2000cwb.googlegro ups.com...
And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D


An answer you can find on this site:
http://www.gotdotnet.com/team/csharp...sk.aspx#switch

The sentence that answers your question is at the end of the paragraph:
The reason this wasnt done was so that developers who were very used to C++
wouldnt have a hard time understanding what a switch statement was doing.

But I agree with you: Simply exiting a switch statement on reaching the end
of a case is much more usefull.

A good message:
This behavior could be added to C# without breaking existing programms. So,
if C#-Designers in future will agree with this, they could simply enhance C#
in that way.
Nov 17 '05 #6

"Scott Roberts" <sc***********@no-spam.intelebill.com> wrote in message
news:OX***************@TK2MSFTNGP15.phx.gbl...

"Andrew Ducker" <an****@ducker.org.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D


Well, it would certainly be more "C# like" that way, but it would prevent
intentional "flow through" from one case to another. While I think such
"flow through" is a horrible, horrible idea, I'm sure someone out there
thinks it's absolutely necessary.


Since C# does not support the C++ style of "flow through" by leaving out the
break statement between cases (except when multiple cases pertain to the
exact same block of code), this is really not a big problem. Something like
this would work:

switch(val)
{
case 1:
case 2:
{
textBox1.Text = "Nothing";
}
case 3:
{
textBox1.Text = "Answer";
}
default:
{
}
}

I like it. It is definitely more consistent. Having this style as an
alternative format really does not seem to add any confusion - it is just as
clear what is being done.
Nov 17 '05 #7
"Scott Roberts" <sc***********@no-spam.intelebill.com> schrieb im
Newsbeitrag news:OX***************@TK2MSFTNGP15.phx.gbl...

"Andrew Ducker" <an****@ducker.org.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
And no, this isn't a complaint about break - I'm very happy to make
things explicit.

However, why isn't the format something like:

switch(myVariable)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}

That way the 'break' is subsumed into the closing curly brackets, and
it follows the same way of grouping commands as the rest of the
language does.

Andy D


Well, it would certainly be more "C# like" that way, but it would prevent
intentional "flow through" from one case to another. While I think such
"flow through" is a horrible, horrible idea, I'm sure someone out there
thinks it's absolutely necessary.

Well, it wouldn't prevent anything. intentional "flow through" would be as
possible
as it is know via goto case
Nov 17 '05 #8

"Jeremy Williams" <je*********@netscape.net> wrote in message
news:Oq**************@TK2MSFTNGP12.phx.gbl...
Since C# does not support the C++ style of "flow through" by leaving out the break statement between cases (except when multiple cases pertain to the
exact same block of code), this is really not a big problem.


As soon as I posted something in the back of my brain told me that C#
doesn't support C++ style flow-through. However, since I never, ever use
flow-through I never bothered to check it.
Nov 17 '05 #9
I like this idea. At first I was skeptical, but today I realized that
it would clear up the scoping stupidity that currently plagues switch,
to wit:

switch (myVariable)
{
case 1:
int result = 15;
...
break;
case 2:
int result = 12;
...
break;
}

currently causes a compiler error, the compiler complaining that
"result" is defined twice in the same scope. However, simply removing
the "int" definition on the second "result" line seems unsatisfactory:
how can declaring a variable in one case make it available in other
cases? Suddenly declaration scope and execution flow are strangely
divorced. (Yes, I realize that they really are different things, but
one can usually ignore that difference, except in this one case where
it jumps out at you.)

Enclosing case code in curlies would solve this problem, and make the
declaration scopes more intuitive:

switch (myVariable)
{
case 1:
{
int result = 15;
...
}
case 2:
{
int result = 12;
...
}
}

would now compile just fine, which is, I would argue, more what one
would expect.

Nov 17 '05 #10

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I like this idea. At first I was skeptical, but today I realized that
it would clear up the scoping stupidity that currently plagues switch,
to wit:

switch (myVariable)
{
case 1:
int result = 15;
...
break;
case 2:
int result = 12;
...
break;
}

currently causes a compiler error, the compiler complaining that
"result" is defined twice in the same scope. However, simply removing
the "int" definition on the second "result" line seems unsatisfactory:
how can declaring a variable in one case make it available in other
cases? Suddenly declaration scope and execution flow are strangely
divorced. (Yes, I realize that they really are different things, but
one can usually ignore that difference, except in this one case where
it jumps out at you.)

Enclosing case code in curlies would solve this problem, and make the
declaration scopes more intuitive:

switch (myVariable)
{
case 1:
{
int result = 15;
...
}
case 2:
{
int result = 12;
...
}
}

would now compile just fine, which is, I would argue, more what one
would expect.

Of course, this is an artifact of block scoping itself. in this case result
is not available outside of your switch block, but in the former case it is.
Seperate case labels aren't seperate scopes, they are just seperate
execution paths, different from a collection of labels and matching gotos
only in clarity.

You shouldn't ever be considering execution flow when considering scope
anyway. The compiler isn't that sophisticated, it never knows where
execution will actually end. Building an ambigious execution path isn't
terribly hard.
Nov 17 '05 #11
> You shouldn't ever be considering execution flow when considering scope anyway.

Well, yes, I realize that... in theory. However, in practice they
almost always line up nicely... except in the case of select. :-)

Nov 17 '05 #12

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
You shouldn't ever be considering execution flow when considering scope
anyway.


Well, yes, I realize that... in theory. However, in practice they
almost always line up nicely... except in the case of select. :-)


Only because if statements don't allow declarations outside of blocks, if it
made sense to do so you'd see the same thing there as well.
Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
19
by: Robert Scheer | last post by:
Hi. In VBScript I can use a Select Case statement like that: Select Case X Case 1 to 10 'X is between 1 and 10 Case 11,14,16 'X is 11 or 14 or 16 End Select
2
by: JS | last post by:
As I can remember this is not allowed in java: int x, y, z; switch(x,y,z){ case 1,2,3: System.out.println("Hep"); break; }
2
by: Tom Morgan | last post by:
Hi everyone, I'm having a brain freeze today so if the answer is a misplacced comma or a missing command, I would be happy. What I want to do is use access to return results via an .asp page. ...
6
by: sparks | last post by:
extracalc = Switch(Me.Parent.Race_Black = -1 And Me.Parent.Sex = "Female", 1.952, Me.Parent.Race_Black = -1, 1.21, Me.Parent.Sex = "Female", 0.742, 1) I look at this and say ok if race = black...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
4
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
4
by: huzzaa | last post by:
I am using a switch statement that will decide from a random number what message to display. I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.