C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Part 1: TestGotoNestedLoops contains 3 nested loops. The first loop iterates through numbers [0, 9], as do the two inner loops.
ForPart 2: In this loop, a condition is checked that causes the loop to exit using the goto keyword.
IfPart 3: With a label, we indicate where a goto-statement should transfer control.
C# program that uses goto, nested loops
using System;
class Program
{
static void Main()
{
Console.WriteLine("RESULT: " + TestGotoNestedLoops());
}
static int TestGotoNestedLoops()
{
// Part 1: 3 nested loops.
int dummy = 0;
for (int a = 0; a < 10; a++)
{
for (int y = 0; y < 10; y++) // Run until condition.
{
// Part 2: use if-statement in loop.
// ... Then goto.
for (int x = 0; x < 10; x++) // Run until condition.
{
if (x == 5 && y == 5)
{
goto Outer;
}
}
dummy++;
}
// Part 3: the goto label.
// ... Control ends up here.
Outer:
continue;
}
return dummy;
}
}
Output
RESULT: 50
Info: This code uses a flag variable named "ok". It sets "ok" to false and breaks when the exit condition is reached.
True, FalseC# program that uses break, for-loop
using System;
class Program
{
static void Main()
{
Console.WriteLine("BREAK: {0}", BreakMethod());
}
static int BreakMethod()
{
int dummy = 0;
for (int a = 0; a < 10; a++)
{
for (int y = 0; y < 10; y++) // Run until condition.
{
bool ok = true;
for (int x = 0; x < 10; x++) // Run until condition.
{
if (x == 5 && y == 5)
{
ok = false;
break;
}
}
if (!ok)
{
break;
}
dummy++;
}
continue;
}
return dummy;
}
}
Output
BREAK: 50
Syntax: There is a special syntax for this construct: it requires goto case or goto default. We examine this syntax form.
GetPrice: GetPrice() performs 3 logical steps, all defined in the switch-statement. We often find switches in single methods like this one.
Note: The method receives the ID of an item. If the ID is equal to 1000, the base price is increased by 10.
And: After this occurs, this ID is treated like a price of 100. It goes to that case.
C# program that uses goto statement in switch
using System;
class Program
{
static void Main()
{
Console.WriteLine(GetPrice(1000));
Console.WriteLine(GetPrice(-1));
Console.WriteLine(GetPrice(int.Parse("100")));
}
static int GetPrice(int id)
{
//
// Default price is 5.
//
int price = 5;
//
// When id is 1000, add 10 to price before multiplying.
//
switch (id)
{
case 1000:
price += 10; // <-- Could get from another method
goto case 100;
case 100:
return price * 10;
default:
return price;
}
//
// 1.
// First, if ID is 1000, add ten to default price.
//
// 2.
// If ID is 1000 or 100, multiply price by 10 and return it.
//
// 3.
// If ID is anything else, return price of 5.
//
}
}
Output
150
5
50
C# program that shows goto error
using System;
class Program
{
static void Main()
{
goto BIRD;
Console.WriteLine("BIRD");
}
}
Output
Error CS0159
No such label 'BIRD' within the scope of the goto statement
C# program that shows goto warning
using System;
class Program
{
static void Main()
{
BIRD:
Console.WriteLine("BIRD");
}
}
Output
Warning CS0164
This label has not been referenced
But: In certain cases we may benefit from goto in switch when trying to reduce method size. A benchmark shows this effect.
Method Size TestInfo: The greatest benefit of switches on ints or non-string constants here is that the low-level MSIL jump statement is used.
Int, uintSwitch EnumIL: switchAnd: New options, like methods, are clearer. With them, programs are less likely to deteriorate into unfathomable messes.
Quote: The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program (Edsger W. Dijkstra).
Quote: Formally, the goto is never necessary, and in practice it is almost always easy to write code without it.... It does seem that goto statements should be used rarely, if at all (The C Programming Language).