You are on page 1of 23

SLG 3.7.

2
Relational
Operators
Joaquin, J.
Sanchez, T.

Slides by GDCuyasen
Target
• Analyze relational operators.
• Explore how to form and evaluate logical(Boolean)
expression.
Operators

One of the core concepts in any programming languages.


Symbols that perform specific mathematical and logical computations on
operands.
Classification

• Arithmetic
• Relational
• Logical
Relational Operators

• yields either True or False


• compare two values according to their equality
• used in creating conditions
Binary Operators

one operator, 2 operands

<left operand> <operator> <right operand>

Examples,

A * B B - 10
A > B U == P
Six Relational Operators

Operator Description Example

== Compare if the operands are equal A == B


(value contained if they are variables) g == 3.0
Six Relational Operators

Operator Description Example

!= Compare if the operands are not equal A != B


g != 3.0
Six Relational Operators

Operator Description Example

> Compare if value of left operand is A > B


greater than the value of right g > 2.75
operand
Six Relational Operators

Operator Description Example

< Compare if value of left operand is less A < B


than the value of right operand g < 2.75
Six Relational Operators

Operator Description Example

>= Compare if value of left operand is A >= B


greater than or equal to the value of g >= 2.75
right operand
Six Relational Operators

Operator Description Example

<= Compare if value of left operand is less A <= B


than or equal to the value of right g <= 2.75
operand
Reading Relational Expressions

A < B

Is the value of A less than the value B?

Yes = True
No = False
Reading Relational Expressions

grade == 1.00

Is the value of grade equal to 1.00?


Examples
int num = 5, sum = 11;
float grade = 2.75;

Expression Meaning Result

num > 10 Is the value of num greater than 10? No (False)

Is the value of num greater than the


sum > num Yes (True)
value of num?
Is the value of num equal to the value
num == sum No (False)
of sum?
Precedence

All relational operators have equal priority and are lower than arithmetic
operators.
Boolean Expressions

Expressions that produces a Boolean value when evaluated


Boolean Type

• bool
• Stores Boolean values
Boolean Type

bool greaterThan, lessThan;


int num = 5, sum = 10;

greaterThan = num > sum;


lessThan = num < sum;
Equal with strings

Variables of type string are compared character by character,


starting with the first character. The character-by-character
comparison continues until either a mismatch is found, or the
last characters have been compared and are equal.
Equal with strings

“Hello” == “hello”

False

The first character ‘H’ is less than the first character ‘h’ of
“hello” because the ASCII value of ‘H’ is 72 while ‘h’ is 104.
therefore, “Hello” is not equal to “hello”.
Equal with strings

“Bill” >= “Billy

False

“Bill” has four characters while “Billy” has five. Therefore, “Bill”
is the shorter string. All four characters of “Bill” are the same as
the corresponding first four characters of “Billy”, and “Billy” is
the larger string. Therefore, “Bill” >= “Billy” is False.
References

• Malik, D. (2011). C++ Programming (5th ed., pp. 6-9). Boston, MA: Course Technology.
• Deitel, H., & Deitel, P. (2005). C++ How to Program. Upper Saddle River, N.J.: Pearson
Education.
• Wanna-Joke. Programmers problem. Retrieved from https://wanna-
joke.com/programmers-problems-4/
• GeeksforGeeks. Operators in C | Set 2 (Relational and Logical Operators) Retrieved from
https://www.geeksforgeeks.org/operators-in-c-set-2-relational-and-logical-operators/
• Rebus Community. Programming Fundamentals. Retrieved from
https://press.rebus.community/programmingfundamentals/chapter/relational-operators/

You might also like