这里,程序讨论了 C++ 中的逻辑运算符。
C++ 中的逻辑运算符可以定义为帮助表达式并生成结果表达式的运算符或符号。结果的值将仅取决于输入表达式和逻辑运算符的含义。常见的逻辑运算符包括 AND、OR 和 NOT。它主要用于检查比较操作的有效性和决策制定。
声明一个布尔类型变量来存储逻辑操作的状态。
逻辑运算符程序的解释
步骤 1: 调用头文件 iostream。
步骤 2: 使用 `namespace std`。
步骤 3: 打开整数类型主函数;int main();
步骤 4: 声明一个布尔类型变量 result。
步骤 5: 检查 (3 != 5) && (3 < 5);调用步骤 13
步骤 6: 检查 (3 == 5) && (3 < 5);调用步骤 13
步骤 7: 检查 (3 == 5) && (3 > 5);调用步骤 13
步骤 8: 检查 (3 != 5) || (3 < 5);调用步骤 13
步骤 9: 检查 (3 != 5) || (3 > 5);调用步骤 13
步骤 10: 检查 (3 == 5) || (3 > 5);调用步骤 13
步骤 11: 检查 !(5 == 2); 调用步骤 13
;
步骤 12: 检查 !(5 == 5);调用步骤 13
步骤 13: 打印变量 result 的值。
步骤 14: 退出;
#include <iostream>
using namespace std;
int main() {
bool result;
result = (3 != 5) && (3 < 5); // true
cout << "(3 != 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 < 5); // false
cout << "(3 == 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 > 5); // false
cout << "(3 == 5) && (3 > 5) is " << result << endl;
result = (3 != 5) || (3 < 5); // true
cout << "(3 != 5) || (3 < 5) is " << result << endl;
result = (3 != 5) || (3 > 5); // true
cout << "(3 != 5) || (3 > 5) is " << result << endl;
result = (3 == 5) || (3 > 5); // false
cout << "(3 == 5) || (3 > 5) is " << result << endl;
result = !(5 == 2); // true
cout << "!(5 == 2) is " << result << endl;
result = !(5 == 5); // false
cout << "!(5 == 5) is " << result << endl;
return 0;
}
(3 != 5) && (3 < 5) is 1 (3 == 5) && (3 < 5) is 0 (3 == 5) && (3 > 5) is 0 (3 != 5) || (3 < 5) is 1 (3 != 5) || (3 > 5) is 1 (3 == 5) || (3 > 5) is 0 !(5 == 2) is 1 !(5 == 5) is 0