Ads

Logical Operators

LOGICAL OPERATORS:

- In logical operators we have
i) Logical AND
ii) Logical OR
iii) Logical NOT

- Logical NOT is a unary operator and logical AND and Logical OR are binary operators.

- We discussed this in earlier topic but let’s regain it in C programming language any value other than ‘0’ is considered as true either it may be positive or negative. For example -1 is considered as true and positive 4 is also considered as true.

LOGICAL AND:

- Logical AND operator is denoted by two ampersands &&.

- And it gives out a true i.e. value 1 when both operands are true or we can say, when both the operands are non-zero.

Syntax:
<operand1>&&<operand2>

- Angle brackets are used for demonstration purpose only and should not be used in your c program

- Logical AND operator can be used on constants, variables and expressions.

TRUTH TABLE:
Operand 1
Operand 2
Result
0
0
0
0
Non zero
0
Non zero
0
0
Non zero
Non zero
1


Constants:
5&&4 is true i.e. 1
-1&&-2 is true
-1&&2 is true
0&&1 is false i.e. 0
10&&0 is false i.e. 0

 
Variables:
If a=10,b=0
a&&b is false i.e 0
b&&a is also false
 

Expressions:
(4+5)&&(6+3) is true i.e.
if x=3,y=4 then (x+9)&&(y+2) is true
(x-x)&&3 is false because (x-x definitely yields error and 0&&3 is false)

- In AND operation if the compiler founds first operand value ‘0’ it will not go further and check second operand, it automatically returns ‘0’

Program:

/* A program to understand the operation of Logical AND */

#include <stdio.h>
int main()
{
 int a=0,b=7;

 if(a&&(b=b+1))
 {
 a=8;
 }
 
 printf("Value of a = %d",a);
 printf("Value of b = %d",b);
 
 return 0;
}

Output:

Value of a=0
Value of b=7

Explanation:
- In above program condition in if is a&&(b=b+1) .  According to our assumption when control reaches a&&(b=b+1) it will check the condition and assigns b=b+1 zero it will not go into if body and directly prints Value of a= 0, Value of b=8; But our assumption is wrong the value of b isn’t changing. This is because to reduce the load on CPU once AND operator finds the first operand as zero, it will return 0 and will not check the second operand. Because whatever the second operand might be the resultant is zero whenever first operand is zero. Check the truth table of Logical AND.

LOGICAL OR:

- Logical OR is denoted by ||.

- It gives out value 0 when both operands are 0. And rest all other conditions it gives 1.

Syntax:
<operand1>||<operand2>
- Angle brackets are used for demonstration purpose only and should not be used in your c program

'OR' TRUTH TABLE :
Operand1
Operand2
Result
0
0
0
0
Non-zero
1
Non-zero
0
1
Non-zero
Non-zero
1



Constants:
5||4 is true
-1||-2 is true
0||1 is true
-2||0 is true
0||0 is false

 
Variables:
If x=30,y=0 then
x||y is true

If x=0,y=0 then
x||y is false
 

Expressions:
If x=3, y=4 then
(x-x)||(y-y) returns 0
0 || (x+y) returns 1

- In OR operation computer returns 1 automatically when first operand is true, it will not go further and check if second operand is true or false.

- If x=4,y=5;

(x)||(x=x+y)

- The above expression the value returned is 1, it directly returns 1 without checking second operand i.e. after the above statements the value in x is 4 but not x+y;

Program:


/* Program to analyze Logical OR operator */
#include<stdio.h>
int main()
{
 int x=4,y=5;
 printf("Value in x= %d\n",x);
 printf("%d\n",x||(x=x+y));
 printf("Value in x= %d\n",x);

 return 0;
}

Output:

Value in x= 4
1
Value in x= 4


LOGICAL NOT:

- Denoted by ‘!’.

Syntax:
!<operand>

- Angle brackets are used for demonstration purpose only and should not be used in your c program.

- This operator returns ‘0’ if operand is non-zero and returns ‘1’ if operand is ‘0’.

- It’s kind of reverses the input.

'NOT' TRUTH TABLE:
OPERAND
RESULT
0
1
Non-zero
0

Program:


/* Write a program using Logical operators.*/

#include<stdio.h>
int main()
{
 int x,y;
 printf("\n Enter value x:\t");
 scanf("%d",&x);
 printf("\n Enter value y:\t");
 scanf("%d",&y);
 printf("Value of x&&y is %d\n",x&&y);
 printf("Value of x||y is %d\n",x||y);
 printf("Value of !x is %d\n",!x);

 return 0;
}

Output:

Enter value x:    3
Enter value y:    0
Value of x&&y is 0
Value of x||y is 1
Value of !x is 0





No comments:

Post a Comment