C++ Tutorial/Operators statements/relational and logical operators

Материал из C\C++ эксперт
Версия от 10:30, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A Demonstration of Branching Based on Relational Operators

#include <iostream>
int main()
{
   using std::cout;
   using std::cin;
   int i, i2;
   cout << "Enter the score for the Mets: ";
   cin >> i;
   cout << "\nEnter the score for the Yankees: ";
   cin >> i2;
   if (i > i2)
      cout << "Go i!\n";
   if (i < i2)
   {
      cout << "Go i2!\n";
   }
   if (i == i2)
   {
      cout << "A tie?\n";
      cout << "Give me the real score for the i2: ";
      cin >> i2;
      if (i > i2)
         cout << "i";
      if (i2 > i)
         cout << "i2";
      if (i2 == i)
         cout << "tie!";
   }
   return 0;
}

Create an XOR using the C++ logical operators.

#include <iostream> 
#include <cmath> 
 
using namespace std; 
 
int main() 
{ 
  bool p, q; 
 
  p = true; 
  q = true; 
 
  cout << p << " XOR " << q << " is " << 
    ( (p || q) && !(p && q) ) << "\n"; 
   
 
  p = false; 
  q = true; 
 
  cout << p << " XOR " << q << " is " << 
    ( (p || q) && !(p && q) ) << "\n"; 
   
 
  p = true; 
  q = false; 
 
  cout << p << " XOR " << q << " is " << 
    ( (p || q) && !(p && q) ) << "\n"; 
   
 
  p = false; 
  q = false; 
 
  cout << p << " XOR " << q << " is " << 
    ( (p || q) && !(p && q) ) << "\n"; 
   
 
  return 0; 
}
1 XOR 1 is 0
0 XOR 1 is 1
1 XOR 0 is 1
0 XOR 0 is 0

Logical operator keywords: and, or, not, not_eq

#include <iostream>
using std::boolalpha;
using std::cout;
using std::endl;
int main()
{
   bool a = true;
   bool b = false;
   int c = 2;
   int d = 3;
   cout << boolalpha;
   cout << "a = " << a << "; b = " << b
      << "; c = " << c << "; d = " << d;
   cout << "\n\nLogical operator keywords:";
   cout << "\n   a and a: " << ( a and a );
   cout << "\n   a and b: " << ( a and b );
   cout << "\n    a or a: " << ( a or a );
   cout << "\n    a or b: " << ( a or b );
   cout << "\n     not a: " << ( not a );
   cout << "\n     not b: " << ( not b );
   cout << "\na not_eq b: " << ( a not_eq b );
   return 0;
}
a = true; b = false; c = 2; d = 3
Logical operator keywords:
   a and a: true
   a and b: false
    a or a: true
    a or b: true
     not a: false
     not b: true
a not_eq b: true

Output the results of several variable comparisons.

#include <iostream>
using namespace std;
int main(void)
{
   int a = 4, b = 5;
   cout << a << " > " <<  b << " is " << (a > b) << endl;
   cout << a << " >= " <<  b << " is " << (a >= b) << endl;
   cout << a << " == " <<  b << " is " << (a == b) << endl;
   cout << a << " <= " <<  b << " is " << (a <= b) << endl;
   cout << a << " < " <<  b << " is " << (a < b) << endl;
   return 0;
}

The relational and logical operators

#include <iostream> 
using namespace std; 
 
int main() {    
  int i, j; 
  bool b1, b2; 
 
  i = 10; 
  j = 11; 
  if(i < j) cout << "i < j\n"; 
  if(i <= j) cout << "i <= j\n"; 
  if(i != j) cout << "i != j\n"; 
  if(i == j) cout << "this won"t execute\n"; 
  if(i >= j) cout << "this won"t execute\n"; 
  if(i > j) cout << "this won"t execute\n"; 
 
  b1 = true; 
  b2 = false; 
  if(b1 && b2) cout << "this won"t execute\n"; 
  if(!(b1 && b2)) cout << "!(b1 && b2) is true\n"; 
  if(b1 || b2) cout << "b1 || b2 is true\n"; 
 
  return 0; 
}
i < j
i <= j
i != j
!(b1 && b2) is true
b1 || b2 is true

Use And operator to connect two boolean expressions

#include <iostream>
int main()
{
    int i1, i2;
    i1 = 4;
    i2 = 2;    
    if ((i1 != 0) && (i2 != 0))
        std::cout << "Both are not zero #1\n";
    return (0);
}
Both are not zero #1