[Help] Tutorial : If Statements

This is a discussion on [Help] Tutorial : If Statements within the C++ Programming forums, part of the General Programming Boards category; Hey all, greeting from Singapore 16M. I'm just started reading the tutorials today and I'm really interested to C++. I ...

  1. #1
    Registered User hackerkts's Avatar
    Join Date
    Feb 2006
    Location
    Singapore
    Posts
    5

    [Help] Tutorial : If Statements

    Hey all, greeting from Singapore 16M. I'm just started reading the tutorials today and I'm really interested to C++.
    I don't understand Boolean operators, 'NOT', 'AND' and 'OR'.
    Can someone help me out this part ?

    Thanks,
    hackerkts

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Post your code, comment on any confusion or compiler error(s)/warning(s). Visit the FAQ.
    Last edited by Dave_Sinkula; 02-11-2006 at 09:20 PM. Reason: Backfilled a link to the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Think of the logical operators just as you would with the same words in the english language.

  4. #4
    Registered User hackerkts's Avatar
    Join Date
    Feb 2006
    Location
    Singapore
    Posts
    5
    Thanks for the reply, I don't the whole part for Boolean, how do I know the result is 0 or not ?
    Just like the example given in the tutorial
    Code:
    A. !( 1 || 0 )         ANSWER: 0	
    B. !( 1 || 1 && 0 )    ANSWER: 0 (AND is evaluated before OR)
    C. !( ( 1 || 0 ) && 0 )  ANSWER: 1 (Parenthesis are useful)
    Thanks,
    hackerkts

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Here's a simple truth table:

    0 && 0 = 0
    0 && 1 = 0
    1 && 0 = 0
    1 && 1 = 1

    0 || 0 = 0
    0 || 1 = 1
    1 || 0 = 1
    1 || 1 = 1

    !0 = 1
    !1 = 0

  6. #6
    Registered User hackerkts's Avatar
    Join Date
    Feb 2006
    Location
    Singapore
    Posts
    5
    >.< Do you mind giving me more details ? Sorry, this my 1st time learning on C++ need more times to understand basic stuff. Eh I mean the example you given can you indicate what does those means ?
    0 && 0 = 0 //0 and 0 equal FALSE
    Hope I didn't get the meaning wrong.

    Edit : Hey I think I got what it means, thanks man for your simple table. *Jumping with joy*

    Thanks,
    hackerkts
    Last edited by hackerkts; 02-11-2006 at 09:58 PM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In C++, 0 evaluates to false, and anything non-zero, like 1, evaluates to true.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    The AND operator says that if any part (the left value or right value) evaluates to false, then the whole statement must be false. The OR operator says that if any part (the left value or right value) evaluates to true, then the whole statement must be true.

    Let's put it this way:

    Say your mom gives you two chores to do... "Rake the leaves AND mow the lawn." If you only rake the leaves, you still haven't done your chores. If you only mow the lawn, you still haven't done your chores. You must do both of them in order to complete your chores. Now lets say she was giving you an option... "Clean the counters OR mop the floor." If you Clean the counters, you've done your chore. If you mop the floor you've done your chore. If you do both, you've still done your chore and then some. If you do neither, obviously you haven't done your chore.

    The NOT operator simply just flips the truth of the statement around. NOT TRUE equals FALSE and NOT FALSE equals TRUE and NOT (TRUE && TRUE || FALSE && TRUE) equals FALSE because the interior evaluates TRUE.

    The order of operations as far as these go are in the order: NOT, AND, OR. If you take that last statement I wrote, NOT (TRUE && TRUE || FALSE && TRUE), and remove the parenthesis, NOT TRUE && TRUE || FALSE && TRUE, it should properly be read as this:

    (NOT TRUE) && TRUE || FALSE && TRUE

    or

    FALSE && TRUE || FALSE && TRUE

    or

    FALSE || FALSE

    equals

    FALSE


    Any questions?
    Last edited by SlyMaelstrom; 02-12-2006 at 12:27 AM.
    Sent from my iPad«

  9. #9
    Registered User hackerkts's Avatar
    Join Date
    Feb 2006
    Location
    Singapore
    Posts
    5
    Nope, thank you sir !
    That's a nice example too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ tutorial 2 (if statements) problem
    By Jester P in forum C++ Programming
    Replies: 7
    Last Post: 07-23-2008, 01:14 PM
  2. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 06:38 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. If Statements Tutorial Code Won't Compile.
    By civilwarsearch in forum C++ Programming
    Replies: 6
    Last Post: 12-14-2003, 01:43 AM
  5. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 01:49 PM

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