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.
  • 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
            • 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!

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

              Maybe someone was just trying to test something and forgot to yank out the code? One can dream right....

              P 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
                Paul Conrad
                wrote on last edited by
                #25

                :wtf:

                "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                1 Reply Last reply
                0
                • A Andrew Rissing

                  Maybe someone was just trying to test something and forgot to yank out the code? One can dream right....

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

                  (You need to follow the link.)

                  A 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    (You need to follow the link.)

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

                    Gotcha. ;D

                    1 Reply Last reply
                    0
                    • C CPallini

                      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 Offline
                      R Offline
                      Rob Grainger
                      wrote on last edited by
                      #28

                      CPallini wrote:

                      That's exactly what they are (IMHO).

                      Guess so, but the operative bit to my eyes is "alternative". Exceptions are much heavier weight than loop variables etc. in terms of the execution model. Enclosing code in try/catch involves manipulation of the stack frame, tracking exception-handling blocks and other devilry. Personally, if a condition can be anticipated, I consider it better to manage using traditional control flow mechanisms.

                      C 1 Reply Last reply
                      0
                      • T Thomas Weller 0

                        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 Offline
                        R Offline
                        Rob Grainger
                        wrote on last edited by
                        #29

                        Thomas Weller wrote:

                        By the way: I'd consider a 'flag' much better than a 'goto'...

                        Personally, I don't like introducing variables to track these kinds of conditions, but I guess at these levels it really comes down to choice of programming style. Granted, though, that often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring.

                        T 1 Reply Last reply
                        0
                        • R Rob Grainger

                          CPallini wrote:

                          That's exactly what they are (IMHO).

                          Guess so, but the operative bit to my eyes is "alternative". Exceptions are much heavier weight than loop variables etc. in terms of the execution model. Enclosing code in try/catch involves manipulation of the stack frame, tracking exception-handling blocks and other devilry. Personally, if a condition can be anticipated, I consider it better to manage using traditional control flow mechanisms.

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

                          You're right and personally I won't use exceptions for that piece of code (I just made a not about the possibility). BTW I won't even write such a piece of code. :rolleyes:

                          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:

                            By the way: I'd consider a 'flag' much better than a 'goto'...

                            Personally, I don't like introducing variables to track these kinds of conditions, but I guess at these levels it really comes down to choice of programming style. Granted, though, that often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring.

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

                            Rob Grainger wrote:

                            often the need to perform such trickery may often signal a deficiency that may better be addressed by refactoring

                            I fully agree on that. The need to introduce variables (or a 'goto') for the mere sake of controlling execution flow to me is clearly what is called a 'code smell'.

                            Rob Grainger wrote:

                            I guess at these levels it really comes down to choice of programming style

                            Yup. The one way isn't really much better than the other... Regards Thomas

                            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.

                              A Offline
                              A Offline
                              adamsappel
                              wrote on last edited by
                              #32

                              Even Java has the "goto" similarity where you can "continue" to a label.

                              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.

                                T Offline
                                T Offline
                                tobster
                                wrote on last edited by
                                #33

                                Use a break statement instead!

                                S 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!

                                  I Offline
                                  I Offline
                                  icemanind
                                  wrote on last edited by
                                  #34

                                  I've programmed for over 10 years. The old BASIC, like GW-Basic or BasicA, yes I can honestly say that goto came in handy. However, I never once used it in C,C++, VB.NET or C#. Never. Since all modern languages have a Continue statement and a Break statement (for loops), I never needed a use for a goto. The reason, I'm guessing, that its not being removed in modern languages is to keep it compatible with old code. Very rarely does a language "lose" a command. They keep them so old code is compatible.

                                  C 1 Reply Last reply
                                  0
                                  • T tobster

                                    Use a break statement instead!

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

                                    try it! you will improve you C# knowledge! or maybe read better... it's a double nested 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.

                                    1 Reply Last reply
                                    0
                                    • I icemanind

                                      I've programmed for over 10 years. The old BASIC, like GW-Basic or BasicA, yes I can honestly say that goto came in handy. However, I never once used it in C,C++, VB.NET or C#. Never. Since all modern languages have a Continue statement and a Break statement (for loops), I never needed a use for a goto. The reason, I'm guessing, that its not being removed in modern languages is to keep it compatible with old code. Very rarely does a language "lose" a command. They keep them so old code is compatible.

                                      C Offline
                                      C Offline
                                      ClementsDan
                                      wrote on last edited by
                                      #36

                                      It's an understatement to say that "goto came in handy" in the old style BASIC. You had to use GOTO because the language was severely lacking in control flow. DO...LOOP didn't exist, so you had to make those loops with GOTO. SELECT CASE didn't exist, so you had to use ON...GOTO. Multi-line IF statements didn't exist, so you either had to cram everything on one line or use GOTO. And exceptions didn't exist, so you used ON ERROR GOTO. So you ended up with code like this, filled with GOTO. And furthermore, line numbers were mandatory on every line, so it was very difficult to tell which lines were GOTO targets and which weren't. And this is what caused all the animosity towards the GOTO statement.

                                      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!

                                        J Offline
                                        J Offline
                                        Johan Vorster
                                        wrote on last edited by
                                        #37

                                        LMAO!!!! Didn't release it even existed in c#!! Love it! That will show those annoying OO purest with their fancy classes and methods and thingies!! :laugh: Go the GOTO!

                                        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!

                                          S Offline
                                          S Offline
                                          steveb
                                          wrote on last edited by
                                          #38

                                          for... do... while... return... switch... if... else... are all glorified GOTO statements. They all translate to assembler JMP by compiler. :laugh:

                                          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