help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question on Logical OR (||)


From: Robert Heller
Subject: Re: Question on Logical OR (||)
Date: Sun, 24 Jun 2012 22:00:49 -0500

At Sun, 24 Jun 2012 16:37:03 -0700 (PDT) ArbolOne <ArbolOne@gmail.com> wrote:

> 
> #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
> #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
> 
> 
> rc = sqlite3_step(stmt);
> if (rc != (SQLITE_DONE) || (SQLITE_ROW)){
> std::cout << "Error " << rc << std:;endl;
> }
> 
> having looked at the snip above, can any one tell me why this program would 
> display:
>                 Error 100

You cannot compare one integer variable to two integer constants: !=
only takes two arguments, never three and the OR operator does not
agregate arguments for other operators.  Both because || has lower
precidence than != and because != only takes two operands.  C/C++ is
NOT a 'natural' language and boolean logic does not work like English
(or any other natural language).  You did a literal translation of a
statement like:

"If rc is not SQLITE_DONE or SQLITE_ROW then print an error message."

Which is a valid English statement, that most English speakers would
understand to mean that rc should be compared to the two constant values
SQLITE_DONE and SQLITE_ROW and if it is equal to some other value
(besides these two values), then print an error message.  Computer
programming languages don't work that way.  It might be possible to to
use a set membership operation, but given that the set is small and
fixed, it is simplier to just test each value separately.  Consider the
English statement:

If rc is not SQLITE_DONE and rc is not SQLITE_ROW then print an error
message."

This statement actually says the same thing (logically), but for
*English* it is awkward (tossing the 'and' in the middle and
duplicating the 'test' is not how wetware computers work [think]).  But
a dumb computer would have not problem with (it is exactly what the
parser for the C/C++ compiler is written to expect), unlike the
original statement above!

What you want is:

rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE && rc != SQLITE_ROW) {
        std::cout << "Error " << rc << std:;endl;
}


What your original code doing was doing is:

        rc != (SQLITE_DONE) => true
        *done* -- the right side of the || is not done, since the left
is true.  If rc was equal to SQLITE_DONE (101), then it would have been:
        1) rc != (SQLITE_DONE) => false
        2) (SQLITE_ROW) => true [SQLITE_ROW != 0, therefore it is not false]

You code would have *always* printed 'Error ' and the value of rc, no
matter what rc was.

Boolean logic 101.

> ??
> 
> Thanks!
>                                                                               
>                                

-- 
Robert Heller             -- 978-544-6933 / heller@deepsoft.com
Deepwoods Software        -- http://www.deepsoft.com/
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments


                                             


reply via email to

[Prev in Thread] Current Thread [Next in Thread]