Relational Operators
The relational operators are – <,>, <=,>=, == and !=.
The general form of using a relational operator is –
Expression1 Operator Expression2
यहाँ operator कोई भी relational operator हो सकता है और Expression1 और Expression2, variables, constants या arithmetic expressions हो सकते है ।
Relational operators के नाम व विवरण :
यदि x=10 और y=20 जब x<y तो ये true होगा क्योकि x is less than y (x,y से छोटा है) ऐसे ही अगर x>y तो ये false होगा क्योकि x is not grater than y(x, y से बड़ा नही है) । ऐसे ही अन्य relational opeartors का result, true और false में आता है ।
An expression containing relational operator is referred as Relational Expression. For example x <= y is a relational expression.12*3 = = 9*4 is also a relational expression, in which arithmetic expression are used on each side of = =. All relational expressions evaluate to true or false. For example 12*3 = = 9*4 will evaluate to true because the result on each side of = = is 36. Similarly, 10 <= 7 will evaluate to false because 10 is neither less than nor equal to 7.
Comparison of Characters
When you compare two characters, this comparison is actually between their ASCII values. If ch1 = ’A’ and ch2 = ’B’ then writing ch1 < ch2 will mean ’A’ < ’B’ and then 65 < 66 clearly true.
In C, true means 1 and false means 0. That is, in C true and false are represented by 1 and 0 respectively. When a relational expression evaluates to true or false it actually evaluates to 1 or 0 respectively. You can obtain this 1 or 0 using assignment operator. For example, consider the following statement –
Operator
|
Name
|
Example
|
Meaning
|
<
|
Less than
|
x < y
|
x is less than y
|
>
|
Greater than
|
x > y
|
x is greater than y
|
<=
|
Less than or equal to
|
x <= y
|
x is less than or equal to y
|
>=
|
Greater than or equal to
|
x >= y
|
x is greater than or equal to y
|
= =
|
Equals to
|
x = = y
|
x is equal to y
|
!=
|
Not equals to
|
x != y
|
x is not equal to y
|
यदि x=10 और y=20 जब x<y तो ये true होगा क्योकि x is less than y (x,y से छोटा है) ऐसे ही अगर x>y तो ये false होगा क्योकि x is not grater than y(x, y से बड़ा नही है) । ऐसे ही अन्य relational opeartors का result, true और false में आता है ।
यहाँ ध्यान देने वाली बात है कि C में double equal sign, = = का उपयोग equal operator के लिए करते है । इसके द्वारा दो values को compare है। C में ये कहे कि x is equal to y, तो इसको x = = y लिखेगे। single equal sign, = को assignment operator which copies the value at right to the variable at left. If you write x = y, then no comparison will be made rather the value of y will be copied to x.
Also, = = operator gives the result true or false, when operands are equal or unequal, respectively. No such result is obtained by = operator. The = = operator does not copy the value at right to the variable at left. When you use = =, you can check that whether two expressions evaluates to a common result or not. For example on writing —
9*4 = = 12*3
true result will be obtained. As 9*4 and 12*3 both evaluates to a common value 36. This cannot be done using = operator. If you write 9*4 = 12*3 then this will not compare the LHS and RHS. Rather, it will be considered illegal because the result of 12*3 i.e. 36 cannot be assigned to 9*4. As you know, a single variable is always required at the left of = to store the result at right. The result of RHS 12*3, which is 36, cannot be stored in 9*4.
Relational Expression
Relational Expression
An expression containing relational operator is referred as Relational Expression. For example x <= y is a relational expression.12*3 = = 9*4 is also a relational expression, in which arithmetic expression are used on each side of = =. All relational expressions evaluate to true or false. For example 12*3 = = 9*4 will evaluate to true because the result on each side of = = is 36. Similarly, 10 <= 7 will evaluate to false because 10 is neither less than nor equal to 7.
Comparison of Characters
When you compare two characters, this comparison is actually between their ASCII values. If ch1 = ’A’ and ch2 = ’B’ then writing ch1 < ch2 will mean ’A’ < ’B’ and then 65 < 66 clearly true.
Similarly, ’A’ > ’a’ means 65 > 97 means false.
true and false
true and false
In C, true means 1 and false means 0. That is, in C true and false are represented by 1 and 0 respectively. When a relational expression evaluates to true or false it actually evaluates to 1 or 0 respectively. You can obtain this 1 or 0 using assignment operator. For example, consider the following statement –
x = 3 < 4;
As you know the relational expression 3 < 4 is true. So the value 1 will be assigned to the x. Here, x must be integer type. You can print the value of x (which is 1 in this case) through
printf( ).
Like this –
Like this –
printf(“Value of x = %d”, x);
The output is –
Value of x = 1
Similarly, the following statement –
y = 10 > 12;
will assign the value 0 to y. Because 10 > 12 is false. Here, y must be integer type The value y can be easily printed using familiar printf( ). Like this –
printf(“Value of y = %d”, y);
The output is –
Value of y = 0
The value 0 (which denotes false) or 1 (which denotes true) that a relational or logical operator returns is called truth value. So, the truth value of expression 10 > 12 is 0 and of 20 > 15 is 1. Also, we can say that truth values are true and false.
NEXT : Logical Operators
PREVIOUS : Arithmetic Operators
ग प्रोग्रामिंग शुरुआती के लिए नमूना कार्यक्रम
ReplyDeleteअस्थायी फ़ाइल नमूना कोड बनाएँ