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
廣告