The :
is used in several contexts and each one can have a difference of meaning.
In this context it is an indicator of completion of a label.
A label is a symbol marking any position in the code (or a more specific position within a command switch
, but not in the case presented). It is used for in some command goto
can specify where the code flow should go. This symbol is no different than a variable name or function name.
The goto
is an unconditional execution flow deviation command, so it switches to the location of the label, whenever the execution goes through there. Its usefulness is limited and heavily questioned, so you should avoid until you have a good reason to use.
Not using the goto
there’s no point in having the label. In the examples they serve for nothing and can be removed. But if leaving does not cause any harm, except the fact that another programmer will find very strange this in the code.
Once compiled this disappears, it is only indicative of the code. Instead of the label of goto
the address of the location of the code where the label, in case the FOO
.
The reason for the error in the second example is that there is no instruction after the label, so there’s no actual address there, there’s no way label point to no address because it is the end of the function. If you need to close the function use the return
.
A more real use, but not a good example because it has old, bad things and the use of goto
is unnecessary (taken from the documentation):
public class GotoTest1 {
static void Main() {
int x = 200, y = 4;
int count = 0;
string[,] array = new string[x, y];
// Initialize the array.
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
array[i, j] = (++count).ToString();
// Read input.
Console.Write("Enter the number to search for: ");
// Input a string.
string myNumber = Console.ReadLine();
// Search.
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (array[i, j].Equals(myNumber)) {
goto Found;
}
}
}
Console.WriteLine($"The number {myNumber} was not found.");
goto Finish;
Found:
Console.WriteLine($"The number {myNumber} is found.");
Finish:
Console.WriteLine("End of search.");
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Without goto
and improved (can improve more):
using static System.Console;
public class GotoTest1 {
static void Main() {
int x = 200, y = 4;
var count = 0;
var array = new string[x, y];
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
array[i, j] = (++count).ToString();
Write("Enter the number to search for: ");
var myNumber = ReadLine();
bool found = Search(array, x, y, myNumber);
if (found) WriteLine($"The number {myNumber} is found.");
else WriteLine($"The number {myNumber} was not found.");
WriteLine("End of search.");
}
private static bool Search(string[,] array, int x, int y, string myNumber) {
for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) if (array[i, j] == myNumber) return true;
return false;
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I’ve been using C for years and I’ve never wondered about it, great.
– Brewerton Santos