Question how to exit from the loop in this code

adityavarma999

New member
Joined
Mar 29, 2012
Messages
3
Programming Experience
Beginner
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Console1
{
    class Arithmetic
    {
        static void Main(string[] args)
        {
            int num1,num2,choice;
            int add,mul,sub;

            float div;

            while (true)
            {
            Console.WriteLine("Enter the Operation you want to perform");
            Console.WriteLine("1. Addition");
            Console.WriteLine("2. Subtraction");
            Console.WriteLine("3. Multiplication");
            Console.WriteLine("4. Division");
            Console.WriteLine("5. Exit the Loop");
            choice= Convert.ToInt32( Console.ReadLine());
          
                switch(choice)
                {
                    case 1:
                        
                        Console.WriteLine("Enter Value of Number1");
                        num1 =  Convert.ToInt32( Console.ReadLine());
                        Console.WriteLine("Enter Value of Number2");
                        num2 =  Convert.ToInt32( Console.ReadLine());
                        add = num1 + num2;
                        Console.WriteLine("Addition Result of {0} and {1} is {2} ",num1,num2,add);
                        break;

                    case 2:
                        
                        Console.WriteLine("Enter Value of Number1");
                        num1 =  Convert.ToInt32( Console.ReadLine());
                        Console.WriteLine("Enter Value of Number2");
                        num2 =  Convert.ToInt32( Console.ReadLine());
                        sub = num1 - num2;
                        Console.WriteLine("Subtraction Result of {0} and {1} is {2} ", num1, num2, sub); break;
                    case 3:
                        
                        Console.WriteLine("Enter Value of Number1");
                        num1 =  Convert.ToInt32( Console.ReadLine());
                        Console.WriteLine("Enter Value of Number2");
                        num2 =  Convert.ToInt32( Console.ReadLine());
                        mul = num1 * num2;
                        Console.WriteLine("Multiplication Result of {0} and {1} is {2} ", num1, num2, mul); break;
                    case 4:
                        
                        Console.WriteLine("Enter Value of Number1");
                        num1 =  Convert.ToInt32( Console.ReadLine());
                        Console.WriteLine("Enter Value of Number2");
                        num2 =  Convert.ToInt32( Console.ReadLine());
                        div = (float)num1 / num2;
                        Console.WriteLine("Division Result of {0} and {1} is {2} ", num1, num2, div); break;
                    

                }
            }
            
         }

    }
}
 
Last edited by a moderator:
Firstly, I have edited your post to use the proper tags for code formatting. As you can see, it's much easier to read now. Please format all code snippets the same way in future.

Secondly, you are missing a 'break' statement in all but your first case so that code won't even compile. C# requires a 'break' statement in every case unless a case is completely empty, which means that that case will fall through to the next and they will both do exactly the same thing thing.

Thirdly, your cases are very bad because all four of them contain the same first four lines. Any time you repeat code you should be looking for ways to refactor it. Doing so in your case is very easy.

As for your actual question, you could use an 'if' statement first. Test whether the user entered 5 first and, if they did, exit the loop; otherwise, prompt the user to enter the two numbers once and once only, then use a 'switch' statement to determine which operation to perform on those two numbers.

Finally, it is dangerous to use Convert.ToInt32 in cases like that. If this is a homework assignment or the like and you have been told that you can assume that all input will be valid then it's fair enough but that would not be safe in a real application. If the user entered a value that could not be converted to an int then your app would crash. The correct option would be to use int.TryParse to first validate the input and, if it passes, convert it. That will allow you to inform the user and/or whatever else is appropriate under the circumstances.
 
You can do something like this

C#:
            int num1, num2, choice;
            int add, mul, sub;

            float div;

[COLOR=#ff0000]            bool rc = true;
            while (rc)
            //while (true)
[/COLOR]            {

And for your case 5 (exit)

C#:
[COLOR=#ff0000]                     case 5:
                        rc = false;
                        break;
[/COLOR]

// OOPS: as a newbie, I'm reviving old threads :(
 
Last edited:
It's easy, enter after your switch code this:
C#:
                Console.WriteLine("If you want exit enter 1, else enter enother number:");
                int br = Convert.ToInt32(Console.ReadLine());
                if (br == 1) break;
 
Last edited:
Back
Top Bottom