Logical OR (||) ??

A

ArbolOne

#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
??

Thanks!
 
I

Ian Collins

#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)){

SQLITE_ROW, being defined as 101, will always be true.

Even if you had written

if (rc != (SQLITE_DONE) || rc != (SQLITE_ROW))

the expression would always be true (if rc==100, it can't be 101).

Did you intend to write

if (rc != (SQLITE_DONE) && rc != (SQLITE_ROW))

?
 
R

Rui Maciel

ArbolOne said:
#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
??

For your program to display that message, the value returned by
sqlite3_step(stmt) and assigned to rc is 100. In other words,
rc == SQLITE_ROW.

so, looking at the if statement, if rc == SQLITE_ROW then the first
expression, rc != SQLITE_DONE, is evaluated to 0. This leads the logical or
operator to evaluate the right hand side expression. As the right hand side
expression is SQLITE_ROW, and as you've defiend SQLITE_ROW as representing
100, as it is unequal to 0 then the logical operator yields 1.

Hence, you get the equivalent of if(1), and your program ends up printing
that "Error 100" message.


Rui Maciel
 
A

ArbolOne

#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
??

Thanks!

Aaaaaaaaaaaaakh!
Yes, yes, you are right.
There are days when our brains don't work the way they supposed to.

Thanks folks!
 
M

Mark

SQLITE_ROW, being defined as 101, will always be true.

Even if you had written

if (rc != (SQLITE_DONE) || rc != (SQLITE_ROW))

the expression would always be true (if rc==100, it can't be 101).

Did you intend to write

if (rc != (SQLITE_DONE) && rc != (SQLITE_ROW))

I'd always recommend using parenthesis rather than relying on operator
precdence. It may not be strictly necessary but does make the code
easier to read.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top