Daniel Espinel
Daniel Espinel

Reputation: 53

Stuck on infinite if loop

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.

Upvotes: 5

Views: 166

Answers (5)

M.kazem Akhgary
M.kazem Akhgary

Reputation: 19149

If its hard to imagine

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

Use DeMorgan's law to expand. replace != with ==. and || with &&. put a ! before expression.

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

Now you see the response must be equal to N and Y and n and y at the same time. Thus its always false statement and negation before it makes it always true.

So now you understand you should put OR instead of AND. then use demorgan law to simplify again.

!(response == "N" || response == "Y" || response == "n" || response == "y") <=>
response != "N" && response != "Y" && response != "n" && response != "y"

Upvotes: 0

swapnil chandankar
swapnil chandankar

Reputation: 87

Your code should be like this,try to use && instead of ||.

    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");
    }

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186793

The immediate cause of the misbehaivour is wrong boolean operator, you want && insetead of ||; you can also combine if and do..while into while..do:

response = Console.ReadLine();

while (response != "N" && response != "Y" && response != "n" && response != "y") {
  WriteLine("Please reply with Y or N");
  response = Console.ReadLine();
}

the next step is to put all the possible responses into a collection:

  Dictionary<String, Boolean> expectedResponses = new Dictionary<String, Boolean>() {
    {"Y", true},
    {"y", true},
    {"N", false},
    {"n", false},
  };

  ...

  response = Console.ReadLine();

  while (!expectedResponses.ContainsKey(response)) {
    WriteLine("Please reply with Y or N");
    response = Console.ReadLine();
  }
  ...
  if (expectedResponses[response]) {
    // user said "yes"
  }
  else {
    // user said "no"
  } 

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172488

You need to use && instead of ||

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

Upvotes: 1

fatihk
fatihk

Reputation: 7919

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

Upvotes: 2

Related Questions