# Quick question on Boolean operators

Printable View

• 08-12-2007
gn17
Quick question on Boolean operators
I just got through the third lesson and was making some simple programs to practice what I learned. One of which is as follows (my question is mainly about the bold part):

Code:

```#include <stdio.h> int main() {     int x;     for (x = 0; x <=5 ; x++){         if (x == (1 || 3)){               continue;}         printf("x is %d and not 1 or 3.\n", x);         }         return 0; }```
It yields the following output:
x is 0 and not 1 or 3.
x is 2 and not 1 or 3.
x is 3 and not 1 or 3.
x is 4 and not 1 or 3.
x is 5 and not 1 or 3.

When I wrote this, I was expecting the program to not output the "x is 1..." and "x is 3..." statements. Surely enough, it didn't output the "x is 1..." line, but it produced the "x is 3..." line, which confuses me. By using the OR operator on the bold part of the code, I expected it to be read as "if x is 1 or 3, continue the loop." It did this for x == 1, but it didn't do this for x == 3, which leaves me quite puzzled. I know I can write it as "x == 1 || x == 3," but I was trying to find a more efficient way to do it.

Could some explain how the statement is being read in the program that makes it not produce the "x is 1..." line but produce the "x is 3..." line? Any other suggestions on how to achieve the result I want?

Much obliged.
• 08-12-2007
robatino
In C, nonzero is true in a boolean expression, and true and false evaluate as 1 and 0 resp., so (1 || 3) evaluates as 1. You need to rewrite it as "x == 1 || x == 3", there's no more efficient way to do it.
• 08-12-2007
MacNilly
First iteration, the condition becomes:

if (0 == (1 || 3))
if (false == (true)) <-- (1 || 3) always evaluates to 1.

which is false, so you print the value of x and that other stuff.

Second iteration, you get:

if (1 == 1)

which is true, so you skip the printing.

When x = 3, you get:

if (3 == 1)

which is false, so you get the printing.

To get the result you want, use

if (x == 1 || x == 3)

EDIT: Beaten taking too long to type this out.
• 08-12-2007
whiteflags
In C, any nonzero value is true. This means that in the statement x == ( 1 || 3 ) 1 or 3 is evaluated first (as true). So the statement simplifies to x == 1. Since 3 is not 1, the output is correct.

Maybe you wanted something like this.
Code:

```#include <stdio.h> int main ( void ) {   int v;   for ( v = 0; v < 5; v++ )   {     if ( v != 1 && v != 3 )       printf( "value = %d\n", v );   }   return 0; }```
• 08-12-2007
gn17
Ah, I see. So Boolean operators always yield a numeric value of only 1 or 0? That makes sense now. Thanks a bunch for the explanations!
• 08-12-2007
OnionKnight
Normally you'd hear that the logical operators (==, !, ||, && etc) return true or false. What this actually means is that they just return a normal int, with the value 1 or 0.
You could try printing it:
Code:

`printf("&#37;d %d\n", 1 && 0, 1 || 0);`
This should output "0 1".
Again I don't know if standards guarantee that, but Prelude uses this to an advantage in her tree tutorials so I guess it's as good as standard. ;)
• 08-12-2007
gn17
Quote:

Originally Posted by OnionKnight
Normally you'd hear that the logical operators (==, !, ||, && etc) return true or false. What this actually means is that they just return a normal int, with the value 1 or 0.
You could try printing it:
Code:

`printf("%d %d\n", 1 && 0, 1 || 0);`
This should output "0 1".
Again I don't know if standards guarantee that, but Prelude uses this to an advantage in her tree tutorials so I guess it's as good as standard. ;)

Haha. Well, I always feel that it's better to know more. Thanks for the input. ;)
• 08-12-2007
Shidlingayya
Hai Dude...
Please understand the basic difference between the bitwise operator and Logical operator

bitwise operator-->performs operations bit by bit, so you are result is correct only in case of bitwise operations

Logical operator--->it will give result only 0 and 1 so, in case of this always non-zero number(both positive and negative) is treated as truth value and zero is considered as false