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. The Lounge
  3. goto... Who uses it?

goto... Who uses it?

Scheduled Pinned Locked Moved The Lounge
questionlearning
131 Posts 66 Posters 13 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.
  • K Kevin Marois

    If you think the 'continue' keyword is anything at all like 'goto', you need to go back to programming 101.

    If it's not broken, fix it until it is

    D Offline
    D Offline
    dan sh
    wrote on last edited by
    #53

    I beg to differ here. Check out the IL generated for the two methods:

    void Test()
    {
    while (true)
    {
    string str = "Something";

                if (str.Length != 10)
                {
                    continue;
                }
    
            }
        }
    
        void Test2() {
        x:
            while (true)
            {
                string str = "Something";
    
                if (str.Length != 10)
                {
                    goto x;
                }
    
            }
        }
    

    //without GOTO .method private hidebysig instance void Test() cil managed { // Code size 32 (0x20) .maxstack 2 .locals init ([0] string str, [1] bool CS$4$0000) IL_0000: nop IL_0001: br.s IL_001c IL_0003: nop IL_0004: ldstr "Something" IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0010: ldc.i4.s 10 IL_0012: ceq IL_0014: stloc.1 IL_0015: ldloc.1 IL_0016: brtrue.s IL_001b IL_0018: nop IL_0019: br.s IL_001c IL_001b: nop IL_001c: ldc.i4.1 IL_001d: stloc.1 IL_001e: br.s IL_0003 } // end of method Proof::Test //With GOTO .method private hidebysig instance void Test2() cil managed { // Code size 32 (0x20) .maxstack 2 .locals init ([0] string str, [1] bool CS$4$0000) IL_0000: nop IL_0001: br.s IL_001c IL_0003: nop IL_0004: ldstr "Something" IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0010: ldc.i4.s 10 IL_0012: ceq IL_0014: stloc.1 IL_0015: ldloc.1 IL_0016: brtrue.s IL_001b IL_0018: nop IL_0019: br.s IL_0001 IL_001b: nop IL_001c: ldc.i4.1 IL_001d: stloc.1 IL_001e: br.s IL_0003 } // end of method Proof::Test2

    "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

    K S 2 Replies Last reply
    0
    • D DanielSheets

      This isn't a programming question. Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.

      G Offline
      G Offline
      Gary Wheeler
      wrote on last edited by
      #54

      I've not used it in 15 years or so, in any of my C# or C++ applications. There are too many better alternatives to goto in those languages. The last time I used goto was in C in the 90's, when the compilers I was using didn't (reliably) support exceptions.

      Software Zen: delete this;

      M 1 Reply Last reply
      0
      • B BobJanova

        That's horrible. Repeated code, and mixing of data management and UI code. It's crying out for throwing exceptions which you catch in the UI code, and a method

        int parseNumericValue(String text){
        int result;
        if(!int.TryParse(text == "" ? "0" : text, out result))
        throw new DataFormatException("Unable to parse data. Verify its entered correctly.\r\nValue = "+text);
        return result;
        }

        (and that's if the default exception from Parse doesn't do the job which it probably does). This is translated VB. And that's not a good thing.

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #55

        I would one step further since the ternary test is not only silly, it might throw an exception all on it's own due to text being null. If you KNOW something is "0", why parse it to 0? Why is an empty string valid? Is a null string valid? The code has other problems. Why create the file before you know whether there are any errors? Why set totalDelays and value back to zero? "its" is spelled "it's" in this context, but it should probably read "it was".

        B D 2 Replies Last reply
        0
        • D DanielSheets

          Where do you see UI code in the snippet I posted?

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #56

          MessageBox.Show

          D 1 Reply Last reply
          0
          • D dan sh

            I beg to differ here. Check out the IL generated for the two methods:

            void Test()
            {
            while (true)
            {
            string str = "Something";

                        if (str.Length != 10)
                        {
                            continue;
                        }
            
                    }
                }
            
                void Test2() {
                x:
                    while (true)
                    {
                        string str = "Something";
            
                        if (str.Length != 10)
                        {
                            goto x;
                        }
            
                    }
                }
            

            //without GOTO .method private hidebysig instance void Test() cil managed { // Code size 32 (0x20) .maxstack 2 .locals init ([0] string str, [1] bool CS$4$0000) IL_0000: nop IL_0001: br.s IL_001c IL_0003: nop IL_0004: ldstr "Something" IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0010: ldc.i4.s 10 IL_0012: ceq IL_0014: stloc.1 IL_0015: ldloc.1 IL_0016: brtrue.s IL_001b IL_0018: nop IL_0019: br.s IL_001c IL_001b: nop IL_001c: ldc.i4.1 IL_001d: stloc.1 IL_001e: br.s IL_0003 } // end of method Proof::Test //With GOTO .method private hidebysig instance void Test2() cil managed { // Code size 32 (0x20) .maxstack 2 .locals init ([0] string str, [1] bool CS$4$0000) IL_0000: nop IL_0001: br.s IL_001c IL_0003: nop IL_0004: ldstr "Something" IL_0009: stloc.0 IL_000a: ldloc.0 IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length() IL_0010: ldc.i4.s 10 IL_0012: ceq IL_0014: stloc.1 IL_0015: ldloc.1 IL_0016: brtrue.s IL_001b IL_0018: nop IL_0019: br.s IL_0001 IL_001b: nop IL_001c: ldc.i4.1 IL_001d: stloc.1 IL_001e: br.s IL_0003 } // end of method Proof::Test2

            "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #57

            The two examples you posted are NOT the same. The second one restarts the 'while' loop all over again, while the first one continues with the next iteration of the same loop. The goto had a totally different effect than the continue Paste this into a console app and walk it through:

            namespace ConsoleApplication1
            {
            class Program
            {
            static void Main(string[] args)
            {
            Test();
            Test2();

                    Console.ReadLine();
                   
                }
            
                static void Test()
                {
                    Console.WriteLine("Test");
                    int x = 0;
            
                    while (true)
                    {
                        x++;
            
                        if (x < 10)
                        {
                            continue;
                        }
            
                        Console.WriteLine(x);
                    }
                }
            
                static void Test2()
                {
                x:
                    Console.WriteLine("Test2");
                    int x = 0;
            
                    while (true)
                    {
                        if (x < 10)
                        {
                            goto x;
                        }
            
                        Console.WriteLine(x);
                    }
                }
            }
            

            }

            The first one starts printing at 10 and never stops because of the continue. The second one never does anything expect print "test2" because of the goto. 2 entirely different functionalities between 'goto' and 'continue'

            If it's not broken, fix it until it is

            D 1 Reply Last reply
            0
            • J Joe Woodbury

              MessageBox.Show

              D Offline
              D Offline
              DanielSheets
              wrote on last edited by
              #58

              Touché

              1 Reply Last reply
              0
              • C Chris Maunder

                In SQL - fairly often to jump to the error handler at the end of our sprocs. I'll admit there's no good reason we do this, since it's easy enough for us to avoid this with if statements, but it's a pattern used in our original code and so for consistency we stuck with it:

                Create Procedure MyProc as

                Begin Tran
                
                -- Do stuff...
                
                if @@error <> 0 goto errorHandler
                
                Commit Tran
                Return 0
                

                errorHandler:
                Rollback Tran
                Return 1

                cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #59

                Chris Maunder wrote:

                since it's easy enough for us to avoid this with if statements

                There is an IF statement, it just lacks the ELSE. :confused:

                "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

                1 Reply Last reply
                0
                • D dusty_dex

                  Many a word has been written about the use of multiple exits being bad practice. Perhaps the next discussion should be on the use of many returns.

                  Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.

                  H Offline
                  H Offline
                  H Brydon
                  wrote on last edited by
                  #60

                  dusty_dex wrote:

                  Many a word has been written about the use of multiple exits being bad practice.

                  I think I understand the issue from all sides and I don't drink that kool-aid. The way I look at it, the goto takes linear flow and folds it back on itself, causing the programmer to think about an interleaved mesh of logic instead of a stream. I see the following items as presenting different concepts and approve/disapprove of each on their own merits: (1) goto backwards within a loop (2) goto forwards within a loop (3) goto forwards to exit handler/error handler at end of method (4) break statement in a loop (4a) bounded loop (4b) infinite loop (ie. exit in the middle) (5) break statement in a switch() (6) continue statement in a loop (7) return statement inside a loop (one only per method) (8) multiple return statements in a method History has shown that #1 has caused the most problems, and #2 follows closely. #3 seems to be the one that most people defend, and I think it has some merit. I have used #3 but I still avoid it whenever I can. I regularly use all of #4 through #8 and have no problem with them. Multiple returns in a method are really no different from a break statement in terms of how the programmer's brain processes the logic. Multiple returns cause an exit from a method (so there is no tortured logic flow), you can debug it (eg. you can put a breakpoint on it), do not fold logic back (so that you can get to a statement from multiple directions) and can reduce the amount of code in a method (less code is better code). Probably other things too - this is off the top of my head.

                  -- Harvey

                  R 1 Reply Last reply
                  0
                  • K Kevin Marois

                    The two examples you posted are NOT the same. The second one restarts the 'while' loop all over again, while the first one continues with the next iteration of the same loop. The goto had a totally different effect than the continue Paste this into a console app and walk it through:

                    namespace ConsoleApplication1
                    {
                    class Program
                    {
                    static void Main(string[] args)
                    {
                    Test();
                    Test2();

                            Console.ReadLine();
                           
                        }
                    
                        static void Test()
                        {
                            Console.WriteLine("Test");
                            int x = 0;
                    
                            while (true)
                            {
                                x++;
                    
                                if (x < 10)
                                {
                                    continue;
                                }
                    
                                Console.WriteLine(x);
                            }
                        }
                    
                        static void Test2()
                        {
                        x:
                            Console.WriteLine("Test2");
                            int x = 0;
                    
                            while (true)
                            {
                                if (x < 10)
                                {
                                    goto x;
                                }
                    
                                Console.WriteLine(x);
                            }
                        }
                    }
                    

                    }

                    The first one starts printing at 10 and never stops because of the continue. The second one never does anything expect print "test2" because of the goto. 2 entirely different functionalities between 'goto' and 'continue'

                    If it's not broken, fix it until it is

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #61

                    I am not saying continue and goto are same (just realized it now). I am saying that internally it is used.

                    "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

                    1 Reply Last reply
                    0
                    • J J4amieC

                      If that's T-SQL in anything resembling modern SQL Server, there are much, much better constructs for error handling now.

                      C Offline
                      C Offline
                      Chris Maunder
                      wrote on last edited by
                      #62

                      try..catch is so bourgeois.

                      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                      1 Reply Last reply
                      0
                      • D DanielSheets

                        This isn't a programming question. Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.

                        S Offline
                        S Offline
                        Septimus Hedgehog
                        wrote on last edited by
                        #63

                        Fortran IV made extensive use of them. I used to love them at the time. Since then, I've never used them. Perhaps they do have a use and if teams that write compilers put them in then whose to argue for and against?

                        If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.

                        K 1 Reply Last reply
                        0
                        • D DanielSheets

                          This isn't a programming question. Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.

                          B Offline
                          B Offline
                          Brisingr Aerowing
                          wrote on last edited by
                          #64

                          I only use it in shell scripts/batch files, and I am working on setting up Python as my default Windows shell system. That will take some work, but I think it will be worth it!

                          Bob Dole

                          The internet is a great way to get on the net.

                          :doh: 2.0.82.7292 SP6a

                          1 Reply Last reply
                          0
                          • D DanielSheets

                            This isn't a programming question. Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.

                            C Offline
                            C Offline
                            C P User 3
                            wrote on last edited by
                            #65

                            None dare mention the fact that "break" and "goto" are really the same thing

                            R 1 Reply Last reply
                            0
                            • D DanielSheets

                              This isn't a programming question. Anyway... I find it useful in very few situations. It can make for cleaner code if used correctly. Of course, it can also be over used.

                              M Offline
                              M Offline
                              Member 4608898
                              wrote on last edited by
                              #66

                              I hate being restricted by programming standards that explicitly say "you should not use gotos". What is the point of using a language that has gotos and not being allowed to use them. It is like someone giving you an ornamental chocolate and your mum telling you that you aren't allowed to eat it because it looks pretty. Why did they give it to us if we weren't meant to eat it. Why does a language have gotos if we are not allowed to use them.:confused: When I was in airlines, the technical manager said that if anyone wanted to use gotos, he wanted a writeup of reasons. Now here was a challenge: all I had to do was reproduce the essay "structured programming using gotos" which I wrote when I was in Uni (1976), with snippets of how much simpler the code was with gotos than without. It was about 3 pages. The goto restriction was lifted for me and no one ever questioned it. The same thing happened when I was in medical. Guess they had never dealt with a real computer scientist before. Everyone else was an ex engineer, astronomer, mathematician or physicist. They just read about programming concepts: they never had to do the research or write up essays on simple, everyday things like use of do-while-false loops or indentation styles.

                              1 Reply Last reply
                              0
                              • G Gary Wheeler

                                I've not used it in 15 years or so, in any of my C# or C++ applications. There are too many better alternatives to goto in those languages. The last time I used goto was in C in the 90's, when the compilers I was using didn't (reliably) support exceptions.

                                Software Zen: delete this;

                                M Offline
                                M Offline
                                Member 4608898
                                wrote on last edited by
                                #67

                                I try to squeeze at least one in at every site where I've worked, just to prove a point (the points being, nobody reads the programming standards and not many people actually read code). I've succeeded in 6 out of 9.

                                1 Reply Last reply
                                0
                                • L Lost User

                                  DanielSheets wrote:

                                  It can make for cleaner code if used correctly.

                                  Your definition of "cleaner code" is quite odd then. There is no "correct" usage of it in modern high level languages. It simply creates an unmaintainable ball of string.

                                  Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                                  J Offline
                                  J Offline
                                  jschell
                                  wrote on last edited by
                                  #68

                                  Collin Jasnoch wrote:

                                  There is no "correct" usage of it in modern high level languages

                                  I doubt the the absoluteness of that statement. It is likely there are few cases but even one refutes your statement.

                                  S 1 Reply Last reply
                                  0
                                  • D dusty_dex

                                    That looks bad to me. Aren't you supposed to use

                                    break ;

                                    to jump out of loops? Exception handling will usually bog down the handled block of code.

                                    Q. Hey man! have you sorted out the finite soup machine? A. Why yes, it's celery or tomato.

                                    J Offline
                                    J Offline
                                    jschell
                                    wrote on last edited by
                                    #69

                                    dusty_dex wrote:

                                    Exception handling will usually bog down the handled block of code.

                                    If that is a reference to performance then as stated it isn't true.

                                    1 Reply Last reply
                                    0
                                    • C Chris Maunder

                                      In SQL - fairly often to jump to the error handler at the end of our sprocs. I'll admit there's no good reason we do this, since it's easy enough for us to avoid this with if statements, but it's a pattern used in our original code and so for consistency we stuck with it:

                                      Create Procedure MyProc as

                                      Begin Tran
                                      
                                      -- Do stuff...
                                      
                                      if @@error <> 0 goto errorHandler
                                      
                                      Commit Tran
                                      Return 0
                                      

                                      errorHandler:
                                      Rollback Tran
                                      Return 1

                                      cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                      J Offline
                                      J Offline
                                      Jan Steyn
                                      wrote on last edited by
                                      #70

                                      An error condition returns the same value as a non-error condition? Many hamsters are dying because of that.

                                      C 1 Reply Last reply
                                      0
                                      • J Jan Steyn

                                        An error condition returns the same value as a non-error condition? Many hamsters are dying because of that.

                                        C Offline
                                        C Offline
                                        Chris Maunder
                                        wrote on last edited by
                                        #71

                                        Oops. Typo.

                                        cheers, Chris Maunder The Code Project | Co-founder Microsoft C++ MVP

                                        1 Reply Last reply
                                        0
                                        • S Septimus Hedgehog

                                          Fortran IV made extensive use of them. I used to love them at the time. Since then, I've never used them. Perhaps they do have a use and if teams that write compilers put them in then whose to argue for and against?

                                          If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.

                                          K Offline
                                          K Offline
                                          Kyudos
                                          wrote on last edited by
                                          #72

                                          Have you ever tried to "unuse" GOTO in optimized F77? The logic required to replicate the program flow without it is often horrendous and generally not worth the effort if the original code works...

                                          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