Logical Operators in C
In the
previous series of C tutorial, we had known about Bitwise operators.
In today's tutorial series, we will learn about Logical operators. When we had
to compare two variables, constants or operands, we used Relational operators,
but what would we do when we had to check more than one condition? Let us know
in this tutorial series what to do when two or more conditions are to be
compared?
What is logical operators?
Logical
operators are used to compare two or more expressions or conditions. This
operator combines two or more expressions and make it a condition.
In other
words, this operator is used to perform logical operations.
Let us
understand by example. - Suppose vacancy has come out in a company and for that
vacancy minimum qualification Graduation is required and at the same time it is
necessary to have 80% or more marks in Graduation, then now there will be a
logic perform that will first check whether Candidates have Graduation
qualification and if Graduation qualification then check if its mark is equal
to or more than 80% if both the condition is true only then further process
will be followed otherwise it will be sorted.
There is three
type of Logical operators which is given in the table below.
operators |
Name |
&& |
Logical
and operator |
|| |
Logical or operator |
! |
Logical
not operator |
Let’s study
all operators one by one
What
is ‘Logical and
operator’ (&&)
This operator is used to check the output of two expressions. In this, if
the output of both expressions is true then only it will return true otherwise
False will return.
Behavior of && operator
Statement 1 && |
Statement 2 |
Result |
False |
False |
False |
True |
False |
False |
True |
True |
True |
For example
#include<stdio.h>
#include<conio.h>
Void main()
{
Int = y,x=5;
Clrscr();
Y=x>4&&x<10
Printf(“%d”,y);
getch();
}
Output
Output will be 1 which is true statement. It is because both statements are
true and ‘&&’ operator combine both statement
What is “logical
or operators (||)”?
This operator is called Logical OR operator. This operator is used to
check the output of two expressions. In this, if one of the two expressions are
output, it will return true, otherwise it will return False.
Behavior of
&& operator
Statement 1 || |
Statement 2 |
Result |
False |
False |
False |
False |
True |
True |
True |
True |
True |
It’s means if one statement is true then the out will be true.
If one statement is false and other statement is true the output will be
true.
If both statements are false then the output will be false
For example
#include<stdio.h>
#include<conio.h>
Void main()
{
Int = y,x=5;
Clrscr();
Y=x>4||x<10
Printf(“%d”,y);
getch();
}
Output
Output will be 1 and it is true because both statements are true
What is “logical
not operators (!)”?
This operator is called Logical NOT operator. This operator is used to
reverse any logical state. If a condition is true, then the Logical NOT
operator makes it false.
For example
#include<stdio.h>
#include<conio.h>
Void main()
{
Int = y,x=5;
Clrscr();
Y=!x>4
Printf(“%d”,y);
getch();
}
Output
Output will be
0 its means it is false statements. It is because, x is greater then 4 but we
use logical not operator‘!’ so we get false
If you
understand the logical operators well in this post, then please comment and
write yes or no.
Thanks for
reading this post
Also read
(https://grapsvs.blogspot.com/2020/05/arithmetic-instruction-in-c-part-12.html)
(https://grapsvs.blogspot.com/2020/05/Arithmetic-instruction-in-c-programmingpart%2013.html)
0 Comments
Please do not enter any spam link in the comment box and please follo me.