Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 9th, 2005, 06:36 AM
Friend of Wrox
Points: 464, Level: 7
Points: 464, Level: 7 Points: 464, Level: 7 Points: 464, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: Colombo, , Sri Lanka.
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default C# switch statement

Hi,

C# switch statement has designed to not allow fall-through, but still why does it require a break?

__________________
nalaka hewage
http://nalakahewage.blogspot.com
Reply With Quote
  #2 (permalink)  
Old December 9th, 2005, 10:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Well if you would see, the break statement is needed in the default block as well. Thats because its not the
break statement thats needed its the requirement of all the blockes under switch to have a 'jump statement'.
Commomnly you provide the break statement so thats what you see, but it can be any jump statement.

Regards
Ankur Verma
Reply With Quote
  #3 (permalink)  
Old December 9th, 2005, 08:28 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is mainly to be consistent with C and C++, and to avoid confusion for programmers from that background. Strictly speaking, it should not be necessary.

Also, be aware that C# switches do allow fall-through for empty cases:
e.g.,
switch (x)
{
  case 1:
  case 2:
     ...
     break;
}



Reply With Quote
  #4 (permalink)  
Old December 13th, 2005, 12:01 AM
Friend of Wrox
Points: 464, Level: 7
Points: 464, Level: 7 Points: 464, Level: 7 Points: 464, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: Colombo, , Sri Lanka.
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default

Hi,

switch(value)
{
  case 0:
    printf(In case 0\n);
  case 1:
    printf(In case 1\n);
  case 2:
    printf(In case 2\n);
    break;
}

C/C++ allows us to use the switch statement like this without a break statement and this will result,

In case 0
In case 1
In case 2

But why can't use use the switch statement without a break or any jump statement in C# like we are using in C/C++?

I know we can implement fall-through in C# using "goto" in each case like, but C# still requires a jump statement.
switch(value)
{
  case 0:
    printf(In case 0\n);
    goto case 1;
  case 1:
    printf(In case 1\n);
    goto case 2;
  case 2:
    printf(In case 2\n);
    break;
}


Reply With Quote
  #5 (permalink)  
Old December 14th, 2005, 05:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

Well nalla I must say that the case you've presented here needs to be re-thought for it design.
I would say that empty case blocks make sense with fallthrougs and C# does support that as Doknjas
mentioned. But with non-empty case blocks, if fallthroughs are not supported, i find it a welcome change.

Also, I dare say, you dont face situations where fall throughs are required as often as as those
where they are not. So if you want fall though supported even with non empty cases blocks, you are
actually asking support for the exception rather than for the norm as they call it.

In C++ where fall through for non empty case blocks is supported, is easier to program a logical error thats
hard to spot latter on. C# is 'safer' that way.

Regards
Ankur Verma
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
switch statement crmpicco Javascript How-To 3 March 9th, 2015 10:37 PM
Switch-statement grstad C# 3 June 11th, 2008 06:53 AM
Using Switch statement in XHTML Lizane Java Basics 1 June 15th, 2007 06:46 AM
switch statement seymour_glass C# 5 January 24th, 2007 12:08 AM
build switch statement to make dynamic crmpicco Javascript How-To 0 October 27th, 2005 05:44 AM



All times are GMT -4. The time now is 06:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.