Relational Operator
Relational operators are used to perform a logical test or create a condition. List of the logical operator is as given below...
Relational opertor |
Meaning |
> |
Greater Than |
< |
Less Than |
>= |
Greater Than or Equal to |
<= |
Less Than or Equal to |
== |
Equal to |
!= |
Not euqal |
- Every relational operator returns true or false.
- In C any non-zero is treated as true and zero is treated as false.
- In C if any expression evaluated true then system returns 1 otherwise return 0.
- Relational operator are evaluated from left to right.
Example-1
Example-2
Logical Operator
- &&(And) ||(Or) !(not) are the 3 logical operators.
- &&(and), || (or) are used to combine two or more conditions.
- Each condition can be true or false, so what will be the output of the combination made by logical operator &&(and) , ||(or). This is decided by truth table.
- truth table is the combination of conditionsa and their results as shown below...
C1 |
C2 |
C1 && C2 |
C1 || C2 |
F |
F |
F |
F |
F |
T |
F |
T |
T |
F |
F |
T |
T |
T |
T |
T |
Examples of Relational Operator
| |
| int a = 5, b = 6, c; |
| c = a > 2 && b > 3; |
| printf("%d %d %d",a,b,c); |
| |
| int a = 5 , b = 6, c; |
| c = a > 2 || b < 8; |
| printf("%d %d %d",a,b,c); |
| |
| int a = 5, b = 6, c; |
| c = a > 2 || ++b; |
| printf("%d %d %d",a,b,c); |
Logical operator are short circuit operator means or(||) operator returns true as soon as if first condtion is true, and(&&) operator returns false as soon as first condition is evaluated false.
Not operator
- Not operator just negates the result as the following truth table shown...
Example of not operator
| |
| int a = 5, b; |
| b = !a > 2; |
| printf("%d %d",a,b); |
| |
| |
| |
| int a = 5, b; |
| b = !(a > 2); |
| printf("%d %d",a,b); |
Comments