Home >Backend Development >C++ >Explain the concepts of logical operators and assignment operators in C language
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 |
#include<stdio.h> main (){ float a=0.5,b=0.3,c=0.7; printf("%d</p><p>",(a<b)&&(b>c));//0// printf("%d</p><p>",(a>=b)&&(b<=c));//1// printf("%d</p><p>",(a==b)||(b==c));//0// printf("%d</p><p>",(b>=a)||(a==c));//0// printf("%d</p><p>",(b<=c)&&!(c>=a));//0// printf("%d</p><p>",!(b<=c)||(c>=a));//1// }
0 1 0 0 0 1Assignment operator is used to assign a value to a variable. TypeThe type of the assignment operator is -
Compound assignment
##= | ||
---|---|---|
=,-=,*=,/=,%= | Compound assignment | |
a=10"a=a-10 | Procedure | below Given is a C program for compound assignment operator -
#include<stdio.h> int main(void){ int i; char a='h'; printf("enter the value of i:</p><p>"); scanf("%d",&i); printf("print ASCII value of %c is %d</p><p>", a, a); a += 5; printf("print ASCII value of %c is %d</p><p>", a, a); a *= a + i; printf("a = %d</p><p>", a); a *= 3; printf("a = %d</p><p>", a); a /= 2; printf("a = %d</p><p>", a); a %= 4; printf("a = %d</p><p>", a); return 0; }Output
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!