473,587 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(myVariab le)
{
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 1456
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.co m

"Andrew Ducker" <an****@ducker. org.uk> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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(myVariab le)
{
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(myVariab le)
{
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(myVariab le)
{
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******** *************@f 14g2000cwb.goog legroups.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(myVariab le)
{
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******** *************@f 14g2000cwb.goog legroups.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(myVariab le)
{
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******** *************@f 14g2000cwb.goog legroups.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(myVariab le)
{
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******** *******@TK2MSFT NGP15.phx.gbl.. .

"Andrew Ducker" <an****@ducker. org.uk> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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(myVariab le)
{
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******** *******@TK2MSFT NGP15.phx.gbl.. .

"Andrew Ducker" <an****@ducker. org.uk> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.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(myVariab le)
{
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*********@ne tscape.net> wrote in message
news:Oq******** ******@TK2MSFTN GP12.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

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

Similar topics

6
16932
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 because the other program is busy. Choose 'Switch to' to activate the busy program and correct the problem." I don't know why this is displayed. I would prefer to disable the display of this message if possible. My app needs to be able to...
19
8080
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
4680
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
5051
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. <coff> is the table <postion> is the column I do not want the results in alpha order, but the order specified below, but I keep getting a syntax error: 'Syntax error (missing operator) in query expression '
6
1543
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 and sex = female then extracalc = 1.952 if race = black then extracalc = 1.21 if sex = female then extracalc = .742 else
19
3783
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 pull a value based on the price per gallon. For example if the price of fuel is 2.44 per gallon and the enter that they get 5.9 miles per gallon the cost of that mile is $.41. Then based on the cost per gallon of 2.44 we might pay them another...
4
2330
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. Here are the error codes and underneath i have listed the program. Thanks in advance for looking. client.c: In function `main': client.c:118: error: syntax error before '-' token client.c: At top level: client.c:132: error: conflicting types...
4
2614
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 display a certain message. here is my code srand(time(0));
2
2874
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 various module that I found it useful. Here is the 1st problem I encounter: I had a function to edit a event row form the database which is fine with me, than I pass on the code to a function that save(update) the data to the database.
0
7927
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8220
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7981
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8222
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6632
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5396
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3846
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2367
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.