diff --git a/content/cpp/concepts/operators/terms/relational-operators/relational-operators.md b/content/cpp/concepts/operators/terms/relational-operators/relational-operators.md new file mode 100644 index 00000000000..65c8417d0e0 --- /dev/null +++ b/content/cpp/concepts/operators/terms/relational-operators/relational-operators.md @@ -0,0 +1,78 @@ +--- +Title: 'C++ Relational Operators' +Description: 'Return values of true (1) or false (0) based on various comparisons between two values.' +Subjects: + - 'Computer Science' +Tags: + - 'Operators' + - 'If' +CatalogContent: + - 'learn-c-plus-plus' +--- + +**Relational operators** (also known as comparison operators) serve to compare two values or variables. They are fundamental to programming, being an essential part of many decision-making methods. Values returned by relation operators will be Boolean, i.e. either true (1) or false (0). + +## Relational EQUAL TO (==) + +The **equal to** (==) operator determines whether two values are equal. It returns true if the values are equal and false if they are not. + +## Relational NOT EQUAL TO (!=) + +The **not equal to** (!=) operator determines whether two values are unequal. It returns true if they are not equal and false if they are equal. + +## Relational GREATER THAN (>) + +The **greater than** (>) operator determines whether the left operand in an expression is greater than the right operand and returns true if so, false if not. + +## Relational LESS THAN (<) + +The **less than** (<) operator determines whether the left operand in an expression is less than the right operand and returns true if so, false if not. + +## Relational GREATER THAN OR EQUAL TO (>=) + +The **greater than or equal to** (>=) operator returns true if the left operand in an expression is greater than or equal to the right operand. Otherwise it returns false. + +## Relational LESS THAN OR EQUAL TO (<=) + +The **less than or equal to** (<=) operator returns true if the left operand in an expression is less than or equal to the right operand. Otherwise it returns false. + +## Codebyte Example + +```codebytes/cpp +#include +using namespace std; + +int main() { + + int w = 2; + int x = 4; + int y = 2; + int z = 9; + + cout << "w = 2, x = 4, y = 2, z = 9\n"; + cout << "True = 1, False = 0\n\n"; + + cout << "(w == y) equals " << (w == y) << "\n"; // Returns true + cout << "(y != w) equals " << (y != w) << "\n"; // Returns false + cout << "(x > w) equals " << (x > w) << "\n"; // Returns true + cout << "(z < x) equals " << (z < x) << "\n"; // Returns false + cout << "(y >= w) equals " << (y >= w) << "\n"; // Returns true + cout << "(x <= y) equals " << (x <= y) << "\n"; // Returns false + + return 0; + +} +``` + +### Output +``` +w = 2, x = 4, y = 2, z = 9 +True = 1, False = 0 + +(w == y) equals 1 +(y != w) equals 0 +(x > w) equals 1 +(z < x) equals 0 +(y >= w) equals 1 +(x <= y) equals 0 +``` diff --git a/content/cpp/concepts/operators/terms/relational-operators/relational-operators.md.backup b/content/cpp/concepts/operators/terms/relational-operators/relational-operators.md.backup new file mode 100644 index 00000000000..65c8417d0e0 --- /dev/null +++ b/content/cpp/concepts/operators/terms/relational-operators/relational-operators.md.backup @@ -0,0 +1,78 @@ +--- +Title: 'C++ Relational Operators' +Description: 'Return values of true (1) or false (0) based on various comparisons between two values.' +Subjects: + - 'Computer Science' +Tags: + - 'Operators' + - 'If' +CatalogContent: + - 'learn-c-plus-plus' +--- + +**Relational operators** (also known as comparison operators) serve to compare two values or variables. They are fundamental to programming, being an essential part of many decision-making methods. Values returned by relation operators will be Boolean, i.e. either true (1) or false (0). + +## Relational EQUAL TO (==) + +The **equal to** (==) operator determines whether two values are equal. It returns true if the values are equal and false if they are not. + +## Relational NOT EQUAL TO (!=) + +The **not equal to** (!=) operator determines whether two values are unequal. It returns true if they are not equal and false if they are equal. + +## Relational GREATER THAN (>) + +The **greater than** (>) operator determines whether the left operand in an expression is greater than the right operand and returns true if so, false if not. + +## Relational LESS THAN (<) + +The **less than** (<) operator determines whether the left operand in an expression is less than the right operand and returns true if so, false if not. + +## Relational GREATER THAN OR EQUAL TO (>=) + +The **greater than or equal to** (>=) operator returns true if the left operand in an expression is greater than or equal to the right operand. Otherwise it returns false. + +## Relational LESS THAN OR EQUAL TO (<=) + +The **less than or equal to** (<=) operator returns true if the left operand in an expression is less than or equal to the right operand. Otherwise it returns false. + +## Codebyte Example + +```codebytes/cpp +#include +using namespace std; + +int main() { + + int w = 2; + int x = 4; + int y = 2; + int z = 9; + + cout << "w = 2, x = 4, y = 2, z = 9\n"; + cout << "True = 1, False = 0\n\n"; + + cout << "(w == y) equals " << (w == y) << "\n"; // Returns true + cout << "(y != w) equals " << (y != w) << "\n"; // Returns false + cout << "(x > w) equals " << (x > w) << "\n"; // Returns true + cout << "(z < x) equals " << (z < x) << "\n"; // Returns false + cout << "(y >= w) equals " << (y >= w) << "\n"; // Returns true + cout << "(x <= y) equals " << (x <= y) << "\n"; // Returns false + + return 0; + +} +``` + +### Output +``` +w = 2, x = 4, y = 2, z = 9 +True = 1, False = 0 + +(w == y) equals 1 +(y != w) equals 0 +(x > w) equals 1 +(z < x) equals 0 +(y >= w) equals 1 +(x <= y) equals 0 +```