blogger Indonesia

Follow judhyns blog

RELATIONAL OPERATOR

Introduction

Relational operators are used in Boolean conditions or expressions, that is, the expressions that return either true or false. The relational operator returns zero values or nonzero values. The zero value is taken as false while the nonzero value is taken as true.

Program

Th relational operators are as follows:

<, <=, >, >=, ==, != 

The priority of the first four operators is higher than that of the later two operators. These operators are used in relational expressions such as:

7 > 12       // false 20.1 < 20.2     // true 'b' < 'c'   // true "abb" < "abc"  // true 

The strings are compared according to dictionary comparison, so if the first characters are equal, the condition is checked for the second characters. If they are also equal then it is checked for the third character, etc. The relational operators return integer values of either zero or non zero.

Note that the equality operator is == and not =. ‘=’ is an assignment operator.

If you want to compare a and b for equality then you should write a == b, not a = b because a = b means you are assigning the value of b to a, as shown in Table 3.1.

Table 3.1: Comparing the equality operator (= =) with the ‘=’ assignment operator.

Case

a

b

a = b

a == b

1

5

3

a = 3 (true)

false

2

7

0

a = 0 (false)

false

3

0

0

a = 0 (false)

true

In case 1, the value of a = 5 and b = 3. The assignment expression assigns the value of b to a, so a will be 3. The expression returns a true value because 3 is not zero. For the same case a == b does not make any assignment and returns a false value because in the value of a does not equal that of b.

In case 2, the value of a = 7 and b = 0. The assignment expression assigns the value of b to a, so a will be 0. The expression returns a false value of zero. For the same case, a == b does not make any assignment and returns a false value because the value of a does not equal that of b.

In case 3, the values of a and b are both 0. The assignment expression assigns the value of b to a, so a will be 0. The expression returns a false value of zero. For the same case, a == b does not make any assignment and returns a true value because the value of a equals that of b.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |