How goto statement works in this example?

39,791

Solution 1

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();

Solution 2

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);

Solution 3

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();
}

Solution 4

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:

Solution 5

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(); 
Share:
39,791
Mircea
Author by

Mircea

Updated on June 03, 2020

Comments

  • Mircea
    Mircea almost 4 years

    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");
    
            NotFound:
             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