Thread: Help with tutorial

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Help with tutorial

    Hello all,

    New user to C/C++ here and I have been going through the tutorials on this site to better understand it. I am confused by the following section though and am hoping someone will help me understand it.

    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)
    To specify, what I do understand is the operators themselves, but don't appreciate what is being performed in each of the above cases. Meaning, I am aware of how comparison operators can be used to check the value of variables, but why does the statement [NOT (1 OR 0)], by itself, return anything at all? I guess I'm confused as to why any of the above statements (A, B, or C) aren't completely useless, and why they conclude with a value. Hopefully that makes sense.

    Thanks in advance for any help.
    Last edited by alaric24; 06-19-2006 at 04:48 AM.

  2. #2
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    I think you need to look at how boolean algebra works in C and C++. Anything non zero is regarded as true (positive and negative values). Zero is regarded as false. Therefore, when applying boolean operators (&& ||), just consider each value as true if non-zer and false if zero.

    So, for A
    NOT(TRUE OR FALSE)
    =NOT (TRUE)
    =FALSE

    I am not going to go through them all. Hope that helps.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should read up on boolean operators, in a book or maybe somewhere in here: http://www.cprogramming.com/tutorial.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    All the C++ boolean operators return values. This is actually to simplify things. The operators can then be regarded as ordinary math operators like + and - which take arguments and return values. When you learn classes and operator overloading you will find returning values from boolean operators very useful. In fact, if(X) will just check if X does not evaluate to zero, which is why you can use if(x) instead of if(x != 0) in code.

    If && didn't return anything, how could you do this?
    if(x || (y && z))

    Think about it.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM