关系运算符-嵌入式实时操作系统uc/os-ii

时间:2024-07-13 19:10:30
【文件属性】:

文件名称:关系运算符-嵌入式实时操作系统uc/os-ii

文件大小:6.14MB

文件格式:PDF

更新时间:2024-07-13 19:10:30

C++

表 5.1 关系运算符 运算符名称 描述 小于(<) 如果左边的操作数小于右边的操作数(op1 < op2),则结果 为 true,否则结果为 false 大于(>) 如果左边的操作数小于右边的操作数(op1 > op2),则结果 为 true,否则结果为 false 小于等于(<=) 如果左边的操作数小于或等于右边的操作数,则结果为 true, 否则结果为 false 大于等于(>=) 如果左边的操作数大于或等于右边的操作数,则结果为 true, 否则结果为 false 如表 5.1 所示,比较运算的结果总是布尔值,即要么为 true,要么为 false。下面的示例代码演示了 如何使用表 5.1 所示的关系运算符: int personAge = 20; bool checkLessThan = (personAge < 100); // true bool checkGreaterThan = (personAge > 100); // false bool checkLessThanEqualTo = (personAge <= 20); // true bool checkGreaterThanEqualTo = (personAge >= 20); // true bool checkGreaterThanEqualToAgain = (personAge >= 100); // false 程序清单 5.3 演示了这些运算符的作用—将结果显示在屏幕上。 程序清单 5.3 演示相等运算符和关系运算符 0: #include 1: using namespace std; 2: 3: int main() 4: { 5: cout << "Enter two integers:" << endl; 6: int num1 = 0, num2 = 0; 7: cin >> num1; 8: cin >> num2; 9: 10: bool isEqual = (num1 == num2); 11: cout << "Result of equality test: " << isEqual << endl; 12: 13: bool isUnequal = (num1 != num2); 14: cout << "Result of inequality test: " << isUnequal << endl; 15: 16: bool isGreaterThan = (num1 > num2); 17: cout << "Result of " << num1 << " > " << num2; 18: cout << " test: " << isGreaterThan << endl; 19: 20: bool isLessThan = (num1 < num2); 21: cout << "Result of " << num1 << " < " << num2 << " test: " << isLessThan << endl; 22: 23: bool isGreaterThanEquals = (num1 >= num2); 24: cout << "Result of " << num1 << " >= " << num2; 25: cout << " test: " << isGreaterThanEquals << endl;


网友评论