Logical Operator

There are three types of logical operators: AND (&&), OR(||) and NOT(!)

Out of these AND and OR are binary, whereas NOT is a unary operator.
AND (&&):

Untitled Diagram (8).jpg

OR (||):

Untitled Diagram (9).jpg

NOT (!):

Untitled Diagram (10).jpg

In the above illustrations we saw truth table of AND, OR and NOT logical operators. Now we will take an example and try to understand how these operators will work.

  1. main()

{

int i = 10, j = 20 , k ;

k = i && j;

printf(“i = %d j = %d k = %d\n”,i,j,k);

}

In the above example, value of i is 10, that means non – zero value for j is 20 which is also a non – zero value. Hence, as both the values are non – zero, therefore answer of k will be 1.

Now I’ll proceed to another example :

2. main()

{

int i = 10, j = 20 , k ;

k = i || ( j = 50);

printf(“i = %d j = %d k = %d\n”,i,j,k);

}

Answer : i =10 , j =20 , k =1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: