Thread: OR or AND

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    OR or AND

    Well which of the followinf is correct for the below statement and why


    i want to make sure that the user does not enter the key a b c and d. In case he enters these he should get an error messgae


    if(input!='a' && input!='b' && input!='c' && input!='d')
    cour<<"Error";

    OR

    if(input!='a' || input!='b' || input!='c' || input!='d')
    cour<<"Error";





    well logicaly the furst one seems to be correct saying that if the user enters A or B or C or D give the error message.. But this statement does not work.. The second statement works...



    I know it is got to do with the OR gate and AND gates.. But please clarify

  2. #2
    In the first you say that if the user does not press a,b,c or d, he will get an error.
    Use the second one.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I say: use none... both are not correct.

    In the first you say if the use does not press a, b, c or d he or she will het an error.
    In the second you say always show an error.

    Third statement: if user enter a OR b OR c OR d then show error
    Code:
    if(input=='a' || input=='b' || input=='c' || input=='d') 
      cout<<"Error" << endl;

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Monster is correct...... just read it to yourself and remember

    && - AND
    || - OR

    So the code monster put up reads, "If input is equal to a or b or c or d, output error"

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: OR or AND

    if(input!='a' || input!='b' || input!='c' || input!='d')
    cour<<"Error";
    This will always be true. Using ||, the whole expression will be true if at least one of the parts are true.

    Assume Input has the value 'a'.
    Then Input != 'b' will be true, since a != b
    Assume that Input has any value except 'a'.
    Then Input != 'a' will be true according to the assumption above.

    Conclusion: The expression will always be true, since one of Input != 'b' and Input != 'a' will always be true.


    (I'm learning Logics in school, this was a nice excercise )
    Last edited by Magos; 06-12-2002 at 09:49 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >i want to make sure that the user does not enter the key a b c
    >and d. In case he enters these he should get an error messgae

    In logic it often helps to describe a problem in a different way. You want an error message appear when the user has entered a or b or c or d. In other words: if the user enters a or b or c or d, then an error message must be printed.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    367
    Even I consider this as easy!

    The only thing about these logic operators which I can consider as a problem is where to place the !, whenever you want to invert an expression.

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    If the expression is

    a && b

    the the inversion is

    !(a && B)

    but using the rules of logic, it is the same as

    !a || !b

    So you can place the inverter wherever you want, but always take good care of the used logic operators. If you're not used to the laws of logic, a thruth table is always very useful.

Popular pages Recent additions subscribe to a feed