 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ArbolOne Guest
|
Posted: Sun Jun 24, 2012 11:46 pm Post subject: Logical OR (||) ?? |
|
|
#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! |
|
| Back to top |
|
 |
Rui Maciel Guest
|
Posted: Sun Jun 24, 2012 11:46 pm Post subject: Re: Logical OR (||) ?? |
|
|
ArbolOne wrote:
| Quote: | #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 |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sun Jun 24, 2012 11:46 pm Post subject: Re: Logical OR (||) ?? |
|
|
On 06/25/12 11:46 AM, ArbolOne wrote:
| Quote: | #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))
?
--
Ian Collins |
|
| Back to top |
|
 |
ArbolOne Guest
|
Posted: Mon Jun 25, 2012 12:53 am Post subject: Re: Logical OR (||) ?? |
|
|
On Sunday, June 24, 2012 7:46:29 PM UTC-4, ArbolOne wrote:
| Quote: | #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! |
|
| Back to top |
|
 |
Mark Guest
|
Posted: Wed Jun 27, 2012 7:19 am Post subject: Re: Logical OR (||) ?? |
|
|
On Mon, 25 Jun 2012 11:54:05 +1200, Ian Collins <ian-news (AT) hotmail (DOT) com>
wrote:
| Quote: | On 06/25/12 11:46 AM, ArbolOne 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)){
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.
--
(\__/) M.
(='.'=) If a man stands in a forest and no woman is around
(")_(") is he still wrong? |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|