First, let’s learn about logical operators.
These are used to logically combine two (or more) expressions.
They are logical AND (&&), logical OR (||) and logical NOT (!)
Logical AND ( &&)
exp1 | exp2 | exp1&&exp2 |
---|---|---|
T | T | T |
T | F | F |
F | T | F |
F | F | F |
##Logical OR (||)
exp2 | exp1||exp2 | |
---|---|---|
T | T | |
F | T | |
T | T | |
F | #F |
##Logical NOT (!)
F | |
Example | a=10,b=20,c=30 | Output | ||
---|---|---|---|---|
(a>b)&&(a
|
(10>20)&&(10 | 0 | ##|| | Logical OR |
! | logical negation | |||
!(10>20) | 1 | Example |
#includemain (){ float a=0.5,b=0.3,c=0.7; printf("%d ",(ac));//0// printf("%d
",(a>=b)&&(b<=c));//1// printf("%d
",(a==b)||(b==c));//0// printf("%d
",(b>=a)||(a==c));//0// printf("%d
",(b<=c)&&!(c>=a));//0// printf("%d
",!(b<=c)||(c>=a));//1// }
0 1 0 0 0 1
Compound assignment
< ul class="list"> Operator##= | ||
---|---|---|
=,-=,*=,/=,%= | Compound assignment | |
a=10"a=a-10 | Procedure | below Given is a C program for compound assignment operator -
#includeint main(void){ int i; char a='h'; printf("enter the value of i: "); scanf("%d",&i); printf("print ASCII value of %c is %d
", a, a); a += 5; printf("print ASCII value of %c is %d
", a, a); a *= a + i; printf("a = %d
", a); a *= 3; printf("a = %d
", a); a /= 2; printf("a = %d
", a); a %= 4; printf("a = %d
", a); return 0; }
You will see the following output -
enter the value of i: 3 print ASCII value of h is 104 print ASCII value of m is 109 a = -80 a = 16 a = 8 a = 0
The above is the detailed content of Explain the concepts of logical operators and assignment operators in C language. For more information, please follow other related articles on the PHP Chinese website!