Ugrás a tartalomhoz

Szerkesztő:LinguisticMystic/cpp/Comparison

A Wikiszótárból, a nyitott szótárból

🔍 Comparison Operators in C++

[szerkesztés]

Comparison operators are fundamental tools in C++ programming. They allow you to compare values or expressions and make decisions based on their relationships. These comparisons return Boolean values (true or false) and are critical for control flow in conditionals, loops, and logical operations.



🧮 What Are Comparison Operators?

[szerkesztés]

Comparison operators compare two values or expressions and return a result based on the comparison:

Operator Meaning Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 10 > 2 true
< Less than 4 < 9 true
>= Greater than or equal 5 >= 5 true
<= Less than or equal 7 <= 9 true



1. ✅ Equal To (==)

[szerkesztés]
int a = 5;
int b = 5;
bool result = (a == b); // result is true

Explain code: This checks whether a and b contain the same value. Since both are 5, the comparison a == b evaluates to true, and the result variable holds true.



2. ❌ Not Equal To (!=)

[szerkesztés]
int x = 10;
int y = 20;
bool result = (x != y); // result is true

Explain code: Here, x is 10 and y is 20. Since the values are not equal, x != y is true.



3. 🔼 Greater Than (>) and 🔽 Less Than (<)

[szerkesztés]
int p = 15;
int q = 8;
bool result_gt = (p > q); // result_gt is true
bool result_lt = (p < q); // result_lt is false

Explain code: p is 15 and q is 8.

  • p > q is true because 15 > 8.
  • p < q is false because 15 is not less than 8.



4. ➕ Greater Than or Equal To (>=) and ➖ Less Than or Equal To (<=)

[szerkesztés]
int m = 25;
int n = 25;
bool result_ge = (m >= n); // result_ge is true
bool result_le = (m <= n); // result_le is true

Explain code: Since both m and n are equal:

  • m >= n is true, because 25 is greater than or equal to 25.
  • m <= n is also true for the same reason.



🧠 Behavior with Different Data Types

[szerkesztés]

📚 Characters (based on ASCII)

[szerkesztés]
char char1 = 'A';
char char2 = 'B';
bool result = (char1 == char2); // result is false

Explain code: ASCII value of 'A' is 65, and 'B' is 66. Since 65 != 66, the result is false.



📝 Strings (Lexicographical Comparison)

[szerkesztés]
string str1 = "apple";
string str2 = "banana";
bool result = (str1 < str2); // result is true

Explain code: Lexicographically, "apple" comes before "banana". Since 'a' < 'b', the result of str1 < str2 is true.



⚠️ Comparing Floating-Point Numbers with Epsilon

[szerkesztés]

Floating-point math can lead to imprecision:

double num1 = 0.1 + 0.1 + 0.1;
double num2 = 0.3;
bool result = (num1 == num2); // might be false

Explain code: Due to floating-point rounding errors, 0.1 + 0.1 + 0.1 might not be exactly 0.3. Thus, the comparison can fail.

✅ Epsilon Comparison Example

[szerkesztés]
#include <iostream>
#include <cmath> // For std::abs

int main() {
    double num1 = 0.1 + 0.1 + 0.1;
    double num2 = 0.3;
    double epsilon = 1e-9;
    
    bool result = std::abs(num1 - num2) < epsilon;

    if (result) {
        std::cout << "num1 and num2 are approximately equal." << std::endl;
    } else {
        std::cout << "num1 and num2 are not equal." << std::endl;
    }

    return 0;
}

Explain code:

  • epsilon defines a small tolerance (0.000000001).
  • std::abs(num1 - num2) < epsilon checks if the two numbers are “close enough” instead of exactly equal.



🎮 Practical Example: Number Guessing Game

[szerkesztés]
#include <iostream>
using namespace std;

int main() {
    int secretNumber = 42;
    int userGuess;

    cout << "Guess the secret number: ";
    cin >> userGuess;

    if (userGuess == secretNumber) {
        cout << "Congratulations! You guessed it right." << endl;
    } else {
        cout << "Oops! That's not the secret number." << endl;
    }

    return 0;
}

Explain code:

  • The program stores a predefined number (42) and asks the user to guess it.
  • It uses the == operator to check if the user’s input matches the secret.
  • Displays appropriate feedback based on the comparison.



🧪 More Examples

[szerkesztés]
#include <iostream>
using namespace std;

int main() {
    int num1 = 10;
    int num2 = 20;

    if (num1 == num2) {
        cout << "num1 is equal to num2" << endl;
    } else {
        cout << "num1 is not equal to num2" << endl;
    }

    if (num1 > num2) {
        cout << "num1 is greater than num2" << endl;
    } else {
        cout << "num1 is not greater than num2" << endl;
    }

    if (num1 <= num2) {
        cout << "num1 is less than or equal to num2" << endl;
    } else {
        cout << "num1 is neither less than nor equal to num2" << endl;
    }

    return 0;
}

Output Explanation:

num1 is not equal to num2
num1 is not greater than num2
num1 is less than or equal to num2
  • 10 == 20 → false
  • 10 > 20 → false
  • 10 <= 20 → true



🧾 Conclusion

[szerkesztés]

Comparison operators are indispensable for:

  • Making decisions (if, switch)
  • Looping (while, for)
  • Validating input
  • Comparing strings or characters

Understanding how and when to use them will greatly improve your ability to write robust, logical, and adaptive C++ programs.