Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. OMG a "Goto"

OMG a "Goto"

Scheduled Pinned Locked Moved The Weird and The Wonderful
asp-netcomhelpquestion
42 Posts 21 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Member 4072299

    For me, the GOTO statement is to be removed completely. When you got to situations where you need to skip a finally, you should better rethink your design/architecture and your code. A clean rewrite of your code and your conditions and tests let you avoid situations like this easily and cleanly!!! ________________________________________ http://www.glyphart.com neat icons and glyphs for websites and applications

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #4

    You cannot skip (at leat that way) finally. On the overall I agree with you. I don't see any reason (maybe there are...) to live the goto statement in C#. IMHO: - In C the goto statement makes perfectly sense. - In C++ it makes seem because of C compatibility. :)

    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]

    1 Reply Last reply
    0
    • C CPallini

      My 5, that code is pure Horror. Anyway (I made a test) the goto statement it is ininfluent. BTW I didn't know C# has goto statement, is there a reason to have it in such a language? :)

      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]

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #5

      CPallini wrote:

      BTW I didn't know C# has goto statement, is there a reason to have it in such a language?

      A reason ? Maybe no, but you could need one if you use a switch.

      switch(iData)
      {
      case 0:
      case 1:
       // do stuff for 0 and 1
       break;
      case 2:
       // do stuff for 2
       goto 3;
      case 3:
       // do stuff for 2 and 3
       break;
      }
      

      Learn from the mistakes of others, you may not live long enough to make them all yourself.

      M L 2 Replies Last reply
      0
      • C CPallini

        My 5, that code is pure Horror. Anyway (I made a test) the goto statement it is ininfluent. BTW I didn't know C# has goto statement, is there a reason to have it in such a language? :)

        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]

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #6

        Search the lounge for prior floggings of this subject. There are (very) rare cases when a goto can actually make the code more readable. "Because otherwise I'd have a half dozen levels of if/else indentation in [a 1000 line VBA function]" isn't one of them. :mad: :doh: :omg: :wtf: :mad: :((

        Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

        1 Reply Last reply
        0
        • C CARPETBURNER

          protected void Page_Load(object sender, EventArgs e)
          {
          string str = "Even exception occur again in catch block still finally block is Working";
          try
          {
          throw new IndexOutOfRangeException();
          }
          catch
          {
          goto Hello;
          throw new IndexOutOfRangeException();

          }
          finally
          {
          Response.Redirect("Error.aspx?str="+ str);
          }
          Hello:
          Response.Write("Finally Skiped!");
          }

          http://www.codeproject.com/KB/aspnet/DotNetBulletQuestions.aspx?msg=2764693#xx2764693xx[^] A goto in a try/catch/finally block... urrrghh!

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #7

          That code was written simply to make a point, it's not "real" code, so it doesn't qualify as a Coding Horror.

          1 Reply Last reply
          0
          • B BadKarma

            CPallini wrote:

            BTW I didn't know C# has goto statement, is there a reason to have it in such a language?

            A reason ? Maybe no, but you could need one if you use a switch.

            switch(iData)
            {
            case 0:
            case 1:
             // do stuff for 0 and 1
             break;
            case 2:
             // do stuff for 2
             goto 3;
            case 3:
             // do stuff for 2 and 3
             break;
            }
            

            Learn from the mistakes of others, you may not live long enough to make them all yourself.

            M Offline
            M Offline
            MidwestLimey
            wrote on last edited by
            #8

            Yeah, the poor mans drop through.

            Bar fomos edo pariyart gedeem, agreo eo dranem abal edyero eyrem kalm kareore

            1 Reply Last reply
            0
            • B BadKarma

              CPallini wrote:

              BTW I didn't know C# has goto statement, is there a reason to have it in such a language?

              A reason ? Maybe no, but you could need one if you use a switch.

              switch(iData)
              {
              case 0:
              case 1:
               // do stuff for 0 and 1
               break;
              case 2:
               // do stuff for 2
               goto 3;
              case 3:
               // do stuff for 2 and 3
               break;
              }
              

              Learn from the mistakes of others, you may not live long enough to make them all yourself.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              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**

              C 1 Reply Last reply
              0
              • L Lost User

                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**

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #10

                It is NOT C/C++, my friend, it is C# ;P

                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 :-D ). :)

                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]

                L 1 Reply Last reply
                0
                • C CPallini

                  It is NOT C/C++, my friend, it is C# ;P

                  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 :-D ). :)

                  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]

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  ou .. am I blind? :) sorry

                  VirtualVoid**.NET**

                  J 1 Reply Last reply
                  0
                  • C CPallini

                    My 5, that code is pure Horror. Anyway (I made a test) the goto statement it is ininfluent. BTW I didn't know C# has goto statement, is there a reason to have it in such a language? :)

                    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]

                    S Offline
                    S Offline
                    Super Lloyd
                    wrote on last edited by
                    #12

                    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.

                    C T P A T 5 Replies Last reply
                    0
                    • S Super Lloyd

                      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.

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #13

                      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]

                      R J 2 Replies Last reply
                      0
                      • S Super Lloyd

                        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.

                        T Offline
                        T Offline
                        Thomas Weller 0
                        wrote on last edited by
                        #14

                        A simple 'break' would do the job, no need for 'goto'... :^) Regards Thomas

                        S R 2 Replies Last reply
                        0
                        • T Thomas Weller 0

                          A simple 'break' would do the job, no need for 'goto'... :^) Regards Thomas

                          S Offline
                          S Offline
                          Super Lloyd
                          wrote on last edited by
                          #15

                          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.

                          T 1 Reply Last reply
                          0
                          • C CPallini

                            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]

                            R Offline
                            R Offline
                            Rob Grainger
                            wrote on last edited by
                            #16

                            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.

                            C A 2 Replies Last reply
                            0
                            • T Thomas Weller 0

                              A simple 'break' would do the job, no need for 'goto'... :^) Regards Thomas

                              R Offline
                              R Offline
                              Rob Grainger
                              wrote on last edited by
                              #17

                              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) {
                              // .. do something
                              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.

                              C T 2 Replies Last reply
                              0
                              • R Rob Grainger

                                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.

                                C Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #18

                                Rob Grainger wrote:

                                not used as an alternative control flow mechanism.

                                That's exactly what they are (IMHO). :-D

                                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]

                                R 1 Reply Last reply
                                0
                                • R Rob Grainger

                                  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) {
                                  // .. do something
                                  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.

                                  C Offline
                                  C Offline
                                  CPallini
                                  wrote on last edited by
                                  #19

                                  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]

                                  1 Reply Last reply
                                  0
                                  • R Rob Grainger

                                    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) {
                                    // .. do something
                                    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.

                                    T Offline
                                    T Offline
                                    Thomas Weller 0
                                    wrote on last edited by
                                    #20

                                    What I mean is: a 'break' would be enough to execute the 'beep bop a loola' :-D ... By the way: I'd consider a 'flag' much better than a 'goto'... Regards Thomas

                                    R 1 Reply Last reply
                                    0
                                    • S Super Lloyd

                                      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.

                                      T Offline
                                      T Offline
                                      Thomas Weller 0
                                      wrote on last edited by
                                      #21

                                      I know. But in the pseudocode above it will definitely execute the 'beep bop a loola'... :-D Regards Thomas

                                      J 1 Reply Last reply
                                      0
                                      • S Super Lloyd

                                        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.

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #22

                                        Add && !foo to the for loop conditions and/or use while instead.

                                        1 Reply Last reply
                                        0
                                        • R Rob Grainger

                                          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.

                                          A Offline
                                          A Offline
                                          Andrew Rissing
                                          wrote on last edited by
                                          #23

                                          I take exception to that remark! *Bypasses his finally block due to a goto statement*

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups