Operators in C Part 2


Operators in C Part 2 :

This post is a continuation of previous post , link to previous post : https://iamafutureprogrammer.blogspot.com/2019/05/operators-in-c-part-1.html

operators in c


Relational and Logical Operators :

Relational Operators are used in control constructs such as if and while .

There are four Relational operators in  C . They are :
Operators
Meaning
< 
Less than
<=
Less than or equal to
> 
Greater than
>=
Greater than or equal to

All the operators falls within the same precedence group , and their associativity is from left to right.

The two equality operators are :
Operators
Meaning
==
Equal to
!=
Not equal to

Equality operators fall in to a separate group and their associativity is from left to right .Relational operators are used to form logical expressions that compare the value of one expression with another. These logical expressions have a value of zero when the comparison is false and a value of one when the comparison is true .
If i , j and k are integer variables having values  1, 2 and 3 respectively.
Then
Expression
Interpretation
value
i < j
True
1
(i+j)>=k
True
1
(j+k)>(i+5)
False
0
k!=3
False
0
j==2
True
1


Logical Operators :

The C language operators allow a programmer to combine simple relational expression to form complex expressions by using logical NOT , AND and  OR .
Operator
Meaning
&&
AND
||
OR
!
NOT

Taking the logical AND , when two expressions are added the resulting expression will be true only if both the sub expressions are true .

E.g, (x>5)&&(y<15) will be only true if x greater than 5 and if y less than 15 .
The second expression (y<5) will not be evaluated if the first expression is false.

A logical expression is evaluated from left to right as far as needed to determine the logical result .

E.g, in the expression (y<15)&&(x++>15) , the value of x is incremented only if the value of y is less than 15.
The logical OR is represented by two vertical bars between two other expressions as (expression1)||(expression2) . the result of the expression is true if either of the two sub expressions is true .

If i is a integer variable having the value 7 , and f is a floating point variable whose value is 5,5 and c is a character variable that represents the character ‘w’ then:
Expression
Interpretation
Value
(i>=6)&&(c==’w’)
true
1
(i>=6)||(c==119)
true
1
(i<11)&&(i>100)
false
0
(c!=’p’)||((i+f)<=10)
true
1

The NOT simply reverses the truth value of the expression that follows it.

Example:
If i is a integer variable with value 7 and  f is a floating point variable whose value is 5.5 then :
Expression
Interpretation
Value
f>5
true
1
!(f>5)
false
0
!(i>(f+1))
false
0


Lets continue the operators in C in our next post ….

Thank you …..

Previous Post
Next Post
Related Posts

0 comments:

Popular Posts