You can
combine results of multiple relations or logical operations by using logical
operation. The logical operators are negation (!), logical AND
(&&), and
logical OR (||), in the same order of
preference.
Program
#include<stdio.h>
main( )
{
int
c1,c2,c3;
scanf("%d%d%d",&c1.&c2,&c3);
if((c1 <
c2)&&(c1<c3))
printf("\n c1 is less than c2 and c3");
if (!(c1< c2))
printf("\n c1 is greater than c2");
if ((c1 < c2)||(c1 < c3))
printf("\n
c1 is less than c2 or c3 or both");
}
Input
c1= 2;
c2= 3;
c3= 4;
Output
ENTER VALUES OF c1, c2 AND c3
2
3
4
c1 is less than c2 and c3
c1 is less than c2 or c3 or both
- Logical AND returns a true value if
both relational expressions are true. Logical OR returns true if any of the
expressions are true. Negations return complements of values of relational
expressions, as shown in Table 3.2.
R1
|
R2
|
R1
&& R2
|
R1 ||
R2
|
! R1
|
T
|
T
|
T
|
T
|
F
|
T
|
F
|
F
|
T
|
F
|
F
|
T
|
F
|
T
|
T
|
F
|
F
|
F
|
F
|
T
|
- Logical operators AND, and OR have higher priority than
assignment operators, but less than relational operators. Negation
operators have the same priority as unary operators, that is, the highest
priority.
- While evaluating logical
expressions, C uses the technique of short circuiting. So if the
expression is: C1 && C2 &&
C3 && C4 if C1 is true then only C2 is evaluated. If C1 is false, the expression
returns false even if C2, C3, and C4 are true. So if C1 is false C2, C3, and C4 are not evaluated. Remember
this when you are doing something such as searching in an array. For
example, if you want to search for K in an array, the last value of which
is subscript N, you can write the search condition in two ways:
I - (a [i] == K) && (i <= N)
II - (i <= N) && (a[i] == K)
- In case I you compare the
array limit with K and check the bound. This is not correct because if the
value of i is more than N you will get the array index out-of-bounds
error.
- In case II, you first check
the bound and then compare the array element. This is correct because you
will never compare the array element if value of i is more than N.The technique of
short-circuiting is applicable to the OR operator also. Thus if the expression is:
C1 || C2 || C3 || C4 if C1 is true then the expression returns true
and C2, C3 and C4 are not evaluated.
Ternary
operators return values based on the outcomes of relational expressions. For
example, if you want to return the value of 1 if the expression is true and 2
if it is false, you can use the ternary operator.
Program/Example
If you
want to assign the maximum values of i and j to k then you can write the statement
k = ( i>j ) ? i : j;
The
general form of the ternary operator is:
(expr 1) ? expr2 : expr3
If expr1 returns true then the value of expr2 is returned as a result; otherwise the value of expr3 is returned.
You can
increment or decrement the value of variable using the increment or decrement
operator. These operators can be applied only to variables and they can be
applied using prefix form or postfix form.
Program
#include<stdio.h>
main( )
{
int I,j,k;
i = 3;
j =4;
k = i++ + --j;
printf("i = %d, j = %d, k = %d",i,j,k);
}
Input
i =3, j = 4.
Output
i = 4, j = 3, k = 6.
Explanation
When the
prefix form is used, the value of the variable is incremented/decremented first
and then applied. In the postfix form, the value is applied and only after the assignment operator is done is the value
incremented or decremented.
- Suppose you write
2. i = 3;
3. j =4;
4. k = i++ + -j;
you will get the value of k as 6, i as 4 and j as 3. The order of evaluation is as follows:
- i gets the value 3.
- j is decremented to 3.
- k gets the value 3 + 3.
- i is incremented.
- Suppose you write
6. i = 5;
7. i = i++ * i++
Then you
will get the value of i as 27. This is because first the
value 5 is used as to make i = 25 and then
i is incremented twice. The increment and decrement
operators have higher priority than the arithmetic operators.