C / C ++中的关系运算符
关系运算符用于比较两个操作数。这些是需要两个操作数的二进制运算符。这些运算符比较两个操作数,并返回1(true)或0(false)。
关系运算符的类型
关系运算符是
1)等于运算符(==)
等于(==)运算符比较两个操作数,如果两个相同,则返回1;否则,返回1。0,否则。
语法:
operand1 == operand2
示例
//C++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 10; int y = 10; cout << "(x == y) : " << (x == y) << endl; if (x == y) cout << x << " //等于 " << y << endl; else cout << x << " //不等于 " << y << endl; x = 20; y = 30; cout << "(x == y) : " << (x == y) << endl; if (x == y) cout << x << " //等于 " << y << endl; else cout << x << " //不等于 " << y << endl; return 0; }
输出:
(x == y) : 1 10 //等于 10 (x == y) : 0 20 //不等于 30
2)不等于运算符(!=)
不等于(!=)运算符比较两个操作数,如果两个不相同,则返回1;否则,返回1。0,否则。
语法:
operand1 != operand2
示例
//C++程序演示 //示例 #include <iostream> using namespace std; int main(){ int x = 10; int y = 10; cout << "(x != y) : " << (x != y) << endl; if (x != y) cout << x << " //不等于 " << y << endl; else cout << x << " //等于 " << y << endl; x = 20; y = 30; cout << "(x != y) : " << (x != y) << endl; if (x != y) cout << x << " //不等于 " << y << endl; else cout << x << " //等于 " << y << endl; return 0; }
输出:
(x != y) : 0 10 //等于 10 (x != y) : 1 20 //不等于 30
3)少于运算符(<)
小于(<)运算符比较两个操作数,如果第一个操作数小于第二个操作数,则返回1;否则,返回1。0,否则。
语法:
operand1 < operand2
示例
//C++程序演示 // example of < operator #include <iostream> using namespace std; int main(){ int x = 10; int y = 10; cout << "(x < y) : " << (x < y) << endl; if (x < y) cout << x << " //小于 " << y << endl; else cout << x << " //不少于 " << y << endl; x = 20; y = 30; cout << "(x < y) : " << (x < y) << endl; if (x < y) cout << x << " //小于 " << y << endl; else cout << x << " //不少于 " << y << endl; return 0; }
输出:
(x < y) : 0 10 //不少于 10 (x < y) : 1 20 //小于 30
4)小于或等于运算符(<=)
小于或等于(<=)运算符比较两个操作数,如果第一个操作数小于或等于第二个操作数,则返回1;否则,返回1。0,否则。
语法:
operand1 <= operand2
示例
//C++程序演示 // example of <= operator #include <iostream> using namespace std; int main(){ int x = 10; int y = 10; cout << "(x <= y) : " << (x <= y) << endl; if (x <= y) cout << x << " //小于或等于 " << y << endl; else cout << x << " //不小于或等于 " << y << endl; x = 40; y = 30; cout << "(x <= y) : " << (x <= y) << endl; if (x <= y) cout << x << " //小于或等于 " << y << endl; else cout << x << " //不小于或等于 " << y << endl; return 0; }
输出:
(x <= y) : 1 10 //小于或等于 10 (x <= y) : 0 40 //不小于或等于 30
5)大于运算符(>)
大于运算符(>)运算符比较两个操作数,如果第一个操作数大于第二个操作数,则返回1;否则,返回1。0,否则。
语法:
operand1 > operand2
示例
//C++程序演示 // example of > operator #include <iostream> using namespace std; int main(){ int x = 10; int y = 10; cout << "(x > y) : " << (x > y) << endl; if (x > y) cout << x << " //大于 " << y << endl; else cout << x << " //不大于 " << y << endl; x = 40; y = 30; cout << "(x > y) : " << (x > y) << endl; if (x > y) cout << x << " //大于 " << y << endl; else cout << x << " //不大于 " << y << endl; return 0; }
输出:
(x > y) : 0 10 //不大于 10 (x > y) : 1 40 //大于 30
6)大于或等于运算符(>=)
大于或等于运算符(>=)运算符比较两个操作数,如果第一个操作数大于或等于第二个操作数,则返回1;否则,返回1。0,否则。
语法:
operand1 >= operand2
示例
//C++程序演示 // example of >= operator #include <iostream> using namespace std; int main(){ int x = 10; int y = 10; cout << "(x >= y) : " << (x >= y) << endl; if (x >= y) cout << x << " //大于 or equal to " << y << endl; else cout << x << " //不大于 or equal to " << y << endl; x = 20; y = 30; cout << "(x >= y) : " << (x >= y) << endl; if (x >= y) cout << x << " //大于 or equal to " << y << endl; else cout << x << " //不大于 or equal to " << y << endl; return 0; }
输出:
(x >= y) : 1 10 //大于 or equal to 10 (x >= y) : 0 20 //不大于 or equal to 30
C++程序演示各种关系运算符的示例
//C++程序演示 example //各种关系运算符 #include <iostream> using namespace std; int main(){ int x = 10; int y = 20; //比较 cout << "(x == y) : " << (x == y) << endl; cout << "(x != y) : " << (x != y) << endl; cout << "(x < y) : " << (x < y) << endl; cout << "(x <= y) : " << (x <= y) << endl; cout << "(x > y) : " << (x > y) << endl; cout << "(x >= y) : " << (x >= y) << endl; //比较 using conditions if (x == y) cout << x << " //等于 " << y << endl; else cout << x << " //不等于 " << y << endl; if (x != y) cout << x << " //不等于 " << y << endl; else cout << x << " //等于 " << y << endl; if (x < y) cout << x << " //小于 " << y << endl; else cout << x << " //不少于 " << y << endl; if (x <= y) cout << x << " //小于或等于 " << y << endl; else cout << x << " //不小于或等于 " << y << endl; if (x > y) cout << x << " //大于 " << y << endl; else cout << x << " //不大于 " << y << endl; if (x >= y) cout << x << " //大于 or equal to " << y << endl; else cout << x << " //不大于 or equal to " << y << endl; return 0; }
输出:
(x == y) : 0 (x != y) : 1 (x < y) : 1 (x <= y) : 1 (x > y) : 0 (x >= y) : 0 10 //不等于 20 10 //不等于 20 10 //小于 20 10 //小于或等于 20 10 //不大于 20 10 //不大于 or equal to 20