Skip to main content

4.6) Logical Operations


Examples

int x = 2, y = 5;
cout << boolalpha;           // to display booleans as words
// Modern syntax
cout << not (x == 3);        // prints "true"
cout << (x > 2 and y < 8);   // prints "false"
cout << (x < 5 or y < 5);    // prints "true"
// Older syntax
cout << !(x == 3);
cout << (x > 2 && y < 8);
cout << (x < 5 || y < 5);

Points to Note

  • Logical operations of 'not', 'and', 'or' are represented by keywords not, and, or
  • These operations can also be represented using !, && and ||, respectively; you may have to use these more traditional representations with older compilers
  • Typically, we use these operators to construct more complex tests in conditional statements