&& or ||

This is a discussion on && or || within the C++ Programming forums, part of the General Programming Boards category; Hi Is it possible for someone to explain to me when using the boolen expressions && and || when each ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    62

    && or ||

    Hi

    Is it possible for someone to explain to me when using the boolen expressions && and || when each one should be used I keep making the mistake of using && when it should be || or vice and versa.

    So

    if((answer > 5) && (answer <10))

    or is it

    if((answer > 5) || (answer <10))

    thanks

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Your first if statement evaluate as true if variable answer is a number between 5 and 10.
    Second if statement is true for any numbe.

    || is ismilar to union and && to intersection.

    Check this (just for logic):
    http://www.themathlab.com/geometry/s...tersection.htm

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Thanks for your prompt reply

    I at least completely understand && now.

    Okay I am going to act dum however, it's only way I will finally understand this,

    Why would || be for any number?

    Thank you

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You have ( cond1 || cond 2 ) as a single expression, which could be either true or false. What or ( || ) does is to say, "the expression is true when at least one of cond1 or cond2 is true, and false otherwise." Or, you could say, "the expression is true if cond1 is true, cond2 is true, or both are true, and false otherwise."
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Right I understand

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21