This C# code is simple:
The scenario of this code:
When the word is matched it results unexpected output:
The word is found
The word is not found
The value of found is: True
When the word is not matched it results the expected output:
The word is not found
The value of found is: False
static void Main()
{
string[] words = { "casino", "other word" };
Boolean found = false;
foreach (string word in words)
{
if (word == "casino")
{
found = true;
goto Found;
}
}
if(!found)
{
goto NotFound;
}
Found: { Console.WriteLine("The word is found"); }
NotFound: { Console.WriteLine("The word is not found"); }
Console.WriteLine("The value of found is: {0}", found);
Console.Read();
}
The goto statement just moves the execution to that point. So in you case the Found is being executed then the next line, NotFound, is being executed.
To fix this using goto you would have to have a 3rd goto which is something like Continue.
I would much prefer something like this however:
Or even better!