// ---
// jupyter:
//   jupytext:
//     text_representation:
//       extension: .cpp
//       format_name: light
//       format_version: '1.5'
//       jupytext_version: 1.14.4
//   kernelspec:
//     display_name: C++11
//     language: C++11
//     name: xeus-cling-cpp11
// ---

// # Examples of logical and comparison operations

#include <iostream>

int a = 0, b = 1;

// **Notice that comparison operators take lower priority than output operator `<<` thus requiring parentheses**

std::cout << (a==b); // is a==b?

std::cout << (a<b); // is a<b?

std::cout << (a<=b); // is a<=b?

// ## Logical Operators
// 1. `&&` and
// 2. `||` or
// 3. `!` not, unary operator
// 4. `^` xor, this is not a logical but a bitwise operator

// checking a<b is equiv. to checking not a>=b thus
std::cout << (a<b) << " is equiv. to " << (!(a>=b));

// checking a==b is equiv. to checking not a!=b thus
std::cout << (a==b) << " is equiv. to " << (!(a!=b));

// in general, any integers can be converted to logical values
// so we can apply not operator for integers
a = 100;
// !a first convert a into logical value true, so this statement
// is equiv. to !true, which false
std::cout << (!a);

// Let's look at some examples

std::cout << (true && false); // true and false is false

std::cout << (true && true); // true and true is true

std::cout << (false || true); // true or false is true

std::cout << (false || false); // false or false is false

std::cout << (true ^ false); // true xor false is true

std::cout << (true ^ true); // true xor true is false

std::cout << (false ^ false); // false xor false if false

std::cout << (false || true && false); // and takes higher priority than or does, so this is false

StarcoderdataJupyterScriptsDedupFiltered