I am studying this code sample:
class Program
{
static void Main(string[] args)
{
int x = 10;
int y = 10;
int generate=0;
string [,] myArrayTable = new string[x, y];
Console.WriteLine("Enter a seek number: ");
string cautat = Console.ReadLine();
for (int i = 0; i < x; i++)
{
for(int j = 0;j < y; j++)
{
myArrayTable[i, j] = (generate++).ToString();
}
}
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
if(cautat.Equals(myArrayTable[i,j]))
{
goto Found;
}
}
}
goto NotFound;
Found:
Console.WriteLine("Numarul a fost gasit");
NotFoun开发者_如何学Pythond:
Console.WriteLine("Numarul nu a fost gasit !");
Console.ReadKey();
}
}
I do not understand why the "Not Found" statement was called and its corresponding message print on console if i enter a seek number like 10, in this case goto: Found statement is executing, so goto: NotFound statement will never be called, but still its corresponding message is printed on console, i do not understand how since in this case program never jumps to this "NotFound" label.
Please if you now give me a hand about this...
Thanks
Eww goto's, I would use and if/else
statement, but if you need goto's:
Found:
Console.WriteLine("Numarul a fost gasit");
goto End;
NotFound:
Console.WriteLine("Numarul nu a fost gasit !");
End:
Console.ReadKey();
I would rewrite this code to avoid the use of goto:
string message;
if (myArrayTable.Cast<string>().Contains(cautat)) {
message = "Found";
} else {
message = "Not found!";
}
Console.WriteLine(message);
It's being called because your code inside of the Found label has nothing to force it to skip over the code int he NotFound label (unless you call goto again, the execution will fall through the label rather than skip it).
That being said, don't use goto
! I'll go as far to say that it's always a bad idea and can be re-written.
In your case, you can add a simple boolean flag to get rid of your goto:
static void Main(string[] args)
{
int x = 10, y = 10;
bool isFound = false;
// Rest of the body
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
if(cautat.Equals(myArrayTable[i,j]))
{
isFound = true;
break;
}
}
if(isFound)
break;
}
if(isFound)
Console.WriteLine("Numarul a fost gasit");
else
Console.WriteLine("Numarul nu a fost gasit!");
Console.ReadKey();
}
Because you just jump to the Found label and proceed to fall through to the Not Found label. You'd need a third label called EndFound and go to it after found.
Found:
Console.WriteLine("Numarul a fost gasit");
goto EndFound;
NotFound:
Console.WriteLine("Numarul nu a fost gasit !");
EndFound:
if you don't want the "Not Found" statments to execute if the "Found" statments execute, use another goto, to skip the NotFound portion. goto jumpts to a section, but that doesn't mean the section won't be executed if it is not jumped to via a goto. Remember, the code executes in a top down fasion, so unless you somehow skip over a peice of code, it will execute.
example:
Found:
Console.WriteLine("Numarul a fost gasit");
goto ReadKey;
NotFound:
Console.WriteLine("Numarul nu a fost gasit !");
ReadKey:
Console.ReadKey();
Because after it jump to Found
, execution just continues to the next line, which happens to be the "not found" console write line. You need to add yet another goto to jump over that (or better yet, redesign this to avoid the gotos entirely)
It is exactly problem like this, that gotos should be avoided.
精彩评论