
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Logical Operators in C++
The concept of logical operators is simple. They allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value of the op1 operator op2 or !op1 grouping.
The logical OR operator (||) returns the boolean value true if either or both operands are true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical OR has left-to-right associativity. The operands to the logical OR operator need not be of the same type, but they must be of integral or pointer type. The operands are commonly relational or equality expressions.
The first operand is completely evaluated and all side effects are completed before continuing evaluation of the logical OR expression. The second operand is evaluated only if the first operand evaluates to false (0). This eliminates needless evaluation of the second operand when the logical OR expression is true.
The logical AND operator (&&) returns the boolean value true if both operands are true and return false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical AND has left-to-right associativity. The operands to the logical AND operator need not be of the same type, but they must be of integral or pointer type. The operands are commonly relational or equality expressions.
The first operand is completely evaluated and all side effects are completed before continuing evaluation of the logical AND expression. The second operand is evaluated only if the first operand evaluates to true (nonzero). This evaluation eliminates needless evaluation of the second operand when the logical AND expression is false.
The logical negation operator (!) reverses the meaning of its operand. The operand must be of arithmetic or pointer type (or an expression that evaluates to arithmetic or pointer type). The operand is implicitly converted to type bool. The result is true if the converted operand is false; the result is false if the converted operand is true. The result is of type bool.
Example
#include<iostream> using namespace std; int main() { bool x = true, y = false; cout << (x || y) << endl; cout << (x && y) << endl; cout << (!x) << endl; return 0; }
Output
This will give the output −
1 0 0
This is because one of the 2 is false, so and is false, one is true so or is true and not of true(x) is false, ie, 0.
- Related Articles
- Python Logical Operators
- Java Logical Operators
- Perl Logical Operators
- Relational and Logical Operators in C
- Logical Operators on String in Python?
- What are Logical Operators in JavaScript?
- Logical Operators on String in C#
- Logical Operators on String in Java
- Explain the logical operators in DBMS
- Java Regular expressions Logical operators
- Written version of Logical operators in C++
- What are the logical operators in Java?
- What are the logical operators in C#?
- What types of logical operators are in javascript?
- How to implement relational and logical operators in JShell in Java 9?
