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:
And
: Theand
operator is equivalent to&&
and issued to evaluate two expressions, returningtrue
only if both expressions aretrue
.Or
: The or operator is equivalent to''
and is used to evaluate two expressions, returningtrue
only if one expression istrue
.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;
}
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;
}
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
What are the logical operators in C++?
In modern C++, the logical operators &&
, ''
and !
are represented as the keywords and
, or
and not
respectively. They are used to compare boolean values.
How do logical operators work in C++?
The logical operators in C++ work as follows:
And
: The and keyword is used in place of&&
to compare two values, returningtrue
only if both values aretrue
.Or
: Theor
keyword is used in place of''
and is used to compare two values, returningtrue
only if one value istrue
.Not
: Thenot
keyword is used in place of!
, allowing the user to invert boolean values.