C++逻辑运算符



尝试以下示例以了解C++中可用的所有逻辑运算符。

将以下C++程序复制并粘贴到test.cpp文件中,然后编译并运行此程序。

#include <iostream>
using namespace std;

main() {
   int a = 5;
   int b = 20;
   int c ;

   if(a && b) {
      cout << "Line 1 - Condition is true"<< endl ;
   }
   
   if(a || b) {
      cout << "Line 2 - Condition is true"<< endl ;
   }
   
   /* Let's change the values of  a and b */
   a = 0;
   b = 10;
   
   if(a && b) {
      cout << "Line 3 - Condition is true"<< endl ;
   } else {
      cout << "Line 4 - Condition is not true"<< endl ;
   }
   
   if(!(a && b)) {
      cout << "Line 5 - Condition is true"<< endl ;
   }
   
   return 0;
}

编译并执行上述代码后,将产生以下结果:

Line 1 - Condition is true
Line 2 - Condition is true
Line 4 - Condition is not true
Line 5 - Condition is true
cpp_operators.htm
广告