0

I am trying to use OR condition in C# in WHILE loop, but it doesn't work :( if I do separate conditions without OR conditions then it works!

it works like this:

while (myAnswer.ToLower() != "yes")
{
    Console.Write("Please write YES or NO!: ");
    myAnswer = Console.ReadLine();
}

but if I try to add additional conditions, doesnt metter its AND or OR, it sends me to infinite loop. For example this doesnt work:

while ((myAnswer.ToLower() != "yes") || (myAnswer.ToLower() != "no"))
{
    Console.Write("Please write YES or NO!: ");
    myAnswer = Console.ReadLine();
}

any ideas?

Lado
  • 189
  • 7

1 Answers1

1

If you want exit from your while if user writes YES or NO, your condition will be:

In other words, you repeat the question until the response will be YES or NO.

while ((myAnswer.ToLower() != "yes") && (myAnswer.ToLower() != "no"))
{
    Console.Write("Please write YES or NO!: ");
    myAnswer = Console.ReadLine();
}
Joe Taras
  • 15,166
  • 7
  • 42
  • 55