GOTO, alive and well after all [modified]
-
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
-
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
-
The switch doesn't seem to help here, with just 4 cases of which 3 are collapsible into one (
cost = n * 25
) Why would they do this?? I mean, if they're going to show how switch works, at least make it seem like it might be useful someday, right?For some reason I considered the following as an improvement as well:
switch(n) { case 1: cost += 25; break; case 2: cost += 25; goto case 1; case 3: cost += 25; goto case 2; default: Console.WriteLine("Invalid selection. Please select 1, 2, or 3."); break; }
Where's that morning coffee...
-
The switch doesn't seem to help here, with just 4 cases of which 3 are collapsible into one (
cost = n * 25
) Why would they do this?? I mean, if they're going to show how switch works, at least make it seem like it might be useful someday, right?Contrary to what we are used to from C, C# does not allow you to fall through from one case to the next. Instead you can do this explicity by using goto. This little code sample is supposed to demonstrate this. But I agree, it also demonstrates well, why goto (hidden or not) in most cases is not a good choice.
"I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011
-
For some reason I considered the following as an improvement as well:
switch(n) { case 1: cost += 25; break; case 2: cost += 25; goto case 1; case 3: cost += 25; goto case 2; default: Console.WriteLine("Invalid selection. Please select 1, 2, or 3."); break; }
Where's that morning coffee...
That's not too bad yet, I once had to debug and preferably understand an awesome switch where some cases were the beginning of an epic journey through almost all of the 128 cases.. And not just as a straight path either, but twisting back on itself and then going an other way because some state was changed along the way.
-
The switch doesn't seem to help here, with just 4 cases of which 3 are collapsible into one (
cost = n * 25
) Why would they do this?? I mean, if they're going to show how switch works, at least make it seem like it might be useful someday, right?What's most useful to a developer - a coffee machine :laugh:
-
That's not too bad yet, I once had to debug and preferably understand an awesome switch where some cases were the beginning of an epic journey through almost all of the 128 cases.. And not just as a straight path either, but twisting back on itself and then going an other way because some state was changed along the way.
-
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
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
{+}: -
What's most useful to a developer - a coffee machine :laugh:
-
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:
IF(CoffeeMachine.IsBroken)
{
GOTO You.Home;
} -
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
{+}: -
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
-
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
-
See also: Spaghetti code :)
"I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011
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
-
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
-
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
-
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 :)
-
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
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
-
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
-
See also: Spaghetti code :)
"I just exchanged opinions with my boss. I went in with mine and came out with his." - me, 2011
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.