0
static void Main(string[] args)
{
    double i, j;
    Console.WriteLine("Enter fnum");
    i = Convert.ToDouble(Console.ReadLine());
    start:
    Console.WriteLine("Enter snum");
    j = Convert.ToDouble(Console.ReadLine());

    if(j==0)
    {
        goto start;
    }

    Console.ReadLine();
    Console.WriteLine("Div result is {0}/{1}={2}", i, j, i / j);
}

I'm trying to implement Divide by zero using GOTO in c#. I got the result but here I would like to first calculate fnum/snum and print the result and then if I enter snum zero it should ask me enter snum other than zero.Then the goto start label should start working. But here in this case I was only able to execute fnum/snum by default.

Console.WriteLine("Enter fnum");
i = Convert.ToDouble(Console.ReadLine());
start:
Console.WriteLine("Enter snum");
j = Convert.ToDouble(Console.ReadLine());

Here if I enter snum zero it should take me to start label and ask me "enter second number other zero"

Can any one help me?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
sni
  • 67
  • 1
  • 1
  • 8

2 Answers2

2
if(j==0)
{
    Console.WriteLine("snum cannot be 0!!");
    goto start;
}

It is not recommended to use goto statements or get into the habit of using them.

They tend to increase complexity and create spaghetti code, they also affect readability as you add more and more of them. A bug caused by a goto statement can be tricky to fix. etc etc. others can edit more reasons not to use goto statements in if they wish.

M Y
  • 1,831
  • 4
  • 24
  • 52
2

You absolutely should NOT be using goto. Use a simple while loop instead:

static void Main(string[] args)
{
    double i, j = 0;
    Console.Write("Enter fnum: ");
    double.TryParse(Console.ReadLine(), out i);

    Console.Write("Enter snum: ");
    while (j == 0)
    {
        double.TryParse(Console.ReadLine(), out j);

        if(j==0)
            Console.WriteLine("Enter a number that is not 0");
    }


    Console.ReadLine();
    Console.WriteLine("Div result is {0}/{1}={2}", i, j, i / j);
}

Additionally, I have modified your code to use Double.TryParse instead of Convert.ToDouble(), as is generally recommended. With it, you can implement a check to make sure that the user has actually entered a number. I have also changed Console.WriteLine to Console.Write for the inputs, which doesn't affect the flow of the program at all, but to me makes it better aesthetically.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • This is not a "simple" `while` loop. A loop which by its constant condition pretends to be infinite, but actually has a `break;` in the middle, is a complex loop. A simple loop would suffice here by moving a bit of code into a function. –  Jul 08 '17 at 17:08
  • @hvd I was using the word "simple" to denote that the loop is easy to write and to understand. However, you're right that a simple loop would suffice, even without a function, as I have done in the edit. – stelioslogothetis Jul 08 '17 at 17:12
  • You've forced yourself to duplicate the loop condition, which a separate function would've avoided, but given how trivial that loop condition is, I suppose that's not too bad. –  Jul 08 '17 at 17:15
  • can I apply try parse to CHAR ?? – sni Jul 08 '17 at 19:15
  • @sni Yes, you can. – stelioslogothetis Jul 08 '17 at 19:57
  • @stybl can u give me an example ? – sni Jul 08 '17 at 22:47
  • ages[i] = int.TryParse(Console.ReadLine(), out ages); What is wrong in the above statement ? @stybl – sni Jul 08 '17 at 22:49
  • @sni `ages` is an array. Do `out ages[i]`. Extra piece of advice: don't use arrays. Use the built-in [List](https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) instead. – stelioslogothetis Jul 08 '17 at 23:29