位置:首页 > 高级语言 > C++在线教程 > C++逻辑运算符

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