Question

I am really confused on loops; I am new to C# (I have only ever worked with assembly on 16 and 32 bit micro controllers).

any way I have this code:

    {


        {
            Console.WriteLine("Enter the first number");
            Decimal iNumber1;
            if (!Decimal.TryParse(Console.ReadLine(), out iNumber1)) { Console.WriteLine("Enter a number dummy"); Console.Read(); return; }
            Decimal x = iNumber1;
            Console.WriteLine("Enter the second number");
            Decimal y = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter 1 for addition");
            Console.WriteLine("Enter 2 for subtration");
            Console.WriteLine("Enter 3 for multification");
            Console.WriteLine("Enter 4 for divition");
            int z = Convert.ToInt32(Console.ReadLine());
            switch (z)
            {
                case 1:
                    Console.WriteLine(x + y);
                    break;
                case 2:
                    Console.WriteLine(x - y);
                    break;
                case 3:
                    Console.WriteLine(x * y);
                    break;
                case 4:
                    Console.WriteLine(x / y);
                    break;
                default:
                    break;

            }
        }
        Console.ReadLine();
    }
}

}

This is just a simple console app calculator; but I want to be able to loop back to the start if a number isn't entered.

Was it helpful?

Solution

This will allow you to run until Esc is pressed.

 while (Console.ReadKey(true).Key != ConsoleKey.Escape)
 {
  // your code
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top