Lesson 16 of 60 – Relational Operators in C++
27%

Relational Operators in C++

Relational operators are used to compare two values or expressions. The result of a relational comparison is a Boolean value: true or false.

Note: Relational operators are commonly used in decision-making statements such as if, if-else, and loops.

1. What are Relational Operators?

Relational operators compare two values and determine their relationship.

int a = 10;
int b = 20;

bool result = a < b;

Here, the result is true because 10 is less than 20.

2. Relational Operators in C++

Operator Name
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

3. Equal To Operator ==

The == operator checks whether two values are equal.

int a = 10;
int b = 10;

bool result = (a == b);

The result is true.

4. Equal To with Different Values

int a = 10;
int b = 20;

bool result = (a == b);

The result is false because the two values are different.

5. Not Equal Operator !=

The != operator checks whether two values are not equal.

int a = 10;
int b = 20;

bool result = (a != b);

The result is true.

6. Not Equal with Same Values

int a = 10;
int b = 10;

bool result = (a != b);

The result is false because both values are equal.

7. Greater Than Operator >

The > operator checks whether the left value is greater than the right value.

int a = 20;
int b = 10;

bool result = (a > b);

The result is true.

8. Greater Than with Smaller Value

int a = 10;
int b = 20;

bool result = (a > b);

The result is false because 10 is not greater than 20.

9. Less Than Operator <

The < operator checks whether the left value is less than the right value.

int a = 10;
int b = 20;

bool result = (a < b);

The result is true.

10. Less Than with Larger Value

int a = 20;
int b = 10;

bool result = (a < b);

The result is false.

11. Greater Than or Equal To >=

The >= operator checks whether the left value is greater than or equal to the right value.

int marks = 40;

bool passed = (marks >= 40);

The result is true.

12. Greater Than or Equal with Greater Value

int marks = 80;

bool passed = (marks >= 40);

The result is true because 80 is greater than 40.

13. Less Than or Equal To <=

The <= operator checks whether the left value is less than or equal to the right value.

int age = 18;

bool result = (age <= 18);

The result is true.

14. Less Than or Equal with Smaller Value

int age = 15;

bool result = (age <= 18);

The result is true because 15 is less than 18.

15. Relational Operators Return bool

A relational expression produces a Boolean result.

int a = 10;
int b = 20;

bool result = a < b;

The variable result stores either true or false.

16. Displaying a Boolean Result

#include <iostream>

int main() {

    int a = 10;
    int b = 20;

    std::cout << (a < b);

    return 0;
}

By default, Boolean output is commonly displayed as 1 for true and 0 for false.

17. Using std::boolalpha

The std::boolalpha manipulator can display Boolean results as true and false.

#include <iostream>

int main() {

    int a = 10;
    int b = 20;

    std::cout << std::boolalpha;
    std::cout << (a < b);

    return 0;
}

18. Comparing Variables

Relational operators can compare variables.

int age1 = 20;
int age2 = 25;

if (age1 < age2) {
    std::cout << "Age1 is smaller";
}

The condition is true, so the message is displayed.

19. Relational Operators with if

Relational operators are frequently used inside if conditions.

int age = 20;

if (age >= 18) {
    std::cout << "Adult";
}

The message is displayed because the condition is true.

20. Comparing Marks

int marks = 75;

if (marks >= 40) {
    std::cout << "Pass";
}

The >= operator checks whether the marks are at least 40.

21. Comparing Two Numbers

#include <iostream>

int main() {

    int a = 50;
    int b = 30;

    if (a > b) {
        std::cout << "A is greater than B";
    }

    return 0;
}

22. Comparing Decimal Values

Relational operators can also compare floating-point values.

double price1 = 99.50;
double price2 = 100.00;

if (price1 < price2) {
    std::cout << "Price1 is lower";
}

For floating-point comparisons, be careful when exact equality is required because of representation and rounding.

23. Comparing Characters

Characters can be compared using relational operators according to their values in the execution character set.

char a = 'A';
char b = 'B';

if (a < b) {
    std::cout << "A comes before B";
}

24. Comparing Strings

C++ std::string objects support relational comparisons.

#include <iostream>
#include <string>

int main() {

    std::string a = "Apple";
    std::string b = "Banana";

    if (a < b) {
        std::cout << "A comes before B";
    }

    return 0;
}

String comparisons are based on the ordering defined for std::string.

25. Combining Relational Operators

Relational expressions can be combined with logical operators.

int age = 25;

if (age >= 18 && age <= 60) {
    std::cout << "Age is within the range";
}

Here, both relational conditions must be true.

26. Common Mistake: = vs ==

A common beginner mistake is confusing assignment = with equality comparison ==.

int age = 20;

if (age == 20) {
    std::cout << "Age is 20";
}

Use = for assignment and == for equality comparison.

27. Relational Operators with User Input

#include <iostream>

int main() {

    int number;

    std::cout << "Enter a number: ";
    std::cin >> number;

    if (number > 0) {
        std::cout << "Positive number";
    } else if (number < 0) {
        std::cout << "Negative number";
    } else {
        std::cout << "Zero";
    }

    return 0;
}

The program uses relational operators to compare the input with zero.

28. Complete Relational Operators Example

#include <iostream>

int main() {

    int a = 20;
    int b = 10;

    std::cout << std::boolalpha;

    std::cout << "a == b: "
              << (a == b) << std::endl;

    std::cout << "a != b: "
              << (a != b) << std::endl;

    std::cout << "a > b: "
              << (a > b) << std::endl;

    std::cout << "a < b: "
              << (a < b) << std::endl;

    std::cout << "a >= b: "
              << (a >= b) << std::endl;

    std::cout << "a <= b: "
              << (a <= b) << std::endl;

    return 0;
}

29. Common Relational Operator Mistakes

  • Using = instead of == when checking equality.
  • Confusing > with >=.
  • Confusing < with <=.
  • Forgetting that relational expressions produce Boolean results.
  • Ignoring floating-point precision when testing exact equality.
  • Writing complicated comparisons without using parentheses or clear formatting.

30. Relational Operators – Final Summary

Operator Name Example Meaning
== Equal to a == b a and b have equal values
!= Not equal to a != b a and b have different values
> Greater than a > b a is greater than b
< Less than a < b a is less than b
>= Greater than or equal a >= b a is greater than or equal to b
<= Less than or equal a <= b a is less than or equal to b

📌 Key Points

  • Relational operators are used to compare values.
  • The result of a relational expression is a Boolean value.
  • == checks whether two values are equal.
  • != checks whether two values are different.
  • > checks whether the left value is greater than the right value.
  • < checks whether the left value is less than the right value.
  • >= checks whether the left value is greater than or equal to the right value.
  • <= checks whether the left value is less than or equal to the right value.
  • Relational operators are commonly used with if statements and loops.
  • Use == for equality comparison and = for assignment.
  • Be careful with exact equality comparisons involving floating-point values.

🧠 Quick Quiz

Question: Which operator is used to check whether two values are equal in C++?