0% found this document useful (0 votes)
10 views

Relational Operators

Computer

Uploaded by

DARWIN RANILE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Relational Operators

Computer

Uploaded by

DARWIN RANILE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Relational operators

< Less than


> Greater than
<= Less than or equal
>= Greater than or equal
== Equal
!= Not equal

- Relational operators are used to compare two values (the left side and the
right side). Since the operator needs a left side and a right side, it is a
binary operator.
- An expression involving relational operator/s can be called as a relational
expression.
- The result of the relational expression can be either 0 or 1 (integer data
type). 0 stands for false and 1 stands for true. (Back then, the result of the
relational expression was said to be 0 (false) or non-zero (true). 0 or 1 is
less confusing and easier to recall.)
- Even if the comparison involves comparing two floats, the result of the
relational expression is still 0 or 1 (still integer data type as result).

See sample program segment below:

int x = 12, y = 6, result;

printf(“%d”,48 < 61); /* should display 1 because 48 is less than 61 */

result = x > 24; /* result gets the value of 0 because x which is 12 is


not greater than 24 */

printf(“\n%d”,x == y); /*should display 0 because y is not equal to x*/

result = x <= y * 2; /* result gets the value of 1 */

From the example:


1. In the first example, an integer is on the left side and an integer is on the
right side. The use of the printf is just an example so that we are able to
see the result of the operation.
2. Second example, a variable is on the left side and an integer is on the
right side. Note: It is also allowed to have the integer on the left side
and the variable is on the right side. As described in the comment, the
value of the variable x is used to compare to 24.
3. Third example, a variable is on the left side and a variable is on the right
side. The use of the printf here is the same reason as the first example.
Also, similar to the second example, the values are being compared.
Thus, the value of x which is 12 is compared to the value of y which is 6.
4. Fourth example, a variable is on the left side and an arithmetic expression
is on the right side. The arithmetic expression is evaluated first. Thus, the
arithmetic expression will result in 12 (because of 6 * 2) and this will be
compared to the value of x.

In reference to the fourth example, here is an update to the table for


precedence of operators:

Parentheses ()
Multiplication and Division */%
Addition and Subtraction +-
Relational operators < > <= >= == !=

Note: The relational operators as shown in the examples above are


not normally used that way. The relational operators are commonly used in
if-statements (next topic) and loops.
Logical operators

&& Logical and


|| Logical or
! Logical not

- Logical operators (logical and, logical or) can be used to evaluate two
values (the left side and the right side). Since the operator needs a left
side and a right side, it is a binary operator.
- It is more common to use logical operators with relational expressions.
Relational expressions would be on the left side and the right side of the
logical operator.
- The logical not (!) is not a binary operator since it only requires a right
side. The logical not is also called as negation.
- An expression involving logical operator/s can be called as a logical
expression.
- The result of the logical expression can be either 0 or 1 (integer data
type). 0 stands for false and 1 stands for true.
- This symbol (|) is called the bar and is commonly located above \ on the
keyboard.

The truth table illustrates the result of a logical expression:

&& (logical and)


Left Right Result
1 1 1
1 0 0
0 1 0
0 0 0

|| (logical or)
Left Right Result
1 1 1
1 0 1
0 1 1
0 0 0

! (logical not) Result


1 0
0 1
The update to the table for precedence of operators:

Parentheses ()
Multiplication and Division */%
Addition and Subtraction +-
Relational operators < > <= >= == !=
Logical operators && ||

See sample program segment below:

int x = 12, y = 6, result;

printf(“%d”,48 < 61 && 100 > 65);


//48 < 61 is 1, 100 > 65 is 1. 1 && 1 is 1

result = x > 24 || y <= 26;


//x > 24 is 0, y <= 26 is 1. 0 || 1 is 1

printf(“\n%d”,!(x == y) );
//12 == 6 is 0. Negating 0 is 1.

result = x <= y * 2 && x – 3 == y + 3;


//result gets 1

Note: Same as the relational operators, the logical operators as


shown in the examples above are not normally used that way. The logical
operators are commonly used in if-statements (next topic) and loops.

You might also like