logical operator in c++ |
LOGICAL OPERATOR IN C++:
In C programming
different types of operator is used to perform a special and specific task such
as Assignment operator and Airthematic operation likely those logical operator
also used to check the logic by identifying the given condition.
We used basic three type of logical operator in C and C++
during programming which are as follow
1.
And operator =&&
2.
Or operator =||
3.
Not operator =!
AND OPERATOR (&&):
Type of operator
which is used when we have to check two or more condition at a time. &&
operator allow two or more conditions to be combined in an if statement. The
flow of control will execute the given instruction if the all given condition
is true otherwise control execute the other instruction after the instruction
which should execute in under supervision of if statement.
In above program the
result will display on screen will be ‘Grade B’ gets printed if both the
conditions evaluate to true. If one of the conditions evaluate to false then
the whole thing is treated as false. if the both condition given above does not
true then the control execute the instruction in supervision of else statement.
OR OPERATOR (||):
It also used when we have to check two or more condition at
a time. || Operator allow two or more conditions to be combined in an if
statement. The flow of control will execute the given instruction if at least
one of the given conditions is true. Otherwise control execute the other
instruction after the instruction which should execute in under supervision of if
statement.
Example:
{
int a=3, b=5, c=3;
if ( a==b || a==c || b==c)
printf(“ Given number is same”);
else
printf(“ no numbers are same”);
}
The above program
after executed will display ‘Given number is same’, because the
condition a==c is true in this case. The feature of OR operator is that it will
executed the instruction in super vision of if statement if one of the given is
true.
NOT OPERATION (!);
The third logical operator is the NOT
operator, written as !. This operator reverses the result of the
expression it operates on. For example, if the expression evaluates to a
non-zero value, then applying ! operator to it results into a 0. Vice
versa.
Here is an example of
the NOT operator applied to a relational expression
! (y < 10 )
This means “not y
less than 10”. In other words, if y is less than 10, the expression will be
false, since (y < 10) is true. We can express the same condition as (y >=
10).
program used NOT ! operator.
0 comments:
Post a Comment
Please do not enter any spam link in the comment box