none
Contine till get result - Console application RRS feed

  • Question

  • Hello,

    I am writing sample console application and in my application i have function which has logic an will result Boolean based on success. it ill ask for the two inputs which will be passed to function. what i need is, is there any way that till the function returns true, the application should not get stopped and should keep allow to enter the input. for example, inside the function i have multiple if condition and on the else condition i have code to return true case logic. when i run the application and it i give the two inputs and if it's not satisfy , i should get allowed to enter the input again. the application should not get closed. how do i achieve this. any suggestion would highly appreciated. thank you. any sample code if possible.

         class Program
            {
                static void Main(string[] args)
                {
    
                    Console.WriteLine("Enter the Input");
                    bool Result = TestCalculation(Console.ReadLine(), Console.ReadLine());
                   
                }


    loving dotnet

    Monday, May 27, 2019 12:45 AM

Answers

  • Greetings B2A.

    A 'while' loop is the usual way to do this sort of thing.

            // Like this.
            class Program
            {
                static void Main(string[] args)
                {
    
                    Console.WriteLine("Enter the Input");
                    bool Result = false;
                    while(!Result)
                    {
                       Result = TestCalculation(Console.ReadLine(), Console.ReadLine());
                    }
                   
                }
             }
    
    
            // Or even like this.
            class Program
            {
                static void Main(string[] args)
                {
    
                    Console.WriteLine("Enter the Input");
                    while(!TestCalculation(Console.ReadLine(), Console.ReadLine()));
                   
                }
            }

    Monday, May 27, 2019 12:52 AM
  • Hi   Born2Achieve,

    Thank you for posting here.

    As Ante Meridian suggested, 'while' loop is a suitable way , or you can also use ‘goto’ to do this:

       static void Main(string[] args)
            {
                CC:  Console.WriteLine("Enter the Input");  // 'CC' is a custom label
                bool Result = TestCalculation(Console.ReadLine(), Console.ReadLine());
                if (!Result)
                {
                    goto CC;
                }
            }
    

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Born2Achieve Tuesday, May 28, 2019 12:36 AM
    Monday, May 27, 2019 8:40 AM
    Moderator

All replies

  • Greetings B2A.

    A 'while' loop is the usual way to do this sort of thing.

            // Like this.
            class Program
            {
                static void Main(string[] args)
                {
    
                    Console.WriteLine("Enter the Input");
                    bool Result = false;
                    while(!Result)
                    {
                       Result = TestCalculation(Console.ReadLine(), Console.ReadLine());
                    }
                   
                }
             }
    
    
            // Or even like this.
            class Program
            {
                static void Main(string[] args)
                {
    
                    Console.WriteLine("Enter the Input");
                    while(!TestCalculation(Console.ReadLine(), Console.ReadLine()));
                   
                }
            }

    Monday, May 27, 2019 12:52 AM
  • Hi   Born2Achieve,

    Thank you for posting here.

    As Ante Meridian suggested, 'while' loop is a suitable way , or you can also use ‘goto’ to do this:

       static void Main(string[] args)
            {
                CC:  Console.WriteLine("Enter the Input");  // 'CC' is a custom label
                bool Result = TestCalculation(Console.ReadLine(), Console.ReadLine());
                if (!Result)
                {
                    goto CC;
                }
            }
    

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Born2Achieve Tuesday, May 28, 2019 12:36 AM
    Monday, May 27, 2019 8:40 AM
    Moderator
  • thanks a lot guys

    loving dotnet

    Tuesday, May 28, 2019 12:35 AM
  • No worries, but I must sound a note of warning.

    In many workplaces it's standard practice for developers who use goto to be shot at dawn. Therefore, use only with extreme caution.

    Tuesday, May 28, 2019 12:51 AM