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, alive and well after all [modified]

GOTO, alive and well after all [modified]

Scheduled Pinned Locked Moved The Lounge
businessquestiondiscussion
25 Posts 11 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.
  • L Lost User

    I just pulled this out of MSDN:

    using System;
    class SwitchTest
    {
    public static void Main()
    {
    Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
    Console.Write("Please enter your selection: ");
    string s = Console.ReadLine();
    int n = int.Parse(s);
    int cost = 0;
    switch(n)
    {
    case 1:
    cost += 25;
    break;
    case 2:
    cost += 25;
    goto case 1;
    case 3:
    cost += 50;
    goto case 1;
    default:
    Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
    break;
    }
    if (cost != 0)
    Console.WriteLine("Please insert {0} cents.", cost);
    Console.WriteLine("Thank you for your business.");
    }
    }

    It has been a long time since I have seen so much 'goto' in one place, especially because I also count in switch statements and 'break'. This makes the question of using goto or not look a bit academic. Edit: In the old days such code would have made much more sense. On an 8 bit CPU without any instructions for multiplication or division all kinds of crazy things were done to avoid having to multiply or divide. But those days are long over...

    "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

    modified on Wednesday, March 23, 2011 4:58 AM

    D Offline
    D Offline
    David1987
    wrote on last edited by
    #8

    CDP1802 wrote:

    On an 8 bit CPU without any instructions for multiplication or division all kinds of crazy things were done to avoid having to multiply or divide

    Like this: cost = n + (n << 3) + (n << 4); Or: cost = (25 & ((n << 7) >> 7)) + (50 & ((n << 6) >> 7)); Or: cost = lookuptable[n]; // I'd probably do this Or: (ok so this is a 16 bit processor that does have a mul, but it's super slow)

    shr dx,1
    jz _skip25
    add ax,25
    _skip25:
    shr dx,1
    jz _skip50
    add ax,50
    _skip50:

    Or:

    srl h
    jr z,{+}
    add a,25
    +: srl h
    jr z,{+}
    add a,50
    {+}:

    L 1 Reply Last reply
    0
    • W Wayne Gaylard

      What's most useful to a developer - a coffee machine :laugh:

      M Offline
      M Offline
      Marcus_2
      wrote on last edited by
      #9

      Wayne Gaylard wrote:

      What's most useful to a developer - a coffee machine :laugh:

      When I got to office this morning I discovered a catastrophe, the coffee machine had broken down last evening... :sigh:

      W 1 Reply Last reply
      0
      • M Marcus_2

        Wayne Gaylard wrote:

        What's most useful to a developer - a coffee machine :laugh:

        When I got to office this morning I discovered a catastrophe, the coffee machine had broken down last evening... :sigh:

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #10

        IF(CoffeeMachine.IsBroken)
        {
        GOTO You.Home;
        }

        1 Reply Last reply
        0
        • D David1987

          CDP1802 wrote:

          On an 8 bit CPU without any instructions for multiplication or division all kinds of crazy things were done to avoid having to multiply or divide

          Like this: cost = n + (n << 3) + (n << 4); Or: cost = (25 & ((n << 7) >> 7)) + (50 & ((n << 6) >> 7)); Or: cost = lookuptable[n]; // I'd probably do this Or: (ok so this is a 16 bit processor that does have a mul, but it's super slow)

          shr dx,1
          jz _skip25
          add ax,25
          _skip25:
          shr dx,1
          jz _skip50
          add ax,50
          _skip50:

          Or:

          srl h
          jr z,{+}
          add a,25
          +: srl h
          jr z,{+}
          add a,50
          {+}:

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

          Yes, this looks familiar :) Lookup tables are quite good for many cases, but you can't afford many of them if your system only has 4k RAM :)

          "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

          D 1 Reply Last reply
          0
          • L Lost User

            Yes, this looks familiar :) Lookup tables are quite good for many cases, but you can't afford many of them if your system only has 4k RAM :)

            "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

            D Offline
            D Offline
            David1987
            wrote on last edited by
            #12

            Yea, but it's just 4 bytes in this case, less than the code would be :)

            L 1 Reply Last reply
            0
            • D David1987

              Yea, but it's just 4 bytes in this case, less than the code would be :)

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

              No question in this case. Things start to look differently if you need lookup tables for cos or sin. 4k RAM is no fun. Even a simple font with 3 x 5 pixel characters was expensive. Or look at the Bresenham algorithm which avoids multiplication and division like the plague. You always had a choice between very slow (some function using little memory and reasonably precise), slow (some other function, still using little memory but less precise) or fast(tables or other clever structures, memory cost relative to the needed precision).

              "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

              D 1 Reply Last reply
              0
              • L Lost User

                See also: Spaghetti code :)

                "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                R Offline
                R Offline
                RogelioP EX DE HL
                wrote on last edited by
                #14

                CDP1802 wrote:

                See also: Spaghetti code

                After years of the 'never GOTO' doctrine was implemented, I've come across horrible text book examples of spaghetti code in which a GOTO statmenent shined but from its absence. On regards of 8 bit CPUs lacking multiply and division instructions, the Motorola 6809 sports a MULtiply instruction, while the Hitachi 6309 adds an extra multiply plus two division instructions. That made this CPU line stand heads and shoulders above the rest of its generation :cool: -- RP

                L 1 Reply Last reply
                0
                • L Lost User

                  No question in this case. Things start to look differently if you need lookup tables for cos or sin. 4k RAM is no fun. Even a simple font with 3 x 5 pixel characters was expensive. Or look at the Bresenham algorithm which avoids multiplication and division like the plague. You always had a choice between very slow (some function using little memory and reasonably precise), slow (some other function, still using little memory but less precise) or fast(tables or other clever structures, memory cost relative to the needed precision).

                  "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                  D Offline
                  D Offline
                  David1987
                  wrote on last edited by
                  #15

                  I remember employing some self modifying code ( X| ) to save some space, so tight were the limits.. wouldn't do that on a modern PC :)

                  L 1 Reply Last reply
                  0
                  • R RogelioP EX DE HL

                    CDP1802 wrote:

                    See also: Spaghetti code

                    After years of the 'never GOTO' doctrine was implemented, I've come across horrible text book examples of spaghetti code in which a GOTO statmenent shined but from its absence. On regards of 8 bit CPUs lacking multiply and division instructions, the Motorola 6809 sports a MULtiply instruction, while the Hitachi 6309 adds an extra multiply plus two division instructions. That made this CPU line stand heads and shoulders above the rest of its generation :cool: -- RP

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

                    And the Z80 had some as well, if I remember right. But they were very slow and it still was a good idea to avoid them if possible.

                    "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                    D R L 3 Replies Last reply
                    0
                    • D David1987

                      I remember employing some self modifying code ( X| ) to save some space, so tight were the limits.. wouldn't do that on a modern PC :)

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

                      Oh, please do and then write an article about how to fool several layers of memory caches :)

                      "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                      1 Reply Last reply
                      0
                      • L Lost User

                        And the Z80 had some as well, if I remember right. But they were very slow and it still was a good idea to avoid them if possible.

                        "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                        R Offline
                        R Offline
                        RogelioP EX DE HL
                        wrote on last edited by
                        #18

                        CDP1802 wrote:

                        And the Z80 had some as well, if I remember right. But they were very slow and it still was a good idea to avoid them if possible.

                        The hardware multiply was missing on the Z80, programmers had to write up the routines to handle that. For those who this may interest, this is how it was done in the old days [^] :-\ -- RP

                        D 1 Reply Last reply
                        0
                        • L Lost User

                          And the Z80 had some as well, if I remember right. But they were very slow and it still was a good idea to avoid them if possible.

                          "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                          D Offline
                          D Offline
                          David1987
                          wrote on last edited by
                          #19

                          The original didn't, but later version had instructions to multiply the two halves of a register pair together

                          1 Reply Last reply
                          0
                          • L Lost User

                            See also: Spaghetti code :)

                            "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                            H Offline
                            H Offline
                            Henry Minute
                            wrote on last edited by
                            #20

                            CDP1802 wrote:

                            Spaghetti code

                            Have you been sniffing forks again?

                            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus! When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.

                            L 1 Reply Last reply
                            0
                            • H Henry Minute

                              CDP1802 wrote:

                              Spaghetti code

                              Have you been sniffing forks again?

                              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus! When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.

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

                              Only until I discovered that Gladys works here.

                              "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                              1 Reply Last reply
                              0
                              • L Lost User

                                I just pulled this out of MSDN:

                                using System;
                                class SwitchTest
                                {
                                public static void Main()
                                {
                                Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
                                Console.Write("Please enter your selection: ");
                                string s = Console.ReadLine();
                                int n = int.Parse(s);
                                int cost = 0;
                                switch(n)
                                {
                                case 1:
                                cost += 25;
                                break;
                                case 2:
                                cost += 25;
                                goto case 1;
                                case 3:
                                cost += 50;
                                goto case 1;
                                default:
                                Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
                                break;
                                }
                                if (cost != 0)
                                Console.WriteLine("Please insert {0} cents.", cost);
                                Console.WriteLine("Thank you for your business.");
                                }
                                }

                                It has been a long time since I have seen so much 'goto' in one place, especially because I also count in switch statements and 'break'. This makes the question of using goto or not look a bit academic. Edit: In the old days such code would have made much more sense. On an 8 bit CPU without any instructions for multiplication or division all kinds of crazy things were done to avoid having to multiply or divide. But those days are long over...

                                "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                                modified on Wednesday, March 23, 2011 4:58 AM

                                S Offline
                                S Offline
                                Slacker007
                                wrote on last edited by
                                #22

                                CDP1802 wrote:

                                It has been a long time since I have seen so much 'goto' in one place

                                GOTO statements are great when you are programming in VBA. However, I have not used it anywhere else.

                                1 Reply Last reply
                                0
                                • L Lost User

                                  I just pulled this out of MSDN:

                                  using System;
                                  class SwitchTest
                                  {
                                  public static void Main()
                                  {
                                  Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
                                  Console.Write("Please enter your selection: ");
                                  string s = Console.ReadLine();
                                  int n = int.Parse(s);
                                  int cost = 0;
                                  switch(n)
                                  {
                                  case 1:
                                  cost += 25;
                                  break;
                                  case 2:
                                  cost += 25;
                                  goto case 1;
                                  case 3:
                                  cost += 50;
                                  goto case 1;
                                  default:
                                  Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
                                  break;
                                  }
                                  if (cost != 0)
                                  Console.WriteLine("Please insert {0} cents.", cost);
                                  Console.WriteLine("Thank you for your business.");
                                  }
                                  }

                                  It has been a long time since I have seen so much 'goto' in one place, especially because I also count in switch statements and 'break'. This makes the question of using goto or not look a bit academic. Edit: In the old days such code would have made much more sense. On an 8 bit CPU without any instructions for multiplication or division all kinds of crazy things were done to avoid having to multiply or divide. But those days are long over...

                                  "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                                  modified on Wednesday, March 23, 2011 4:58 AM

                                  Mike HankeyM Offline
                                  Mike HankeyM Offline
                                  Mike Hankey
                                  wrote on last edited by
                                  #23

                                  CDP1802 wrote:

                                  Edit: In the old days such code would have made much more sense. On an 8 bit CPU without any instructions for multiplication or division all kinds of crazy things were done to avoid having to multiply or divide. But those days are long over...

                                  We have Co-Processors now...:)

                                  Semper Fi http://www.hq4thmarinescomm.com[^]
                                  www.jaxcoder.com[^] WinHeist - Windows Electronic Inventory SysTem

                                  1 Reply Last reply
                                  0
                                  • R RogelioP EX DE HL

                                    CDP1802 wrote:

                                    And the Z80 had some as well, if I remember right. But they were very slow and it still was a good idea to avoid them if possible.

                                    The hardware multiply was missing on the Z80, programmers had to write up the routines to handle that. For those who this may interest, this is how it was done in the old days [^] :-\ -- RP

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

                                    No bitshift instructions on the z80?

                                    3x12=36 2x12=24 1x12=12 0x12=18

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      And the Z80 had some as well, if I remember right. But they were very slow and it still was a good idea to avoid them if possible.

                                      "I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011

                                      L Offline
                                      L Offline
                                      l a u r e n
                                      wrote on last edited by
                                      #25

                                      i was going to mention that too

                                      "mostly watching the human race is like watching dogs watch tv ... they see the pictures move but the meaning escapes them"

                                      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