位置:首頁 > 高級語言 > 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