Re: Funny loop with post-increment.

Frans Bouma <[email protected]> Tue, 19 Feb 2008 12:18:11 +0100
Newsgroups gmane.comp.windows.devel.dotnet.clr
Message-ID <00cd01c872e9$1cf7d6d0$56e78470$@nl>
> On Feb 19, 2008 9:21 PM, Frans Bouma <[email protected]> wrote:
> > > On Feb 19, 2008 8:57 PM, Frans Bouma <[email protected]> wrote:
> > > retarded break? i like the break in case statements. what is your
> > > issue with it, out of interest?
> >
> >         it doesn't add anything. There's no fall through in C#, so
> >         switch(foo)
> >         {
> >                 case 1:
> >                         // some code
> >                 case 2:
> >                         // some code
> >         }
> >
> >         would have been OK as well. If you forget a break statement, the
> > compiler will whine that you have to place the break there because there's
> no
> > fall through, however the break statement itself doesn't do anything:
there
> > was already a break in the code at that spot.
>
> there is indeed faltlhrough, just not fallthrough when you have code in one:
>
> switch(n){
>  case 1:
>  case 2:
>   Console.WriteLine("a");
>   break;
> }
>
> // ^^ valid
>
> switch(n){
>  case 1:
>    Console.WriteLine("m");
>  case 2:
>    Console.WriteLine("x");
>    break;
> }
>
> // ^^ invalid
>
>
> >         In other places where break is used, it does something: it breaks
> out
> > of the block it is in, though in switch/case it's unnecessary.
>
> it is necessary given the above.

        No, it's not.

switch(n){
 case 1:
 case 2:
  Console.WriteLine("a");
 case 3:
  Console.WriteLine("b");
}

        would be perfectly fine. As there's no fall through after code blocks,
the block of case 1/2 won't end up in the block of case 3. However 'break' is
required after the code of case 2.

        Anyway, it's never going to change :)

                FB

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com