Relational Operators

Understanding Relational Operators

Relational Operators

  • Relational operators are commands that determine the relationship between two values.
  • Common relational operators include: equal to (==), not equal to (!=), less than (<), less than or equal to (<=), greater than (>), and greater than or equal to (>=).

Usage of Relational Operators

  • These operators are used to evaluate the relationship between two values or expressions, returning a Boolean value (true or false).
  • For example, 3 > 2 would return true since 3 is indeed greater than 2, while 2 == 3 would return false as 2 is not equal to 3.
  • != operator checks if the two compared values are not equal. Therefore 3 != 2 would return true.
  • <= and >= operators are inclusive, 3 >= 3 would return true as 3 is greater than or equal to 3.

Boolean Values

  • Boolean is a data type that has only two possible values, true and false.
  • All relational operators return Boolean values.
  • It is important to distinguish between == which is a relational operator and = which is an assignment operator. For instance, i = 5 assigns the value 5 to variable i, while i == 5 checks if the value of i is equal to 5.

Key Takeaway

  • Relational operators are tools that allow us to compare two values. They return Boolean values, which are particularly useful in control structures such as if statements and loops. Understanding these can greatly benefit code design and implementation.