Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck on infinite if loop

Tags:

c#

I am working on a c# console dice sim lab for my class in Visual Studio 2015. I broke my program to three if loops depending on the user's response. I have one dedicated as a do while loop if the user doesn't input a valid response. I am stuck in that loop for some reason and can't get out. On top of that, since it's to tell the user to input a valid response, it's the first if statement. Because of that, even if I input "y", "Y", "n", or "N" it still initializes. Here is the part in question.

        // get response from user
        response = ReadLine();

        // loop starts if user does not reply with valid response in order to retrieve a valid response
        if (response != "N" || response != "Y" || response != "n" || response != "y")
        {
            do
            {
                WriteLine("Please reply with Y or N");
                response = ReadLine();
            }
            while (response != "N" || response != "Y" || response != "n" || response != "y");
        }

I am using the or operator, so I don't understand why it's looping the way it is.

like image 1000
Daniel Espinel Avatar asked Sep 27 '22 08:09

Daniel Espinel


2 Answers

response != "N" || response != "Y" || response != "n" || response != "y"

should be

response != "N" && response != "Y" && response != "n" && response != "y"

Because you should quit the loop if you hit one of the valid responses

like image 193
fatihk Avatar answered Oct 03 '22 14:10

fatihk


You need to use && instead of ||

response != "N" && response != "Y" && response != "n" && response != "y"
like image 28
Rahul Tripathi Avatar answered Oct 03 '22 13:10

Rahul Tripathi