A Guide to C++ Or, And and Not Logical Operators

In modern C++, or and and not are keywords that can be used to replace the boolean opeartors &&, || and !, respectively. Learn more.

Written by Swastik Baranwal
Published on Sep. 23, 2024
Developer writing code in C++
Image: Shutterstock / Built In
Brand Studio Logo

In modern C++, you can use and, or and not as boolean operators, which means &&, || and ! respectively. This makes them identical to languages like Python. These operators were in C as macros, but modern C++ introduced them as keywords.

#include <iostream>

int main() {

std::cout << true and true << std::endl;
std::cout << true or false << std::endl;
std::cout << not true << std::endl;

 return 0;
}

This is exactly identical to:

#include <iostream>

int main() {

std::cout << true && true << std::endl;
std::cout << true || false << std::endl;
std::cout << !true << std::endl;

 return 0;
}

You can probably use any one of the styles depending on your codebase.

What are the C++ And, Or and Not Operators

and, or and not are boolean operators that were introduced as keywords in modern C++. Here’s how they work:

  1. And: The and operator is equivalent to && and issued to evaluate two expressions, returning true only if both expressions are true.
  2. Or: The or operator is equivalent to '' and is used to evaluate two expressions, returning true only if one expression is true.
  3. Not: The not operator is equivalent to ! and is used to invert boolean values.

 

C++ Or, And and Not Logical Operators Explained With Code

Below you’ll find an explanation of how the and, or and not operators in C++ work, along with sample code.

1. And Operator

C++ and operator is used for boolean evaluations between boolean values. It is equivalent to && operator. Both are used to evaluate two expressions and return true only if both expressions are true. Otherwise, it returns false.

And Operator Syntax

bool and bool

And Operator Code Example

#include <iostream>
using namespace std;

int main() {
	bool a = true;
	bool b = false;

	if (a and b) {
    	cout << "Both are true" << endl;
	} else {
    	cout << "One or both are false" << endl;
	}

	return 0;
}

More on Software EngineeringSTD::Optional in C++: A Guide

2.Or Operator

C++ or operator is used for boolean evaluations between boolean values. It is equivalent to the || operator. Both are used to evaluate two expressions and return true only if one expressions is true. Otherwise, it returns false.

Or Operator Syntax

bool or bool

Or Operator Code Example

#include <iostream>
using namespace std;

int main() {
	bool a = true;
	bool b = false;

	if (a or b) {
    	cout << "One is true" << endl;
	} else {
    	cout << "Both are false" << endl;
	}

	return 0;
}
A tutorial on how to use logical operators in C++. | Video: Tech With Tim

More on Software EngineeringC++ Lambda Expressions Explained

3. Not Operator

C++ not operator is used to invert boolean values. Not true becomes false and not false becomes true.

Not Operator Syntax

not boolean_value

Not Operator Code Example

#include <iostream>
using namespace std;

int main() {
	bool a = true;
	

	if (not a) {
    	cout << "a is now false" << endl;
	} else {
    	cout << "a is still true" << endl;
	}

	return 0;
}

Even though and, or and not are introduced as keywords, many codebases still use the symbolic form and these should be used when really needed.

Frequently Asked Questions

In modern C++, the logical operators &&, '' and ! are represented as the keywords and, or and not respectively. They are used to compare boolean values.

The logical operators in C++ work as follows:

  • And: The and keyword is used in place of && to compare two values, returning true only if both values are true.
  • Or: The or keyword is used in place of '' and is used to compare two values, returning true only if one value is true.
  • Not: The not keyword is used in place of !, allowing the user to invert boolean values. 
Explore Job Matches.