Greater Than or Equal To Operator (>=
) in C++
The greater than or equal to operator in C++ is used to compare two values and check if the value on the left-hand side is greater than or equal to the one on the right-hand side.
It returns a Boolean result:
true
if the left operand is greater than or equal to the right operand.false
otherwise.
Syntax
operand1 >= operand2
1. Integer Comparison
gte_int.cpp
int a = 10, b = 10;
std::cout << (a >= b); // Output: 1 (true)
gte_int_false.cpp
int a = 5, b = 10;
std::cout << (a >= b); // Output: 0 (false)
2. Float and Double Comparison
gte_float.cpp
float x = 7.5f, y = 6.0f;
std::cout << (x >= y); // Output: 1 (true)
gte_double_equal.cpp
double a = 3.14159, b = 3.14159;
std::cout << (a >= b); // Output: 1 (true)
3. Character Comparison
Characters are compared using their ASCII values.
gte_char.cpp
char a = 'z', b = 'm';
std::cout << (a >= b); // Output: 1 (true)
gte_char_equal.cpp
char a = 'c', b = 'c';
std::cout << (a >= b); // Output: 1 (true)
4. Boolean Comparison
Boolean values are treated as integers: false = 0
, true = 1
.
gte_bool.cpp
bool a = true;
bool b = false;
std::cout << (a >= b); // Output: 1 (true)
gte_bool_equal.cpp
bool a = false, b = false;
std::cout << (a >= b); // Output: 1 (true)
5. String Comparison (std::string
)
Lexicographical comparison is supported with std::string
.
gte_string_true.cpp
std::string name1 = "zebra";
std::string name2 = "apple";
std::cout << (name1 >= name2); // Output: 1 (true)
gte_string_equal.cpp
std::string name1 = "code";
std::string name2 = "code";
std::cout << (name1 >= name2); // Output: 1 (true)
6. C-style Strings (char[]
)
Use strcmp()
to compare C-style strings.
gte_cstring.cpp
#include <cstring>
char a[] = "delta";
char b[] = "alpha";
std::cout << (strcmp(a, b) >= 0); // Output: 1 (true)
7. Greater Than or Equal in if
Statement
gte_if.cpp
int marks = 90;
if (marks >= 50) {
std::cout << "Pass";
}
8. Greater Than or Equal in Loops
gte_loop.cpp
int i = 10;
while (i >= 1) {
std::cout << i << " ";
i--;
}
Output: 10 9 8 7 6 5 4 3 2 1
9. Mixed Type Comparisons
gte_mixed.cpp
int a = 5;
float b = 5.0f;
std::cout << (a >= b); // Output: 1 (true)
gte_mixed_false.cpp
double a = 3.2;
int b = 5;
std::cout << (a >= b); // Output: 0 (false)
10. Practice Programs
Practice 1: Compare Student Marks
practice_gte_marks.cpp
int marks = 75;
if (marks >= 60) {
std::cout << "First Class";
}
Practice 2: Compare Characters
practice_gte_char.cpp
char grade1 = 'B';
char grade2 = 'C';
if (grade1 >= grade2) {
std::cout << "Grade1 is higher or equal.";
}
Practice 3: String Rank Check
practice_gte_string.cpp
std::string word1 = "quiz";
std::string word2 = "math";
if (word1 >= word2) {
std::cout << word1 << " comes after or is same as " << word2;
}
Summary Table
Data Type | Example | Result |
---|---|---|
int | 5 >= 3 | true |
float | 5.0f >= 5.0f | true |
double | 2.5 >= 3.0 | false |
char | 'z' >= 'a' | true |
bool | true >= false | true |
std::string | "zebra" >= "apple" | true |
char[] | strcmp("beta", "alpha") >= 0 | true |
Notes and Best Practices
- Always ensure that the types being compared are compatible.
- For
std::string
, use the>=
operator. - For C-style strings, always use
strcmp()
for accurate comparison. - In mixed-type expressions (e.g.,
int
withfloat
), C++ applies implicit type promotion.