Logical operators in C.


Logical operators in C.
Introduction:
Logical Operators are used to combine one or more relational expressions that results in formation of complex logic expression. Like relational operators, the logical operators evaluate the result of logical expression in terms of Boolean values that can be true(1) or false (0) according to the result of the logical expression. They are generally used in an expression.
    In C, there are three logical operators, logical AND(&&), logical OR(||) and logical NOT(!). The logical AND and logical OR operators are binary operators whereas logical NOT is an unary operator.

Logical AND(&&):
The logical AND (&&) is a binary operator that evaluates to true if and only if both of its operands evaluate to true. Otherwise, it return false.
Expression1
Expression2
Expression1 && Expression2
Value
False
False
True
true
False
True
False
true
False
False
False
true
0
0
0
1
                Table: Result of expression1 && expression2

   If age is of type int containing a value 25 and gender is of type char containing value ‘M’ then the logical expression (age>=18) && (gender==’M’) is true as (age>=18) and (gender==’M’) both evaluate to true.

Logical OR(||) :
The logical OR(||) is a binary operator that evaluates to true if and only if either or both of its operands evaluate to true. The result is false only when both operands are false.
Expression1
Expression2
Expression1 || Expression2
Value
False
False
True
true
False
True
False
true
False
False
False
true
0
1
1
1
      Table: Result of expression1 || expression2
Here expression1 and expression2 are two expressions used with logical OR (||) operator.
If age is of type int containing a value 25 and gender is of type char containing value ‘F’ then logical expression (age>=18) || (gender==’M’) is true as one the expression i.e. (age>=18) evaluates to true.

Example:
Write a program to check whether a number is divisible by 3 AND 5 or by 3 OR 5?
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“Enter a number =”);
scanf(“%d”,&num);
printf(“If answer = 0(no) and  =1(yes)”);
printf(“\nIs %d”,num);
printf(“\n\tdivisible by 3 and 5? %d”,(num%3==0 && num%5==0));
printf(“\n\tdivisible by 3 or 5? %d”,(num%3==0 || num%5==0));
getch();
}
OUTPUT:
Enter a number = 33
If answer = 0(No) and =1(yes)
Is 33
    divisible by 3 and 5? 0
    divisible by 3 or 5? 1
See video.


It should be remembered that C uses the short-circuit method for evaluating expressions involving logical && or logical || operators. If  in an expression involving logical && operator, the first operand evaluates to false then the resulting value is set false immediately no matter what the second operands is. If in an expression involving logical || operator, the first operand evaluates to true then the resulting value is set to true immediately no matter what the second operand is. Thus, in both these cases, there is no need to evaluate the second operand. The advantage of this short circuit method is that it saves time and produces more efficient code by not evaluating the second operand.
     The logical NOT operator(!) is a unary operator that takes only a single operand. It reverses the value of the logical expression i.e. it returns true if an expression is false and returns false if an expression is true. It always precedes it operand.
      Now consider I is an integer variable which is assigned the value to be 2 i.e.(i=2).
Now (i>3) will evaluate to false, so value assigned to expression is 0. Therefore !(i>3) will evaluate the expression to be true because negation of false would be true and the value assigned to the expression is 1.





2 comments:

Powered by Blogger.