locked
WHILE with multiple OR condition RRS feed

  • Question

  • Good Day.

    I'm trying to check user input in C# console if it is within specific values , my code is not working , would you please check and advise what is wrong ?                                                                                                                                                        Note: I do not need any other methods to solve it like bool , range ... etc , only I want to know what is the correct to solve it using while . Thanks in advance ..

                

              int x;
                Console.WriteLine("Enter year between 2013 and 2015");
                x = int.Parse(Console.ReadLine());

                while (x !=2013 || x !=2014 || x=!2015)
                {
                    Console.WriteLine("Enter year between 2013 and 2015");
                    x = int.Parse(Console.ReadLine());
                }

     

    Saturday, July 13, 2013 7:58 AM

Answers

  • The while condition is incorrect. Consider what happens when x is 2013:

    x != 2013 is false, x != 2014 is true and x != 2015 is true. You have (false || true || true) and that's true and the loop keeps going even if 2013 was a correct value. What you need is AND, not OR:

    while (x != 2013 && x != 2014 && x != 2015) {...}

    This can also be expressed using OR but you have to change the negations:

    while (!(x == 2013 || x == 2014 || x == 2015)) {...}

    • Proposed as answer by Murtaza Lokhandwala Saturday, July 13, 2013 8:39 AM
    • Marked as answer by MM_ZZ Saturday, July 13, 2013 8:46 AM
    Saturday, July 13, 2013 8:30 AM

All replies

  • The while condition is incorrect. Consider what happens when x is 2013:

    x != 2013 is false, x != 2014 is true and x != 2015 is true. You have (false || true || true) and that's true and the loop keeps going even if 2013 was a correct value. What you need is AND, not OR:

    while (x != 2013 && x != 2014 && x != 2015) {...}

    This can also be expressed using OR but you have to change the negations:

    while (!(x == 2013 || x == 2014 || x == 2015)) {...}

    • Proposed as answer by Murtaza Lokhandwala Saturday, July 13, 2013 8:39 AM
    • Marked as answer by MM_ZZ Saturday, July 13, 2013 8:46 AM
    Saturday, July 13, 2013 8:30 AM
  • For scenarios like this (entering Data, possibly with repeating), I prefer a do...while (inlcudes Mike Danes answer):

    int x;
    
    do {
        Console.WriteLine("Enter year between 2013 and 2015");
        x = int.Parse(Console.ReadLine());
    } while(x != 2013 && x != 2014 && x != 2015)

    With a do...while it will run at least once, but will repeat as often as nessesary.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Saturday, July 13, 2013 9:26 AM
  • Try something a little more like this:

    int x=0;
    while (x !=2013 && x !=2014 && x=!2015)
    {
        Console.WriteLine("Enter year between 2013 and 2015");
        x = int.Parse(Console.ReadLine());
    }
    Also, if they type something not parsable to an integer (like 2013.2 or 1/23/2013 or "Hi, Mom") this will fail.I suggest using int.Tryparse for that.


    I'd rather live with false hope than with false despair.

    Saturday, July 13, 2013 6:48 PM