-2

I want to use multiple conditions in while loop:

Console.WriteLine("Select an action to perform\n");
int n1 = Convert.ToInt32(Console.ReadLine());
do
{
    Console.WriteLine("Insert a valid method\n");
    n1 = Convert.ToInt32(Console.ReadLine());
}
while ((n1 == 1) || (n1 == 2));

Console.WriteLine(n1);
Console.ReadKey();

In here I want to check the value n1 is equals to 1 or 2. Until the user enter n1 or 2 this should loop. The thing is I can get this to working if im using just one condition but cant get this working when there are 2 conditions. Also how to equal these values to another string?

Ex:

while ((n1 == "one") || (n1 =="two"))

I think theres something I didnt understand about || (OR) operator. I read few solutions yet I couldnt figure it out.

lasith92
  • 17
  • 6
  • 1
    Your code is doing what is expected. As long as `n1` equals 1 or `n2` equals 2 it will continue looping. If you want to stop looping once the user either entered 1 or 2 then you need to negate your expression. Either `n1 != 1 || n1 != 2` or `!(n1 == 1 || n1 == 2)` – MindSwipe Jul 11 '19 at 05:51
  • Put the values you are interested in a `HashSet` **before** the loop. Then use `Contains`. That way you can have a single `Contains` check, regardless of the number of values you want to check. – mjwills Jul 11 '19 at 05:58
  • 1
    `n1 != 1 || n1 != 2` and `!(n1 == 1 || n1 == 2)` are not equivalent. `n1 != 1 && n1 != 2` and `!(n1 == 1 || n1 == 2)` are – Sohaib Jundi Jul 11 '19 at 06:11

4 Answers4

1

You want this to loop until the user enters 1 or 2.

But in the code, you asked it to loop when the user enters 1 or 2.

So, instead of

while ((n1 == 1) || (n1 == 2));

you should write

while (!(n1 == 1 || n1 == 2));

Remaining part of the code is good, it'll work as expected.

No need to check for strings "one" and "two" as you're converting input to Int32 in line 6. Convert.ToInt32(Console.ReadLine()) can't convert string "one" to Integer '1'.

Light Yagami
  • 961
  • 1
  • 9
  • 29
  • Thank you !!! i tried doing while((n1 != 1) || (n1 != 2)) and it didnt work for some reason. idk. And i just wanted to know what if i want to equal this to a string. – lasith92 Jul 11 '19 at 06:35
  • the string thing is more like a seperate question. rather than equaling to a int value how to check this with another string value? String s1 = Console.ReadLine(); while (!(s1 == "a" || s1 == "b")); – lasith92 Jul 11 '19 at 06:53
  • @lasith92 Use `Equals`. For example, you can write `String s1 = Console.ReadLine(); while (!(s1.Equals("a") || s1.Equals("b")));` Is this what you wanted? – Light Yagami Jul 11 '19 at 06:56
1

You are confusing do....while with do...until (the latter of which is not a c# construct).

If you want an "until" sort of logic loop, use while(true) and use an if statement with the until condition to break the loop.

Console.WriteLine("Select an action to perform\n");
int n11 = Convert.ToInt32(Console.ReadLine());
while (true)
{
    Console.WriteLine("Insert a valid method\n");
    n1 = Convert.ToInt32(Console.ReadLine());
    if ((n1 == 1) || (n1 == 2)) break;
}

An alternative is to keep the while construct but invert the logic. This isn't my favorite answer because it may cause the logic to become less clear.

Console.WriteLine("Select an action to perform\n");
int n11 = Convert.ToInt32(Console.ReadLine());
do
{
    Console.WriteLine("Insert a valid method\n");
    n1 = Convert.ToInt32(Console.ReadLine());
}
while ((n1 != 1) && (n1 != 2));
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • while ((n1 != 1) && (n1 != 2)); can you explain why you've used && here? And that clarified the issue. – lasith92 Jul 11 '19 at 06:41
  • 1
    It means "continue only if the user didn't enter a 1 and didn't enter a 2." When you flip Boolean logic, `or`s become `and`s. – John Wu Jul 11 '19 at 06:50
0

Your current code is this:

while ((n1 == 1) || (n1 == 2))

This code states that the loop should be repeated / continue if n1 == 1 or n1 == 2. What you actually want is to repeat where n1 isn't equal to either of them:

while (n1 != 1 && n1 != 2)

This states that if n1 isn't 1, and n1 isn't 2, then it should loop. If this statement isn't true, the loop will exit and your code will move on to Console.WriteLine(n1);.

Note that n1 != 1 && n1 != 2 is the opposite of n1 == 1 || n1 == 2.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • && will need both to be true right? If i want either n1=1 or 2 it should exit.... if i want just 1 to be true it should be && or || operater? – lasith92 Jul 11 '19 at 06:46
  • Yes, both conditions will have to be true. That is to say: if `n1 != 1` returns false, and `n1 != 2` returns false, the will exit, otherwise it will go back to `Console.WriteLine("Insert a valid method\n");`. Remember `!=` means "not equal", so in words it reads "if n1 is not equal to 1, and n1 is not equal to 2, continue the loop". – ProgrammingLlama Jul 11 '19 at 06:55
0

I suggest using infinite loop while(true) {...} which we break on valid input (please, note int.TryParse instead of Convert.ToInt32 - we don't want exception on input like "bla-bla-bla"):

// Let's be nice and show expected user response - 1 or 2
Console.WriteLine("Select an action to perform (1, 2)\n");

int n1;

while (true) {
  // If user input is a valid i.e.
  //   1. Input is valid integer - int.TryParse returns true
  //   2. User input is either 1 or 2
  // we break the loop and start business logic; otherwise keep asking  
  if (int.TryParse(Console.ReadLine(), out n1) && ((n1 == 1) || (n1 == 2)))
    break;

  // Again, let's be nice and help user
  Console.WriteLine("Insert a valid method (either 1 or 2)\n"); 
}

Console.WriteLine(n1);
Console.ReadKey();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215